# Draw mechanical springs in TikZ

Currently I am using the following path style to draw springs in TikZ

\tikzstyle{spring}=[thick,decorate,decoration={zigzag,pre length=0.1cm,post
length=0.1cm,segment length=6}]


for example

\draw[spring] (0,0) -- (0,1);


However is it possible to make the springs look like in the following picture:

Is there any nice way to incorporate the possibility of drawing the spring stretched or compressed like in the picture above?

-
Have a look at tikzpicture of several springs. –  Stefan Kottwitz Jan 19 '12 at 16:28

You can use the coil decoration for the effect. With segment length you can mimic the compression amount.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,patterns}
\begin{document}
\begin{tikzpicture}
\node[circle,fill=blue,inner sep=2.5mm] (a) at (0,0) {};
\node[circle,fill=blue,inner sep=2.5mm] (b) at (2,2) {};
\draw[decoration={aspect=0.3, segment length=3mm, amplitude=3mm,coil},decorate] (0,5) -- (a);
\draw[decoration={aspect=0.3, segment length=1.5mm, amplitude=3mm,coil},decorate] (2,5) -- (b);
\fill [pattern = north east lines] (-1,5) rectangle (3,5.2);
\draw[thick] (-1,5) -- (3,5);
\end{tikzpicture}
\end{document}


-

Only for PSTricks' fans:

\documentclass{article}
\usepackage{pst-coil}

\usepackage[active,tightpage]{preview}
\PreviewEnvironment{pspicture}
\PreviewBorder=12pt

\psset
{
coilarm=0.25,
coilwidth=0.3
}

\begin{document}

\begin{pspicture}[showgrid](-1,-2)(9,5)
% Ceiling
\psframe
[
fillstyle=vlines,
hatchsep=2pt,
hatchwidth=0.5\pslinewidth,
hatchcolor=gray,
hatchangle=45,
linestyle=none
](0,4)(8,4.25)

% Spring without box
\uput[90](1,4.25){A}
\pszigzag[coilheight=0.3](1,4)(1,2)

% Spring stretched due to box weight
\uput[90](3,4.25){B}
\pszigzag[coilheight=0.5](3,4)(3,1)
\psframe*[origin={3,1}](-0.5,0)(0.5,-1)

% Spring stretched by external force
\uput[90](5,4.25){C}
\pszigzag[coilheight=0.8](5,4)(5,-0.5)
\psframe*[origin={5,-0.5}](-0.5,0)(0.5,-1)

% Position at any time
\uput[90](7,4.25){D}
\pszigzag[coilheight=0.7](7,4)(7,0)
\psframe*[origin={7,0}](-0.5,0)(0.5,-1)

% Reference lines
\psset{linecolor=red,linestyle=dashed}
\psline(0,2)(8,2)
\psline(0,1)(3,1)
\psline(0,-0.5)(5,-0.5)
\psline(7,0)(8,0)

% Labels
\psset{linecolor=blue,linestyle=solid}
\psline{<->}(-0.1,2)(-0.1,1)
\uput[180](-0.1,1.5){$\Delta y$}
\psline{<->}(-0.1,1)(-0.1,-0.5)
\uput[180](-0.1,0.25){$A$}
\psline{|->}(8.1,2)(8.1,0)
\uput[0](8.1,1){$y$}
\rput[br](8.5,-1.75){\tiny created by Forgiver \copyright 2012}
\end{pspicture}

\end{document}


If we set gridstyle to false, we will get the cleaner result as follows:

Note that I keep the number of winding equal for each spring to make them more realistic.

Edit:

\documentclass[border=0pt,pstricks]{standalone}
\usepackage[nomessages]{fp}

\FPset\CoilArm{0.25}
\FPset\CoilWidth{0.3}
\FPeval\CoilTurn{round(50/3:3)}
\FPeval\DeltaY{0.5}
\FPeval\Amp{1.5}
\FPeval\FPS{25}
\FPeval\Vx{2}% propagation speed
\FPeval\Period{1}% second

\psset
{
coilarm=\CoilArm,
coilwidth=\CoilWidth,
}

\newcommand\System[4][0]{% #1: frame, #2: x, #3: y, #4: label
\uput[90](#2,4.25){#4}
\FPeval\CoilHeight{round((4-(#3)-2*CoilArm)/(CoilWidth*CoilTurn):3)}
\pszigzag[coilheight=\CoilHeight,linejoin=2](#2,4)(#2,#3)
\ifnum#1=1
\bgroup
\psset{origin={#2,#3}}
\psframe[dimen=inner,fillstyle=solid,fillcolor=black](-0.5,0)(0.5,-1)
\psdot[linecolor=yellow](0,-0.5)
\egroup
\fi
}

\begin{document}
\FPeval\DeltaTime{round(1/\FPS:2)}
\FPeval\TotalFrame{round(\FPS*\Period:0)}
\multido{\n=0.00+\DeltaTime}{\TotalFrame}{%
\begin{pspicture*}[showgrid=false](-1.5,-2)(3.5,5)
% Ceiling
\psframe
[
fillstyle=vlines,
hatchsep=2pt,
hatchwidth=0.5\pslinewidth,
hatchcolor=gray,
hatchangle=45,
%linestyle=none
](0,4)(2,4.25)
% Spring without box
\FPeval\Y{round(-DeltaY-Amp*cos(2*pi*\n/Period)+2:3)}
\System[1]{1}{\Y}{A}
\psplot[algebraic,linecolor=red,plotpoints=1000]
{-1.5}{3.5}{-\DeltaY-\Amp*cos((2*\psPi/\Period)*((-\Vx*\n+x-1)/\Vx))+2-0.5}
\end{pspicture*}}

\end{document}

-
The physical scenario for this diagram is as follows: (A) a massless spring is hanged on the ceiling. (B) a box is attached to the spring's lower end and very very slowly released until it stretches the spring \Delta y. (C) An external force pulls the box until it stops at a distance of A. At t=0 the external force is removed. (D) The position of the spring lower end at any time t. The solution is y(t)=\Delta y + A\cos(\omega t). The solution will be different if we change the condition in (B): we release the box to fall freely. –  Please don't touch Jun 4 '12 at 9:30
Please ignore this copyright label because it's just for fun. –  Please don't touch Jun 4 '12 at 9:51
Nice animation! If you set the corners of the lines on the spring to be bevelled, you won't get the artifact of the corners getting shorter when the mass is near the top. In TikZ, you'd set line join=bevel, I'm sure there's something similar for PSTricks. –  Jake Aug 11 '12 at 8:49
@Jake: Thanks for the comment. I have updated the animation. –  Please don't touch Aug 11 '12 at 9:13

This is mainly modified from this answer.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,patterns}
\usetikzlibrary{calc,patterns,decorations.markings}
\usetikzlibrary{positioning}
%
\newcommand{\myfig}[4]{%
\tikzstyle{spring}=[thick,decorate,decoration={aspect=0.5, segment length=#1, amplitude=2mm,coil}]
\tikzstyle{platform}=[fill,pattern=north east lines,draw=none,minimum width=2cm,minimum height=0.3cm]
%
\coordinate (g) at (0,0);
\coordinate (topspring) at (0,-1cm);
\coordinate (bottomspring) at (0,{#2}); %%changing the values (5cm) here will compress or expand the spring
\coordinate (pt2) at ($(bottomspring) + (0,-.5cm)$); %% this is relative.
\coordinate (pt3) at ($(pt2) + (0,#3)$); %% this is relative.
%
\node [platform,anchor=south] at (g)  {};
\draw[very thick] (-1,0) -- (1,0);
\draw [thick](topspring)--(g);
\draw [spring] (bottomspring) -- (topspring);
\draw [thick] (bottomspring) -- (pt2.north);
\draw [fill=black] (pt3) circle (#3) node[draw=none,inner sep = 0,scale=#4,text=white]{$m$};
}
\begin{document}
%
\begin{tikzpicture}[yshift=0cm,every node/.style={draw,outer sep=0pt,thick}]
\begin{scope}[xshift=-2.5cm]
\myfig{1mm}{-3cm}{-0.1cm}{0}
\node[draw=none,right=.1cm] at (pt3)(a) {$A$};
\draw [thick,dashed] ($(pt3) + (0.8,0)$) -- +(1.2,0)node[draw=none,inner sep = 0,pos=.5](a1){};
\draw [thick,dashed,] ($(pt3) + (3.3,0)$) -- +(1.2,0)node[draw=none,inner sep = 0,pos=.5](a2){};
\end{scope}
%
\begin{scope}
\myfig{2mm}{-4.8cm}{-0.35cm}{1.5}
\node[draw=none,right=.25cm] at (pt3)(b) {$B$};
\draw [thick,dashed] ($(pt3) + (-1.7,0)$) -- +(1.2,0)node[draw=none,inner sep = 0,pos=.5](b1){};
\draw[thick,latex-latex] (a1) -- (b1)node[draw=none,inner sep = 0,pos=.5,right=0.1cm]{$\delta l_1$};
\end{scope}
%
\begin{scope}[xshift=2.5cm]
\myfig{3mm}{-6.6cm}{-0.4cm}{1.8}
\node[draw=none,right=.3cm] at (pt3)(b) {$C$};
\draw [thick,dashed] ($(pt3) + (-1.7,0)$) -- +(1.2,0)node[draw=none,inner sep = 0,pos=.5](c1){};
\draw[thick,latex-latex] (a2) -- (c1)node[draw=none,inner sep = 0,pos=.5,right=0.1cm]{$\delta l_2$};
\end{scope}
%
\end{tikzpicture}
%
\end{document}


Kindly excuse me if the code is cluttered a bit. It can be simplified further by inserting extra lines inside the scope, into the macro \myfig itself.

# Let us have some fun in this episode of having some free time and getting bored:

\documentclass{article}
\usepackage{filecontents}%
\begin{filecontents*}{beam.tex}
\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,patterns}
\usetikzlibrary{calc,patterns,decorations.markings}
\usetikzlibrary{positioning}
%
\newcommand{\myfig}[4]{%
\tikzstyle{spring}=[thick,decorate,decoration={aspect=0.5, segment length=#1, amplitude=2mm,coil}]
\tikzstyle{platform}=[fill,pattern=north east lines,draw=none,minimum width=2cm,minimum height=0.3cm]
%
\coordinate (g) at (0,0);
\coordinate (topspring) at (0,-.2cm);
\coordinate (bottomspring) at (0,{#2}); %%changing the values (5cm) here will compress or expand the spring
\coordinate (pt2) at ($(bottomspring) + (0,-1cm)$); %% this is relative.
\coordinate (pt3) at ($(pt2) + (0,#3)$); %% this is relative.
%
\node [platform,anchor=south] at (g)  {};
\draw[very thick] (-1,0) -- (1,0);
\draw [thick](topspring)--(g);
\draw [spring] (bottomspring) -- (topspring);
\draw [thick] (bottomspring) -- (pt2.north);
\draw [fill=black] (pt3) circle (#3) node[draw=none,inner sep = 0,scale=#4,text=white]{$m$};
}
\begin{document}
%
\foreach \x/\y/\r/\s in {1/19/1/.3,1.4/24.8/2/.5,1.8/30.5/3/.7,2.2/36.3/4/.9,2.6/42/5/1.1,3/47.8/6/1.4,3.4/53.6/7/1.8,3.8/59.4/8/2.3,
4.2/65.2/9/2.7,4.4/68/10/3.2}{
\begin{tikzpicture}[scale=1,yshift=0cm,every node/.style={draw,outer sep=0pt,thick}]
\myfig{\x mm}{-\y mm}{-\r mm}{\s}
\path (-1,0) rectangle (1,-10);
\end{tikzpicture}
}
\end{document}
\end{filecontents*}
%create the beam.pdf.
\immediate\write18{pdflatex beam}
% convert to GIF animation
\immediate\write18{convert -delay 10 -loop 0 -density 200 -alpha remove beam.pdf beam.gif}

\begin{document}
This is empty
\end{document}


-
To be more realistic, you need to keep the number of winding equal for each coil or spring. –  Please don't touch Jun 4 '12 at 3:17
@AdorableCreature: You changed your name again!. Now it (the spring) is more realistic. You may start counting ;-) –  Harish Kumar Dec 25 '12 at 12:13
It is a very realistic animation. –  Please don't touch Dec 25 '12 at 14:36
But it is difficult to implement this animation in a lab. :-) –  Please don't touch Dec 25 '12 at 14:52

Here a variation from the answer of Harish Kumar. First it's preferable to avoid tikzstyle inside the macro \myfig. I keep only springstyle but inside a scope to work with a local style. I change the options for every node. Now thick is set up for every path, except when there is a local change. I change the way to draw the lines between the figs.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{patterns,decorations.pathmorphing}

\newcommand{\myfig}[6]{%
\begin{scope}[xshift=#6,
spring/.style = {decorate,
decoration = {aspect         = 0.5,
segment length = #1,
amplitude      = 2mm,
coil}}]

\path (0,0)                            coordinate (g)
(0,-1cm)                         coordinate (topspring)
(0,#2)                           coordinate (bottomspring)
(bottomspring) ++(0,-.5cm)       coordinate (pt2)
+(0cm,-#3)       coordinate (pt3)
+(1.25cm,-#3)    coordinate (#5 pt3);

\node [platform,
anchor = south] at (g)  {};
\draw [very thick]    (-1,0)         -- (1,0);
\draw                (topspring)     -- (g)
(bottomspring)  -- (pt2.north);
\draw [spring]       (bottomspring)  -- (topspring);
\draw [fill=black] (pt3) circle (#3)
node[inner sep = 0,
scale     = #4,
text      = white]{$m$};
\node[right=1.5*#3] at (pt3) {#5} ;
\end{scope}
}

\begin{document}
\begin{tikzpicture}[thick,
every node/.style = {draw      = none,
inner sep = 0pt,
outer sep = 0pt},
platform/.style   = {fill,
pattern = north east lines,
minimum width  = 2cm,
minimum height  =0.3cm}]
\myfig{1mm}{-4cm}{0.1cm}{0}{A}{-2.5cm}
\myfig{3mm}{-6cm}{0.35cm}{1.5}{B}{0cm}
\myfig{3mm}{-10cm}{0.4cm}{1.8}{C}{2.5cm}

\draw[dashed]  (A pt3)  +(-0.6,0)     --  +(0.6,0)
+(-0.6+2.5,0) -- coordinate (b1) +(0.6+2.5,0)
(B pt3)  +(-0.6-2.5,0) -- coordinate (a2) +(0.6-2.5,0)
(C pt3)  +(-0.6-2.5,0) -- coordinate (b2) +(0.6-2.5,0) ;

\draw[latex-latex] (A pt3) -- node[right=0.1cm]{$\delta l_1$} (a2);
\draw[latex-latex] (b1)    -- node[right=0.1cm]{$\delta l_2$} (b2);

\end{tikzpicture}
\end{document}


-