Packages and Libraries
The spring mass system that we would like to create in TikZ is shown below. Mainly, it has three parts: 1) the support, 2) the spring and 3) the mass.
![Spring Mass system in TikZ Spring Mass system in TikZ](https://i0.wp.com/latexdraw.com/wp-content/uploads/2021/04/Spring-Mass-system-in-TikZ.png?resize=450%2C468&ssl=1)
We need to load TikZ as it's the main drawing package. We need patterns which is a TikZ library for the support. In addition, we need to load decorations.pathmorphing which is also a TikZ library for drawing the spring!
Step 1: draw the support
The support corresponds to a rectangle shape filled with north west lines pattern.
Details: Let's consider a rectangle of 3cm width and 0.2cm height. The rectangle has no border and filled with north west lines pattern. We will add a thick line at the bottom side of the rectangle to get the following illustration:
![Supporting structure Supporting structure](https://i0.wp.com/latexdraw.com/wp-content/uploads/2021/04/Supporting-structure.png?resize=405%2C73&ssl=1)
and here is the corresponding LaTeX code:
\documentclass[border=0.2cm]{standalone} % Required package and libraries \usepackage{tikz} \usetikzlibrary{decorations.pathmorphing,patterns} \begin{document} \begin{tikzpicture}[black!75,thick] % Supporting structure \fill [pattern = north west lines] (-1.5,0) rectangle ++(3,.2); \draw[thick] (-1.5,0) -- ++(3,0); \end{tikzpicture} \end{document}
Step 2: draw the spring
As mentioned above, the spring can be drawn using decorations.pathmorphing library. Check the following line code which highlights different coil shape options:
\draw [ decoration={ coil, segment length = 1mm, amplitude = 2mm, aspect = 0.5, post length = 3mm, pre length = 3mm}, decorate] (0,0) -- ++(0,-2.5)
Coil options
![Amplitude coil TikZ Amplitude coil TikZ](https://i0.wp.com/latexdraw.com/wp-content/uploads/2021/04/Amplitude-coil-TikZ.png?resize=180%2C446&ssl=1)
![Segment length coil TikZ Segment length coil TikZ](https://i0.wp.com/latexdraw.com/wp-content/uploads/2021/04/Segment-length-coil-TikZ.png?resize=260%2C455&ssl=1)
![Aspect coil TikZ Aspect coil TikZ](https://i0.wp.com/latexdraw.com/wp-content/uploads/2021/04/Aspect-coil-TikZ.png?resize=180%2C543&ssl=1)
![Pre Post lines coil TikZ Pre Post lines coil TikZ](https://i0.wp.com/latexdraw.com/wp-content/uploads/2021/04/Pre-Post-lines-coil-TikZ.png?resize=260%2C576&ssl=1)
Details: In our case, we will draw the coil from (0,0) to (0,-3) with the following parameters:
- aspect=0.3, - segment length=1.2mm, - amplitude=2mm, - pre length=3mm,- post length=3mm
Here is the corresponding code:
\documentclass[border=0.2cm]{standalone} % Required package and libraries \usepackage{tikz} \usetikzlibrary{decorations.pathmorphing,patterns} \begin{document} \begin{tikzpicture}[black!75,thick] % Supporting structure \fill [pattern = north west lines] (-1.5,0) rectangle ++(3,.2); \draw[thick] (-1.5,0) -- ++(3,0); % Spring \draw [ decoration={ coil, aspect=0.3, segment length=1.2mm, amplitude=2mm, pre length=3mm, post length=3mm}, decorate ] (0,0) -- ++(0,-3) node[midway,right=0.25cm,black]{$k$}; \end{tikzpicture} \end{document}
Compiling this code yields:
![Draw a spring in TikZ Draw a spring in TikZ](https://i0.wp.com/latexdraw.com/wp-content/uploads/2021/04/Draw-a-spring-in-TikZ.png?resize=350%2C369&ssl=1)
We have added a text node to the right of the middle point of the spring.
Step 3: draw the mass
The mass corresponds to a rectangle filled with yellow color and has a label m. We will draw it using rectangle node shape (more details). Here is the corresponding LaTeX code:
\documentclass[border=0.2cm]{standalone} % Required package and libraries \usepackage{tikz} \usetikzlibrary{decorations.pathmorphing,patterns} \begin{document} \begin{tikzpicture}[black!75,thick] % Supporting structure \fill [pattern = north west lines] (-1.5,0) rectangle ++(3,.2); \draw[thick] (-1.5,0) -- ++(3,0); % Spring \draw [ decoration={ coil, aspect=0.3, segment length=1.2mm, amplitude=2mm, pre length=3mm, post length=3mm}, decorate ] (0,0) -- ++(0,-3) node[midway,right=0.25cm,black]{$k$}; % Mass \node[draw, fill=yellow!60, minimum width=1cm, minimum height=0.75cm, anchor=north, label=east:$m$] at (0,-3) {}; \end{tikzpicture} \end{document}
Compiling this code yields:
![Simple mass spring in TikZ Simple mass spring in TikZ](https://i0.wp.com/latexdraw.com/wp-content/uploads/2021/04/Simple-mass-spring-in-TikZ.png?resize=350%2C447&ssl=1)
Comments:
- The node by default has a rectangle shape that can be highlighted by adding draw option (draws the node shape border).
- fill=yellow!60: adds a light yellow color to the rectangle shape
- minimum width and minimum height sets the minimum size of the rectangle.
-anchor=north: by default the node center will be positioned on the provided coordinates (0,-3). Adding this option will position the top of the rectangle on the coordinates (0,-3). More details can be found in the tutorial: draw a rectangle in TikZ.
- label=east:$m$ : adds the text label $m$ on the right of the node shape.
Spring Mass system LaTeX code
Here is the final LaTeX code of a simple spring mass system in TikZ. We have added arrows to the previous code and for more details you can check the post: TikZ arrows.
\documentclass[border=0.2cm]{standalone} % Required package and libraries \usepackage{tikz} \usetikzlibrary{decorations.pathmorphing,patterns} \begin{document} \begin{tikzpicture}[black!75,thick] % Supporting structure \fill [pattern = north west lines] (-1.5,0) rectangle ++(3,.2); \draw[thick] (-1.5,0) -- ++(3,0); % Spring \draw [ decoration={ coil, aspect=0.3, segment length=1.2mm, amplitude=2mm, pre length=3mm, post length=3mm}, decorate ] (0,0) -- ++(0,-3) node[midway,right=0.25cm,black]{$k$}; % Mass \node[draw, fill=yellow!60, minimum width=1cm, minimum height=0.75cm, anchor=north, label=east:$m$] at (0,-3) {}; % x1 arrow \draw[very thick, red, |-latex] (-2,0) -- ++(0,-1) node[midway,left]{\small $x_1$}; % x2 arrow \draw [very thick, blue, -latex ] (-0.8,-3.4) -- ++(-1,0) ++(0.25,0) -- ++ (0,-1) node[midway,left]{\small $x_2$}; \end{tikzpicture} \end{document}
![Spring Mass system in TikZ Spring Mass system in TikZ](https://i0.wp.com/latexdraw.com/wp-content/uploads/2021/04/Spring-Mass-system-in-TikZ.png?resize=450%2C468&ssl=1)
Extension of the spring Mass system
![Extension of a coil spring Mass system in TikZ LaTeX Extension of a coil spring Mass system in TikZ LaTeX](https://i0.wp.com/latexdraw.com/wp-content/uploads/2021/04/Extension-of-a-coil-spring-Mass-system-in-TikZ-LaTeX.png?resize=676%2C320&ssl=1)
The post Spring Mass system in TikZ: Short Drawing Guide appeared first on TikZBlog.
See also: Original Source by Benmiloud Mohammed
Note: The copyright belongs to the blog author and the blog. For the license, please see the linked original source blog.
Leave a Reply
You must be logged in to post a comment.