Draw coil with varying compression

decorationspst-coiltikz-pgf

I am trying to a draw a coil with varying compression, along the lines of the image below (not mine).enter image description here

I considered just drawing two different coils with different compressions next to each other

\usetikzlibrary{decorations.pathmorphing,patterns}
\begin{tikzpicture}
\draw[decoration={aspect=0.3, segment length=4mm, amplitude=4mm,coil},decorate] (0,0) 
-- (2,0); 
\draw[decoration={aspect=0.3, segment length=2mm, amplitude=4mm,coil},decorate] (0,0) 
-- (-2,0); 
\end{tikzpicture}

which yields this: enter image description here

but the transition from compressed coil to non compressed coil is not very smooth. Does anyone know how to work around this?

Best Answer

Just a wild suggestion (a bit on the side of a lucky shot, because I have no time to find out the correct numbers, but...).

You can draw a constant-size coil but distort the x-coordinate with a cosine function.

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,patterns}
\usepgfmodule{nonlineartransformations}
\makeatletter
\def\mytransformation{%
    \pgfmathsetmacro{\myX}{\pgf@x + 2*cos(\pgf@x*8)}%
    % no need to change y --- let's comment this out
    % \pgfmathsetmacro{\myY}{\pgf@y}
    \setlength{\pgf@x}{\myX pt}%
    % \setlength{\pgf@y}{\myY pt}
}
\makeatother\begin{document}
\begin{tikzpicture}
    \begin{scope}
        \pgftransformnonlinear{\mytransformation}
        \draw[decoration={aspect=0.3, segment length=2mm, amplitude=4mm,coil},decorate]
            (0,0) -- (10,0);
    \end{scope}
\end{tikzpicture}
\end{document}

cosine-modulated coil