[Tex/LaTex] Appending a line (of fixed length) to a spring drawn with tikz coil

decorationstikz-pgf

Looking at the nice answer here: Draw mechanical springs in TikZ, I would like to attach a line of fixed length (say 5mm) to the top of the spring (to the platform) and bottom of the spring (to the mass).

The key here is I would like these segments to remain (with length 5mm) whether the spring in the answer to the post above is uncompressed or compressed.

Edit: Here's what I've got thus far, but getting my linear segment from the bottom of the coil to the top of the mass is giving me trouble.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,patterns}
\usetikzlibrary{calc,patterns,decorations.markings}
\usetikzlibrary{positioning,snakes}

\begin{document}

\begin{tikzpicture}[every node/.style={draw,outer sep=0pt,thick}]

\tikzstyle{spring}=[thick,decorate,decoration={aspect=0.5, segment length=1mm, amplitude=2mm,coil}]
\tikzstyle{dampener}=[thick,decoration={markings,
  mark connection node=dmp,
  mark=at position 0.5 with
  {
    \node (dmp) [thick,inner sep=0pt,transform shape,rotate=-90,minimum width=15pt,minimum height=3pt,draw=none] {};
    \draw [thick] ($(dmp.north east)+(2pt,0)$) -- (dmp.south east) -- (dmp.south west) -- ($(dmp.north west)+(2pt,0)$);
    \draw [thick] ($(dmp.north)+(0,-5pt)$) -- ($(dmp.north)+(0,5pt)$);
  }
}, decorate]
\tikzstyle{platform}=[fill,pattern=north east lines,draw=none,minimum width=2cm,minimum height=0.3cm]

\node (g) [platform,anchor=north] {};

\node[draw=none,below=1.25cm of g,inner sep=-2,minimum size=0mm] (topspring){};

\node[draw=none,below=1.25cm of topspring] (bottomspring){};
\node[draw=none,below=.5cm of bottomspring] (attachment){};

\node[circle,below=.5cm of attachment] (pt2){$m$};

\draw [dampener](topspring.north)--(g.south) node[draw=none,pos=.52,right=.4cm] {$b$};
\draw [spring] (topspring.south) -- (bottomspring.north) node[draw=none,pos=.5,right=.25cm] {$k$};
\draw [thick] (bottomspring) -- (attachment);

\end{tikzpicture}

enter image description here

Edit #2: A little modification of Harish's answer allowed me to get the look I was after, with the coil tightness varying with the displacement of the mass.

enter image description here

Edit #3: Harish's answer gives a very efficient solution for adjusting multiple parameters.

Best Answer

I do not think I understood you clearly. But, is it like this?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,patterns}
\usetikzlibrary{calc,patterns,decorations.markings}
\usetikzlibrary{positioning,snakes}

\begin{document}

\begin{tikzpicture}[every node/.style={draw,outer sep=0pt,thick}]

\tikzstyle{spring}=[thick,decorate,decoration={aspect=0.5, segment length=1mm, amplitude=2mm,coil}]
\tikzstyle{dampener}=[thick,decoration={markings,
  mark connection node=dmp,
  mark=at position 0.5 with
  {
    \node (dmp) [thick,inner sep=0pt,transform shape,rotate=-90,minimum width=15pt,minimum height=3pt,draw=none] {};
    \draw [thick] ($(dmp.north east)+(2pt,0)$) -- (dmp.south east) -- (dmp.south west) -- ($(dmp.north west)+(2pt,0)$);
    \draw [thick] ($(dmp.north)+(0,-5pt)$) -- ($(dmp.north)+(0,5pt)$);
  }
}, decorate]
\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,-1.25cm);
\coordinate (bottomspring) at (0,-4cm); %%changing the values (5cm) here will compress or expand the spring
\coordinate (pt2) at ($(bottomspring) + (0,-.5cm)$); %% this is relative. 

\node [platform,anchor=south] at (g)  {};
\draw[very thick] (-1,0) -- (1,0);
\draw [dampener](topspring)--(g) node[draw=none,pos=.52,right=0.25cm] {$b$};
\draw [spring] (bottomspring) -- (topspring) node[draw=none,pos=.5,right=.25cm] (spring){$k$};
\draw [thick] (bottomspring) -- (pt2.north);
\draw ($(pt2) + (0,-.2)$) circle (.2cm) node[draw=none,inner sep = 0]{$m$};

\end{tikzpicture}
\end{document}

enter image description here

Edit: To answer the query in the comment:

We can use \scope.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,patterns}
\usetikzlibrary{calc,patterns,decorations.markings}
\usetikzlibrary{positioning,snakes}

