5

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?

2 Answers 2

9

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

1
  • What a clever solution. Thank you! 🙏 May 31, 2022 at 7:25
2

Completing the Rmano answer I propose this code:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,patterns}
\usepgfmodule{nonlineartransformations}
\makeatletter
\def\mytransformation{%
\pgfmathsetmacro{\myX}{\pgf@x + 2*cos(\pgf@x*8)}
%\pgfmathsetmacro{\myY}{\pgf@y}
\setlength{\pgf@x}{\myX pt}
%\setlength{\pgf@y}{\myY pt}
}
\makeatother
\begin{document}
    \begin{tikzpicture}
        \begin{scope}
            \pgftransformnonlinear{\mytransformation}
            \draw[cyan,line width=1pt,decoration={aspect=0.3, segment length=2.5mm, amplitude=4mm,coil},decorate]
                (0,0) -- (10,0);
        \end{scope}
        \draw (0,-.5) -- (10,-.5);
         \foreach \i in {0,.1,...,10}
        \draw[line width=.2pt] (\i,-.5)--(\i,-.6);
        \foreach \i in {0.5,1.5,...,9.5}
        \draw[line width=.4pt] (\i,-.5)--(\i,-.7)  node[below] {\tiny $\i$};
            \foreach \i in {0,1,...,10}
                \draw[line width=.8pt] (\i,-.5)--(\i,-.8) node[below] {\footnotesize \bfseries $\i$};
        \node at (5,-1.3) () {\footnotesize cm};
        \draw[line width=.05pt]  (.57,.6)--(.57,-.4)
                    (2.09,.6)--(2.09,-.4)
                    (3.61,.6)--(3.61,-.4)
                    (5.13,.6)--(5.13,-.4)
                    (6.65,.6)--(6.65,-.4)
                    (8.17,.6)--(8.17,-.4)
                    (9.685,.6)--(9.685,-.4)
                    (.57,.6)--(9.685,.6);
       \foreach \i in {1.3,2.82,4.34,5.86,7.38,8.9}
            \node[fill=white] at (\i,.6) () {\tiny $\lambda=1.52$};
    \end{tikzpicture}
\end{document}

The output is:

enter image description here

where you can comment or delete the code for the ruler.

EDIT: Without the ruler

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,patterns}
\usepgfmodule{nonlineartransformations}
\makeatletter
\def\mytransformation{%
\pgfmathsetmacro{\myX}{\pgf@x + 2*cos(\pgf@x*8)}
%\pgfmathsetmacro{\myY}{\pgf@y}
\setlength{\pgf@x}{\myX pt}
%\setlength{\pgf@y}{\myY pt}
}
\makeatother
\begin{document}
    \begin{tikzpicture}
        \begin{scope}
            \pgftransformnonlinear{\mytransformation}
            \draw[cyan,line width=1pt,decoration={aspect=0.3, segment length=2.5mm, amplitude=4mm,coil},decorate]
                (0,0) -- (10,0);
        \end{scope}
%       \draw (0,-.5) -- (10,-.5);
%        \foreach \i in {0,.1,...,10}
%       \draw[line width=.2pt] (\i,-.5)--(\i,-.6);
%       \foreach \i in {0.5,1.5,...,9.5}
%       \draw[line width=.4pt] (\i,-.5)--(\i,-.7)  node[below] {\tiny $\i$};
%           \foreach \i in {0,1,...,10}
%               \draw[line width=.8pt] (\i,-.5)--(\i,-.8) node[below] {\footnotesize \bfseries $\i$};
%       \node at (5,-1.3) () {\footnotesize cm};
        \draw[line width=.05pt]  (.57,.6)--(.57,.3)
                    (2.09,.6)--(2.09,.3)
                    (3.61,.6)--(3.61,.3)
                    (5.13,.6)--(5.13,.3)
                    (6.65,.6)--(6.65,.3)
                    (8.17,.6)--(8.17,.3)
                    (9.685,.6)--(9.685,.3)
                    (.57,.6)--(9.685,.6);
       \foreach \i in {1.3,2.82,4.34,5.86,7.38,8.9}
            \node[fill=white] at (\i,.6) () {\tiny $\!\lambda{=}1.52\!$};
    \end{tikzpicture}
\end{document}

enter image description here

... and some little changes!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .