Draw mechanical springs in TikZ
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{pst-coil,pstricks-add}
\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}
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}