[Tex/LaTex] Draw mechanical springs in TikZ

technical-drawingtikz-pgf

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:

Springs

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

Best Answer

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}

enter image description here