[Tex/LaTex] Help on decorations: how to draw a spiral coiled tube in TikZ

decorationstikz-pgf

Hi everybody I was trying to draw a spiral coiled tube

enter image description here

with TikZ decoration library. This is what i got so far:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations}


\pgfdeclaredecoration{example}{initial} {
\state{initial}[width=5mm, next state=mynext] {
%
    \pgfpathmoveto{\pgfpoint{0cm   }{1cm   }}
    \pgfpathquadraticcurveto{\pgfpoint{.6cm   }{.5cm   }}{\pgfpoint{0cm   }{0cm   }}
    \pgfpathquadraticcurveto{\pgfpoint{.25cm   }{.1cm   }}{\pgfpoint{.5cm   }{0cm   }}
    \pgfpathquadraticcurveto{\pgfpoint{1.2cm   }{.5cm   }}{\pgfpoint{.5cm   }{1cm   }}
    \pgfpathquadraticcurveto{\pgfpoint{.25cm   }{.9cm   }}{\pgfpoint{0cm}{1cm}}
    \pgfpathmoveto{\pgfpoint{.5cm   }{0cm   }}
    } 
\state{mynext}[width=5mm]{
    \pgfpathquadraticcurveto{\pgfpoint{.25cm   }{.1cm   }}{\pgfpoint{.5cm   }{0cm   }}\pgfpathquadraticcurveto{\pgfpoint{1.2cm   }{.5cm   }}{\pgfpoint{.5cm   }{1cm   }}
    \pgfpathquadraticcurveto{\pgfpoint{.25cm   }{.9cm   }}{\pgfpoint{0cm}{1cm}}
    \pgfpathmoveto{\pgfpoint{.5cm   }{0cm   }}
}
\state{final} {
    \pgfpathlineto{\pgfpointdecoratedpathlast}
    }
}


\begin{document}

\tikz[decoration=example] {
    \draw [decorate]    (0,0) -- (3,0); 
    \draw [blue!20!,decorate] (0,0) to [out=45,in=135] (3,0);
    \draw [red,decorate] (0,-5) to [out=30,in=100] (3,-5);
}
\end{document}

enter image description here

This results in a good one for straight lines, but with some bending there is a gap between the segments. So this is what i expected since i'am not able to hand over two coordinates to the next repetition.

I tried to compute the end point (upper corner) by storing \pgfdecoratedangle and transforming it back to the last segments angel but \pgfdecoratedangle seems to be empty.

So how can i connect those segments properly and/or
how could one draw such a tube ?

Update: another approach with an existing decorations:

\documentclass[border=1mm]{standalone}
% based on http://tex.stackexchange.com/questions/31707/how-to-raise-a-generic-curve-problem-with-pgfdeclaredecoration
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{decorations.pathmorphing}

\tikzset{forcedist/.style={decorate, decoration={markings,
mark=between positions 0 and 1 step 0.0999 with {\draw[thick] (0,-#1) to[bend left] (0,#1); }}}}

\begin{document}

 \begin{tikzpicture}
  \draw [double distance = 1cm,thick, double=red, decoration=snake]  (0,0) .. controls++(1,1) and (6,-3).. (7,0);
  % those lines should be wavy
  \draw [forcedist=5mm]  (0,0) .. controls++(1,1) and (6,-3).. (7,0);
  \draw [double distance = 5mm,thick, double=red]  (0,3) --++(8,0);
  \draw [forcedist=2.5mm ]  (0,3)--++(8,0);
  \end{tikzpicture}

\end{document}

So this looks quite encouraging, although i'm not able to make those douled lines wavy so that there is a impression of a thicker spiral. The latter results in enter image description here

Best Answer

Here's a small modification of your original code that remembers the key point on the previous computation (the outer-forward corner). It does this by defining a coordinate node at that point. Since there are a lot of transformations involved, this seemed the simplest method of achieving that end.

\documentclass{article}
%\url{http://tex.stackexchange.com/q/64074/86}
\usepackage{tikz}
\usetikzlibrary{decorations}


\pgfdeclaredecoration{example}{initial} {
\state{initial}[width=5mm, next state=mynext] {
%
    \pgfpathmoveto{\pgfpoint{0cm   }{1cm   }}
    \pgfpathquadraticcurveto{\pgfpoint{.6cm   }{.5cm  
}}{\pgfpoint{0cm   }{0cm   }}
    \pgfpathquadraticcurveto{\pgfpoint{.25cm   }{.1cm  
}}{\pgfpoint{.5cm   }{0cm   }}
    \pgfpathquadraticcurveto{\pgfpoint{1.2cm   }{.5cm  
}}{\pgfpoint{.5cm   }{1cm   }}
    \pgfpathquadraticcurveto{\pgfpoint{.25cm   }{.9cm  
}}{\pgfpoint{0cm}{1cm}}
    \pgfpathmoveto{\pgfpoint{.5cm   }{0cm   }}
    \pgfcoordinate{coil-tmp}{\pgfpoint{.5cm}{1cm}}
    } 
\state{mynext}[width=5mm]{
    \pgfpathquadraticcurveto{\pgfpoint{.25cm   }{.1cm  
}}{\pgfpoint{.5cm   }{0cm   }}\pgfpathquadraticcurveto{\pgfpoint{1.2cm  
}{.5cm   }}{\pgfpoint{.5cm   }{1cm   }}
    \pgfpathquadraticcurveto{\pgfpoint{.25cm   }{.9cm  
}}{\pgfpointanchor{coil-tmp}{center}}
    \pgfpathmoveto{\pgfpoint{.5cm   }{0cm   }}
    \pgfcoordinate{coil-tmp}{\pgfpoint{.5cm}{1cm}}
}
\state{final} {
    \pgfpathlineto{\pgfpointdecoratedpathlast}
    }
}


\begin{document}

\tikz[decoration=example] {
    \draw [decorate]    (0,0) -- (3,0); 
    \draw [blue!20!,decorate] (0,0) to [out=45,in=135] (3,0);
    \draw [red,decorate] (0,-5) to [out=30,in=100] (3,-5);
}
\end{document}

Result:

Joined-up spiral

Related Question