\newcommand{\myfig}[1]{%
\coordinate (g) at (0,0);
\coordinate (topspring) at (0,-1.25cm);
\coordinate (bottomspring) at (0,{#1}); %%changing the values (5cm) here will compress or expand the spring
\coordinate (pt2) at ($(bottomspring) + (0,-.5cm)$); %% this is relative.

\node [platform,anchor=south] at (g)  {};
\draw[very thick] (-1,0) -- (1,0);
\draw [dampener](topspring)--(g) node[draw=none,pos=.52,right=0.25cm] {$b$};
\draw [spring] (bottomspring) -- (topspring) node[draw=none,pos=.5,right=.25cm] (spring){$k$};
\draw [thick] (bottomspring) -- (pt2.north);
\draw ($(pt2) + (0,-.2)$) circle (.2cm) node[draw=none,inner sep = 0]{$m$};
}
\begin{document}

\begin{tikzpicture}[yshift=2cm,every node/.style={draw,outer sep=0pt,thick}]

\tikzstyle{spring}=[thick,decorate,decoration={aspect=0.5, segment length=1mm, amplitude=2mm,coil}]
\tikzstyle{dampener}=[thick,decoration={markings,
  mark connection node=dmp,
  mark=at position 0.5 with
  {
    \node (dmp) [thick,inner sep=0pt,transform shape,rotate=-90,minimum width=15pt,minimum height=3pt,draw=none] {};
    \draw [thick] ($(dmp.north east)+(2pt,0)$) -- (dmp.south east) -- (dmp.south west) -- ($(dmp.north west)+(2pt,0)$);
    \draw [thick] ($(dmp.north)+(0,-5pt)$) -- ($(dmp.north)+(0,5pt)$);
  }
}, decorate]
\tikzstyle{platform}=[fill,pattern=north east lines,draw=none,minimum width=2cm,minimum height=0.3cm]

\begin{scope}[xshift=-2.5cm]
\myfig{-2cm}
\end{scope}

\myfig{-4cm}

\begin{scope}[xshift=2.5cm]
\myfig{-6cm}
\end{scope}

\begin{scope}[xshift=5cm]
\myfig{-8cm}
\end{scope}

\begin{scope}[xshift=7.5cm]
\myfig{-10cm}
\end{scope}

\begin{scope}[xshift=10cm]
\myfig{-12cm}
\end{scope}

\end{tikzpicture}

\end{document}

enter image description here

Edit: Some more beautification to incorporate the physical meaning of the present case:

\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{dampener}=[thick,decoration={markings,
  mark connection node=dmp,
  mark=at position 0.5 with
  {
    \node (dmp) [thick,inner sep=0pt,transform shape,rotate=-90,minimum width=15pt,minimum height=3pt,draw=none] {};
    \draw [thick] ($(dmp.north east)+(2pt,0)$) -- (dmp.south east) -- (dmp.south west) -- ($(dmp.north west)+(2pt,0)$);
    \draw [thick] ($(dmp.north)+(0,-5pt)$) -- ($(dmp.north)+(0,5pt)$);
  }
}, decorate]
\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,-1.25cm);
\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.

\node [platform,anchor=south] at (g)  {};
\draw[very thick] (-1,0) -- (1,0);
\draw [dampener](topspring)--(g) node[draw=none,pos=.52,right=0.25cm] {$b$};
\draw [spring] (bottomspring) -- (topspring) node[draw=none,pos=.5,right=.25cm] (spring){$k$};
\draw [thick] (bottomspring) -- (pt2.north);
\draw ($(pt2) + (0,#3)$) circle (#3) node[draw=none,inner sep = 0,scale=#4]{$m$};
}
\begin{document}

\begin{tikzpicture}[yshift=0cm,every node/.style={draw,outer sep=0pt,thick}]
\begin{scope}[xshift=-2.5cm]
\myfig{1mm}{-3cm}{-0.2cm}{1}
\end{scope}

\myfig{1.5mm}{-4.5cm}{-0.25cm}{1.1}

\begin{scope}[xshift=2.5cm]
\myfig{2mm}{-6cm}{-0.3cm}{1.2}
\end{scope}

\begin{scope}[xshift=5cm]
\myfig{2.5mm}{-8cm}{-0.35cm}{1.3}
\end{scope}

\begin{scope}[xshift=7.5cm]
\myfig{3mm}{-10cm}{-0.4cm}{1.4}
\end{scope}

\begin{scope}[xshift=10cm]
\myfig{3.5mm}{-12cm}{-0.45cm}{1.5}
\end{scope}

\end{tikzpicture}

\end{document}

enter image description here

Here \myfig takes four arguments. First one is segment length, second the stretching position of the spring (the coordinate bottomspring), third is radius of the circle enclosing m and the last is the font size for m. Hence one has to use \myfig{3mm}{-10cm}{-0.4cm}{1.4} with appropriate values as per the need.