Appending a line (of fixed length) to a spring drawn with tikz coil

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}


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.

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

-
Instead of drawing from (a) to the clamped part just use the decoration starting from a little higher from (a) and end the drawing below the clamp. Would that solve your problem? – percusse May 7 '12 at 20:32
Generally: If you only want to mark a position, don't use a \node, use \coordinate (or \node with inner sep=0pt), otherwise you get unwanted whitespace. In your drawing, what's attachment supposed to be (as opposed to pt2)? – Jake May 7 '12 at 22:38
@Jake: Thanks for the \node vs \coordinate tip; that looks like what was causing the unwanted white space. Here, "attachment" is supposed to be the coordinate (!) joining the linear piece to the top of the circular mass. "pt2" is the center of the circular mass. – JohnD May 7 '12 at 23:47

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}


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}


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}


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.

-
Yes, thank you! – JohnD May 7 '12 at 23:56
One last issue, which I'm sure is due my inexperience with relative positioning. As I change the \coordinate for (bottomspring), it seems the mass position remains constant while the platform moves. I would like the platform to be constant and the mass to move as I adjust that (bottomspring) coordinate. What determines which of these is constant (anchored) vs relative? – JohnD May 8 '12 at 2:30
@texasAUtiger: \coordinate (pt2) at ($(bottomspring) + (0,-.5cm)$); is relative to \coordinate (bottomspring) at (0,-4cm);. i.e., if you change (-4cm) to say, (-8cm), then the coordinate (pt2)` will shift accordingly. (Platform is constant at (0,0) (coordinate g) always). Change -4cm and see the effect. – Harish Kumar May 8 '12 at 4:14
That's very nice! – percusse May 8 '12 at 15:37
@texasAUtiger: How is the edited (improved) code? Pl. let me know. – Harish Kumar May 9 '12 at 5:57