65

Currently I am using the following path style to draw springs in TikZ

\tikzstyle{spring}=[thick,decorate,decoration={zigzag,pre length=0.1cm,post
  length=0.1cm,segment length=6}]

for example

\draw[spring] (0,0) -- (0,1);

However is it possible to make the springs look like in the following picture:

Springs

Is there any nice way to incorporate the possibility of drawing the spring stretched or compressed like in the picture above?

73
+600

You can use the coil decoration for the effect. With segment length you can mimic the compression amount.

\documentclass{article} 
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,patterns}
\begin{document}
\begin{tikzpicture}
\node[circle,fill=blue,inner sep=2.5mm] (a) at (0,0) {};
\node[circle,fill=blue,inner sep=2.5mm] (b) at (2,2) {};
\draw[decoration={aspect=0.3, segment length=3mm, amplitude=3mm,coil},decorate] (0,5) -- (a); 
\draw[decoration={aspect=0.3, segment length=1.5mm, amplitude=3mm,coil},decorate] (2,5) -- (b); 
\fill [pattern = north east lines] (-1,5) rectangle (3,5.2);
\draw[thick] (-1,5) -- (3,5);
\end{tikzpicture}
\end{document}

enter image description here

61

Only for PSTricks' fans:

enter image description here

\documentclass{article}
\usepackage{pst-coil}

\usepackage[active,tightpage]{preview}
\PreviewEnvironment{pspicture}
\PreviewBorder=12pt

\psset
{
    coilarm=0.25,
    coilwidth=0.3
}

\begin{document}

\begin{pspicture}[showgrid](-1,-2)(9,5)
    % Ceiling
    \psframe
    [
        fillstyle=vlines,
        hatchsep=2pt,
        hatchwidth=0.5\pslinewidth,
        hatchcolor=gray,
        hatchangle=45,
        linestyle=none
    ](0,4)(8,4.25)

    % Spring without box
    \uput[90](1,4.25){A}
    \pszigzag[coilheight=0.3](1,4)(1,2)


    % Spring stretched due to box weight
    \uput[90](3,4.25){B}
    \pszigzag[coilheight=0.5](3,4)(3,1)
    \psframe*[origin={3,1}](-0.5,0)(0.5,-1)

    % Spring stretched by external force
    \uput[90](5,4.25){C}
    \pszigzag[coilheight=0.8](5,4)(5,-0.5)
    \psframe*[origin={5,-0.5}](-0.5,0)(0.5,-1)

    % Position at any time
    \uput[90](7,4.25){D}
    \pszigzag[coilheight=0.7](7,4)(7,0)
    \psframe*[origin={7,0}](-0.5,0)(0.5,-1)

    % Reference lines
    \psset{linecolor=red,linestyle=dashed}
    \psline(0,2)(8,2)
    \psline(0,1)(3,1)
    \psline(0,-0.5)(5,-0.5)
    \psline(7,0)(8,0)

    % Labels
    \psset{linecolor=blue,linestyle=solid}
    \psline{<->}(-0.1,2)(-0.1,1)
    \uput[180](-0.1,1.5){$\Delta y$}
    \psline{<->}(-0.1,1)(-0.1,-0.5)
    \uput[180](-0.1,0.25){$A$}
    \psline{|->}(8.1,2)(8.1,0)
    \uput[0](8.1,1){$y$}
    \rput[br](8.5,-1.75){\tiny created by Forgiver \copyright 2012}
    \end{pspicture}

\end{document}

If we set gridstyle to false, we will get the cleaner result as follows:

enter image description here

Note that I keep the number of winding equal for each spring to make them more realistic.

Edit:

enter image description here

\documentclass[border=0pt,pstricks]{standalone}
\usepackage{pst-coil,pstricks-add}
\usepackage[nomessages]{fp}

\FPset\CoilArm{0.25}
\FPset\CoilWidth{0.3}
\FPeval\CoilTurn{round(50/3:3)}
\FPeval\DeltaY{0.5}
\FPeval\Amp{1.5}
\FPeval\FPS{25}
\FPeval\Vx{2}% propagation speed
\FPeval\Period{1}% second

\psset
{
    coilarm=\CoilArm,
    coilwidth=\CoilWidth,
}


\newcommand\System[4][0]{% #1: frame, #2: x, #3: y, #4: label
    \uput[90](#2,4.25){#4}
    \FPeval\CoilHeight{round((4-(#3)-2*CoilArm)/(CoilWidth*CoilTurn):3)}
    \pszigzag[coilheight=\CoilHeight,linejoin=2](#2,4)(#2,#3)
    \ifnum#1=1
        \bgroup
            \psset{origin={#2,#3}}
            \psframe[dimen=inner,fillstyle=solid,fillcolor=black](-0.5,0)(0.5,-1)
            \psdot[linecolor=yellow](0,-0.5)
        \egroup
    \fi
}

\begin{document}
\FPeval\DeltaTime{round(1/\FPS:2)}
\FPeval\TotalFrame{round(\FPS*\Period:0)}
\multido{\n=0.00+\DeltaTime}{\TotalFrame}{%
\begin{pspicture*}[showgrid=false](-1.5,-2)(3.5,5)
    % Ceiling
    \psframe
    [
        fillstyle=vlines,
        hatchsep=2pt,
        hatchwidth=0.5\pslinewidth,
        hatchcolor=gray,
        hatchangle=45,
        %linestyle=none
    ](0,4)(2,4.25)
    % Spring without box
    \FPeval\Y{round(-DeltaY-Amp*cos(2*pi*\n/Period)+2:3)}
    \System[1]{1}{\Y}{A}
    \psplot[algebraic,linecolor=red,plotpoints=1000]
        {-1.5}{3.5}{-\DeltaY-\Amp*cos((2*\psPi/\Period)*((-\Vx*\n+x-1)/\Vx))+2-0.5}
\end{pspicture*}}

\end{document}
  • The physical scenario for this diagram is as follows: (A) a massless spring is hanged on the ceiling. (B) a box is attached to the spring's lower end and very very slowly released until it stretches the spring \Delta y. (C) An external force pulls the box until it stops at a distance of A. At t=0 the external force is removed. (D) The position of the spring lower end at any time t. The solution is y(t)=\Delta y + A\cos(\omega t). The solution will be different if we change the condition in (B): we release the box to fall freely. – kiss my armpit Jun 4 '12 at 9:30
  • All the stuff posted to this site is automatically licensed under a Creative Commons Attribution Required license (blog.stackoverflow.com/2010/08/defending-attribution-required). It's not necessary to add a copyright notice to your posts or images, you own the copyright automatically. – Jake Jun 4 '12 at 9:38
  • Please ignore this copyright label because it's just for fun. – kiss my armpit Jun 4 '12 at 9:51
  • 2
    Nice animation! If you set the corners of the lines on the spring to be bevelled, you won't get the artifact of the corners getting shorter when the mass is near the top. In TikZ, you'd set line join=bevel, I'm sure there's something similar for PSTricks. – Jake Aug 11 '12 at 8:49
  • 2
    @Jake: Thanks for the comment. I have updated the animation. – kiss my armpit Aug 11 '12 at 9:13
46

This is mainly modified from this answer.

\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{platform}=[fill,pattern=north east lines,draw=none,minimum width=2cm,minimum height=0.3cm]
%
\coordinate (g) at (0,0);
\coordinate (topspring) at (0,-1cm);
\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.
\coordinate (pt3) at ($(pt2) + (0,#3)$); %% this is relative.
%
\node [platform,anchor=south] at (g)  {};
\draw[very thick] (-1,0) -- (1,0);
\draw [thick](topspring)--(g);
\draw [spring] (bottomspring) -- (topspring);
\draw [thick] (bottomspring) -- (pt2.north);
\draw [fill=black] (pt3) circle (#3) node[draw=none,inner sep = 0,scale=#4,text=white]{$m$};
}
\begin{document}
%
\begin{tikzpicture}[yshift=0cm,every node/.style={draw,outer sep=0pt,thick}]
\begin{scope}[xshift=-2.5cm]
\myfig{1mm}{-3cm}{-0.1cm}{0}
\node[draw=none,right=.1cm] at (pt3)(a) {$A$};
\draw [thick,dashed] ($(pt3) + (0.8,0)$) -- +(1.2,0)node[draw=none,inner sep = 0,pos=.5](a1){};
\draw [thick,dashed,] ($(pt3) + (3.3,0)$) -- +(1.2,0)node[draw=none,inner sep = 0,pos=.5](a2){};
\end{scope}
%
\begin{scope}
\myfig{2mm}{-4.8cm}{-0.35cm}{1.5}
\node[draw=none,right=.25cm] at (pt3)(b) {$B$};
\draw [thick,dashed] ($(pt3) + (-1.7,0)$) -- +(1.2,0)node[draw=none,inner sep = 0,pos=.5](b1){};
\draw[thick,latex-latex] (a1) -- (b1)node[draw=none,inner sep = 0,pos=.5,right=0.1cm]{$\delta l_1$};
\end{scope}
%
\begin{scope}[xshift=2.5cm]
\myfig{3mm}{-6.6cm}{-0.4cm}{1.8}
\node[draw=none,right=.3cm] at (pt3)(b) {$C$};
\draw [thick,dashed] ($(pt3) + (-1.7,0)$) -- +(1.2,0)node[draw=none,inner sep = 0,pos=.5](c1){};
\draw[thick,latex-latex] (a2) -- (c1)node[draw=none,inner sep = 0,pos=.5,right=0.1cm]{$\delta l_2$};
\end{scope}
%
\end{tikzpicture}
%
\end{document}

enter image description here

Kindly excuse me if the code is cluttered a bit. It can be simplified further by inserting extra lines inside the scope, into the macro \myfig itself.

Let us have some fun in this episode of having some free time and getting bored:

\documentclass{article}
\usepackage{filecontents}%
\begin{filecontents*}{beam.tex}
\documentclass[tikz]{standalone}
\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{platform}=[fill,pattern=north east lines,draw=none,minimum width=2cm,minimum height=0.3cm]
%
\coordinate (g) at (0,0);
\coordinate (topspring) at (0,-.2cm);
\coordinate (bottomspring) at (0,{#2}); %%changing the values (5cm) here will compress or expand the spring
\coordinate (pt2) at ($(bottomspring) + (0,-1cm)$); %% this is relative.
\coordinate (pt3) at ($(pt2) + (0,#3)$); %% this is relative.
%
\node [platform,anchor=south] at (g)  {};
\draw[very thick] (-1,0) -- (1,0);
\draw [thick](topspring)--(g);
\draw [spring] (bottomspring) -- (topspring);
\draw [thick] (bottomspring) -- (pt2.north);
\draw [fill=black] (pt3) circle (#3) node[draw=none,inner sep = 0,scale=#4,text=white]{$m$};
}
\begin{document}
%
\foreach \x/\y/\r/\s in {1/19/1/.3,1.4/24.8/2/.5,1.8/30.5/3/.7,2.2/36.3/4/.9,2.6/42/5/1.1,3/47.8/6/1.4,3.4/53.6/7/1.8,3.8/59.4/8/2.3,
4.2/65.2/9/2.7,4.4/68/10/3.2}{
\begin{tikzpicture}[scale=1,yshift=0cm,every node/.style={draw,outer sep=0pt,thick}]
\myfig{\x mm}{-\y mm}{-\r mm}{\s}
\path (-1,0) rectangle (1,-10);
\end{tikzpicture}
}
\end{document}
\end{filecontents*}
%create the beam.pdf.
\immediate\write18{pdflatex beam}
% convert to GIF animation
\immediate\write18{convert -delay 10 -loop 0 -density 200 -alpha remove beam.pdf beam.gif}

\begin{document}
 This is empty
\end{document}

enter image description here

  • 5
    To be more realistic, you need to keep the number of winding equal for each coil or spring. – kiss my armpit Jun 4 '12 at 3:17
  • 1
    @AdorableCreature: You changed your name again!. Now it (the spring) is more realistic. You may start counting ;-) – user11232 Dec 25 '12 at 12:13
  • It is a very realistic animation. – kiss my armpit Dec 25 '12 at 14:36
  • But it is difficult to implement this animation in a lab. :-) – kiss my armpit Dec 25 '12 at 14:52
33

Here a variation from the answer of Harish Kumar. First it's preferable to avoid tikzstyle inside the macro \myfig. I keep only springstyle but inside a scope to work with a local style. I change the options for every node. Now thick is set up for every path, except when there is a local change. I change the way to draw the lines between the figs.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{patterns,decorations.pathmorphing}

\newcommand{\myfig}[6]{%
\begin{scope}[xshift=#6,
             spring/.style = {decorate,
                              decoration = {aspect         = 0.5, 
                                            segment length = #1,
                                            amplitude      = 2mm,
                                            coil}}] 

\path (0,0)                            coordinate (g) 
      (0,-1cm)                         coordinate (topspring) 
      (0,#2)                           coordinate (bottomspring) 
      (bottomspring) ++(0,-.5cm)       coordinate (pt2)
                      +(0cm,-#3)       coordinate (pt3)
                      +(1.25cm,-#3)    coordinate (#5 pt3);

 \node [platform,
        anchor = south] at (g)  {};
 \draw [very thick]    (-1,0)         -- (1,0);
 \draw                (topspring)     -- (g)
                      (bottomspring)  -- (pt2.north);
 \draw [spring]       (bottomspring)  -- (topspring);
 \draw [fill=black] (pt3) circle (#3) 
                          node[inner sep = 0,
                               scale     = #4,
                               text      = white]{$m$};
 \node[right=1.5*#3] at (pt3) {#5} ;
 \end{scope}
}

\begin{document}
\begin{tikzpicture}[thick,
                    every node/.style = {draw      = none,
                                         inner sep = 0pt,
                                         outer sep = 0pt},
                    platform/.style   = {fill, 
                                         pattern = north east lines,
                                         minimum width  = 2cm,
                                         minimum height  =0.3cm}]
 \myfig{1mm}{-4cm}{0.1cm}{0}{A}{-2.5cm}
 \myfig{3mm}{-6cm}{0.35cm}{1.5}{B}{0cm}
 \myfig{3mm}{-10cm}{0.4cm}{1.8}{C}{2.5cm} 

\draw[dashed]  (A pt3)  +(-0.6,0)     --  +(0.6,0)
                        +(-0.6+2.5,0) -- coordinate (b1) +(0.6+2.5,0)
               (B pt3)  +(-0.6-2.5,0) -- coordinate (a2) +(0.6-2.5,0) 
               (C pt3)  +(-0.6-2.5,0) -- coordinate (b2) +(0.6-2.5,0) ;

\draw[latex-latex] (A pt3) -- node[right=0.1cm]{$\delta l_1$} (a2); 
\draw[latex-latex] (b1)    -- node[right=0.1cm]{$\delta l_2$} (b2);

\end{tikzpicture}
\end{document}

enter image description here

6

Just for fun: the stretching of the coil due to its own weight, i.e. the lower parts get stretched less than the upper parts. The bulk of the code is from this answer, the stretching effect is a simple nonlinear transformation, and its contribution to the code only a few lines.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{decorations.pathmorphing,patterns}
\usepgfmodule{nonlineartransformations}
\makeatletter
\def\nltrafo{%
\pgfmathsetmacro{\myy}{\pgf@y+0.003*\pgf@y*\pgf@y}%
\pgf@y=\myy pt}
\newif\ifcoil@closed
\pgfkeys{%
/pgf/decoration/.cd,
3d coil color/.store in=\TDCoilColor, 
3d coil color/.initial=black,
3d coil color=black,
3d coil width/.store in=\TDCoilWidth, 
3d coil width/.initial=0.4pt,
3d coil width=0.4pt,
3d coil dist/.store in=\TDCoilDist, 
3d coil dist/.initial=0.6pt,
3d coil dist=0.6pt,
3d coil opacity/.store in=\TDCoilOpacity, 
3d coil opacity/.initial=1,
3d coil opacity=1,
3d coil closed/.code=\coil@closedtrue
}

 % https://tex.stackexchange.com/a/219088/121799
\tikzset{get stroke color/.code={%
    \expandafter\global% Jump over, now we have \global
    \expandafter\let% Jump over now we have \global\let
    \expandafter\pgfsavedstrokecolor% Jump we have \global\let\pgf...
    \csname\string\color@pgfstrokecolor\endcsname% Finally expand this and put it at the end 
    },                                           % \global\let\pgf...{} in expanded form 
    restore stroke color/.code={\pgf@setstrokecolor#1},
}
\def\pgfpoint@onthreedcoil#1#2#3{%
  \pgf@x=#1\pgfdecorationsegmentamplitude%
  \pgf@x=\pgfdecorationsegmentaspect\pgf@x%
  \pgf@y=#2\pgfdecorationsegmentamplitude%
  \pgf@xa=0.083333333333\pgfdecorationsegmentlength%
  \advance\pgf@x by#3\pgf@xa%
  \advance\pgf@x by-\generaloffset pt%
}

% coil decoration
%
% Parameters: \pgfdecorationsegmentamplitude, \pgfdecorationsegmentlength,

\pgfdeclaredecoration{3d complete coil}{initial}
{ 
    \state{initial}[width=0.5*\pgfdecorationsegmentlength,
    next state=coil, persistent precomputation={% from https://tex.stackexchange.com/a/25689/121799
    \pgfmathsetmacro\matchinglength{\pgfdecoratedinputsegmentlength / int(\pgfdecoratedinputsegmentlength/\pgfdecorationsegmentlength)}
    \setlength{\pgfdecorationsegmentlength}{\matchinglength pt}
    \tikzset{get stroke color}
    \pgfmathsetmacro{\generaloffset}{\pgfdecorationsegmentlength}
    \pgfmathsetmacro{\initialoffset}{1.5*\pgfdecorationsegmentlength}
    \pgfmathsetmacro{\auxoffset}{2.5*\pgfdecorationsegmentlength}
  }]    { 
    % line in the back
    %
    \pgfsetstrokecolor{\TDCoilColor}
    \pgfsetfillcolor{\TDCoilColor}
    \pgfsetstrokeopacity{\TDCoilOpacity}
    \pgfsetlinewidth{\TDCoilWidth}     
    \ifcoil@closed
     \begingroup
      \def\generaloffset{\auxoffset}
      \pgfpathmoveto{\pgfpoint@onthreedcoil{1    }{ 1    }{15}}
      \pgfpathcurveto
      {\pgfpoint@onthreedcoil{1.555}{ 1    }{16}}
      {\pgfpoint@onthreedcoil{2    }{ 0.555}{17}}
      {\pgfpoint@onthreedcoil{2    }{ 0    }{18}}
      \pgfcoordinate{TD@coilast}{\pgfpoint@onthreedcoil{2    }{ 0    }{18}}
      \pgfcoordinate{TD@coilfirst}{\pgfpoint@onthreedcoil{1    }{ 1    }{15}}
      \pgfusepath{stroke} 
      \pgfsetstrokecolor{\TDCoilColor}
     \endgroup
    \fi
    \begingroup %
    \def\generaloffset{\initialoffset}
    \ifcoil@closed
     \pgfpathmoveto{\pgfpointanchor{TD@coilast}{center}}
    \else
     \pgfpathmoveto{\pgfpointorigin}
    \fi
    \pgfpathcurveto
    {\pgfpoint@onthreedcoil{2    }{-0.555}{7}}
    {\pgfpoint@onthreedcoil{1.555}{-1    }{8}}
    {\pgfpoint@onthreedcoil{1    }{-1    }{9}}
    \pgfusepath{stroke} 
    %
    % white background for front thick part
    %
    \pgfsetstrokeopacity{1}
    \pgfsetstrokecolor{white}
    \pgfsetfillcolor{white}
    \pgfsetlinewidth{1.5*\TDCoilWidth+1.5*\TDCoilDist}
    \pgfpathmoveto{\pgfpoint@onthreedcoil{1    }{-1    }{9}}
    % draw forward
    \pgfpathcurveto
    {\pgfpoint@onthreedcoil{0.445}{-1    }{10}}
    {\pgfpoint@onthreedcoil{0    }{-0.555}{11.25}}
    {\pgfpoint@onthreedcoil{0    }{ 0    }{12.5}}
    \pgfpathcurveto
    {\pgfpoint@onthreedcoil{0    }{ 0.555}{13.25}}
    {\pgfpoint@onthreedcoil{0.445}{ 1    }{14.25}}
    {\pgfpoint@onthreedcoil{1    }{ 1    }{15}}
    % draw the curve back
    \pgfpathcurveto
    {\pgfpoint@onthreedcoil{0.445}{ 1    }{14}}
    {\pgfpoint@onthreedcoil{0    }{ 0.555}{12.75}}
    {\pgfpoint@onthreedcoil{0    }{ 0    }{11.5}}
    \pgfpathcurveto
    {\pgfpoint@onthreedcoil{0    }{-0.555}{10.75}}
    {\pgfpoint@onthreedcoil{0.445}{-1    }{10}}
    {\pgfpoint@onthreedcoil{1    }{-1    }{9}}
    \pgfusepath{stroke,fill} 
    % 
    % draw the thick foreground path
    %
    \pgfsetstrokecolor{\TDCoilColor}
    \pgfsetfillcolor{\TDCoilColor}
    \pgfsetstrokeopacity{\TDCoilOpacity}
    \pgfpathmoveto{\pgfpoint@onthreedcoil{1    }{ 1    }{3}}
    \pgfsetlinewidth{\TDCoilWidth} 
    % forward shifted +
    \pgfpathmoveto{\pgfpoint@onthreedcoil{1    }{-1    }{9}}
    \pgfpathcurveto
    {\pgfpoint@onthreedcoil{0.445}{-1    }{10}}
    {\pgfpoint@onthreedcoil{0    }{-0.555}{11.25}}
    {\pgfpoint@onthreedcoil{0    }{ 0    }{12.5}}
    \pgfpathcurveto
    {\pgfpoint@onthreedcoil{0    }{ 0.555}{13.25}}
    {\pgfpoint@onthreedcoil{0.445}{ 1    }{14.25}}
    {\pgfpoint@onthreedcoil{1    }{ 1    }{15}}
    % draw the curve back shfted -
    \pgfpathcurveto
    {\pgfpoint@onthreedcoil{0.445}{ 1    }{14}}
    {\pgfpoint@onthreedcoil{0    }{ 0.555}{12.75}}
    {\pgfpoint@onthreedcoil{0    }{ 0    }{11.5}}
    \pgfpathcurveto
    {\pgfpoint@onthreedcoil{0    }{-0.555}{10.75}}
    {\pgfpoint@onthreedcoil{0.445}{-1    }{10}}
    {\pgfpoint@onthreedcoil{1    }{-1    }{9}}
    \pgfusepath{stroke,fill} 
    \pgfpathmoveto{\pgfpoint@onthreedcoil{1    }{ 1    }{15}}
    \pgfpathcurveto
    {\pgfpoint@onthreedcoil{1.555}{ 1    }{16}}
    {\pgfpoint@onthreedcoil{2    }{ 0.555}{17}}
    {\pgfpoint@onthreedcoil{2    }{ 0    }{18}}
    \pgfcoordinate{TD@coilast}{\pgfpoint@onthreedcoil{2    }{ 0    }{18}} 
    \pgfusepath{stroke}     
    \endgroup
  }
  \state{coil}[switch if less than=%
    1.9*\pgfdecorationsegmentlength  to last,
               width=+\pgfdecorationsegmentlength]
    { % line in the back
    %
    \pgfsetstrokecolor{\TDCoilColor}
    \pgfsetfillcolor{\TDCoilColor}
    \pgfsetstrokeopacity{\TDCoilOpacity}
    \pgfpathmoveto{\pgfpointanchor{TD@coilast}{center}}
    \pgfsetlinewidth{\TDCoilWidth} 
    \pgfpathcurveto
    {\pgfpoint@onthreedcoil{2    }{-0.555}{7}}
    {\pgfpoint@onthreedcoil{1.555}{-1    }{8}}
    {\pgfpoint@onthreedcoil{1    }{-1    }{9}}
    \pgfusepath{stroke} 
    %
    % white background for front thick part
    %
    \pgfsetstrokeopacity{1}
    \pgfsetstrokecolor{white}
    \pgfsetfillcolor{white}
    \pgfsetlinewidth{1.5*\TDCoilWidth+1.5*\TDCoilDist}
    \pgfpathmoveto{\pgfpoint@onthreedcoil{1    }{ 1    }{3}}
    \pgfpathmoveto{\pgfpoint@onthreedcoil{1    }{-1    }{9}}
    % draw forward
    \pgfpathcurveto
    {\pgfpoint@onthreedcoil{0.445}{-1    }{10}}
    {\pgfpoint@onthreedcoil{0    }{-0.555}{11.25}}
    {\pgfpoint@onthreedcoil{0    }{ 0    }{12.5}}
    \pgfpathcurveto
    {\pgfpoint@onthreedcoil{0    }{ 0.555}{13.25}}
    {\pgfpoint@onthreedcoil{0.445}{ 1    }{14.25}}
    {\pgfpoint@onthreedcoil{1    }{ 1    }{15}}
    % draw the curve back
    \pgfpathcurveto
    {\pgfpoint@onthreedcoil{0.445}{ 1    }{14}}
    {\pgfpoint@onthreedcoil{0    }{ 0.555}{12.75}}
    {\pgfpoint@onthreedcoil{0    }{ 0    }{11.5}}
    \pgfpathcurveto
    {\pgfpoint@onthreedcoil{0    }{-0.555}{10.75}}
    {\pgfpoint@onthreedcoil{0.445}{-1    }{10}}
    {\pgfpoint@onthreedcoil{1    }{-1    }{9}}
    \pgfusepath{stroke,fill} 
    % 
    % draw the thick foreground path
    %
    \pgfsetstrokecolor{\TDCoilColor}
    \pgfsetfillcolor{\TDCoilColor}
    \pgfsetstrokeopacity{\TDCoilOpacity}
    \pgfpathmoveto{\pgfpoint@onthreedcoil{1    }{ 1    }{3}}
    \pgfsetlinewidth{\TDCoilWidth} 
    % forward shifted +
    \pgfpathmoveto{\pgfpoint@onthreedcoil{1    }{-1    }{9}}
    \pgfpathcurveto
    {\pgfpoint@onthreedcoil{0.445}{-1    }{10}}
    {\pgfpoint@onthreedcoil{0    }{-0.555}{11.25}}
    {\pgfpoint@onthreedcoil{0    }{ 0    }{12.5}}
    \pgfpathcurveto
    {\pgfpoint@onthreedcoil{0    }{ 0.555}{13.25}}
    {\pgfpoint@onthreedcoil{0.445}{ 1    }{14.25}}
    {\pgfpoint@onthreedcoil{1    }{ 1    }{15}}
    % draw the curve back shfted -
    \pgfpathcurveto
    {\pgfpoint@onthreedcoil{0.445}{ 1    }{14}}
    {\pgfpoint@onthreedcoil{0    }{ 0.555}{12.75}}
    {\pgfpoint@onthreedcoil{0    }{ 0    }{11.5}}
    \pgfpathcurveto
    {\pgfpoint@onthreedcoil{0    }{-0.555}{10.75}}
    {\pgfpoint@onthreedcoil{0.445}{-1    }{10}}
    {\pgfpoint@onthreedcoil{1    }{-1    }{9}}
    \pgfusepath{stroke,fill} 
    \pgfpathmoveto{\pgfpoint@onthreedcoil{1    }{ 1    }{15}}
    \pgfpathcurveto
    {\pgfpoint@onthreedcoil{1.555}{ 1    }{16}}
    {\pgfpoint@onthreedcoil{2    }{ 0.555}{17}}
    {\pgfpoint@onthreedcoil{2    }{ 0    }{18}}
    \pgfusepath{stroke} 
    \pgfcoordinate{TD@coilast}{\pgfpoint@onthreedcoil{2    }{ 0    }{18}} 
  }
  \state{last}[next state=final]
    { % line in the back
    %
    \pgfsetstrokecolor{\TDCoilColor}
    \pgfsetfillcolor{\TDCoilColor}
    \pgfsetstrokeopacity{\TDCoilOpacity}
    \pgfpathmoveto{\pgfpointanchor{TD@coilast}{center}}
    \pgfsetlinewidth{\TDCoilWidth} 
    \pgfpathcurveto
    {\pgfpoint@onthreedcoil{2    }{-0.555}{7}}
    {\pgfpoint@onthreedcoil{1.555}{-1    }{8}}
    {\pgfpoint@onthreedcoil{1    }{-1    }{9}}
    \pgfusepath{stroke} 
    % 
    % draw the thick foreground path
    %
    \ifcoil@closed %\pgfpointanchor{TD@coilfirst}{center}
     %
     % white background for front thick part
     %
     \pgfsetstrokeopacity{1}
     \pgfsetstrokecolor{white}
     \pgfsetfillcolor{white}
     \pgfsetlinewidth{1.5*\TDCoilWidth+1.5*\TDCoilDist}
     \pgfpathmoveto{\pgfpoint@onthreedcoil{1    }{ 1    }{3}}
     \pgfpathmoveto{\pgfpoint@onthreedcoil{1    }{-1    }{9}}
     % draw forward
     \pgfpathcurveto
     {\pgfpoint@onthreedcoil{0.445}{-1    }{10}}
     {\pgfpoint@onthreedcoil{0    }{-0.555}{11.25}}
     {\pgfpoint@onthreedcoil{0    }{ 0    }{12.5}}
     \pgfpathcurveto
     {\pgfpoint@onthreedcoil{0    }{ 0.555}{13.25}}
     {\pgfpoint@onthreedcoil{0.445}{ 1    }{14.25}}
     {\pgfpointanchor{TD@coilfirst}{center}}
     % draw the curve back
     \pgfpathcurveto
     {\pgfpoint@onthreedcoil{0.445}{ 1    }{14}}
     {\pgfpoint@onthreedcoil{0    }{ 0.555}{12.75}}
     {\pgfpoint@onthreedcoil{0    }{ 0    }{11.5}}
     \pgfpathcurveto
     {\pgfpoint@onthreedcoil{0    }{-0.555}{10.75}}
     {\pgfpoint@onthreedcoil{0.445}{-1    }{10}}
     {\pgfpoint@onthreedcoil{1    }{-1    }{9}}
     \pgfusepath{stroke,fill} 
     \pgfsetstrokecolor{\TDCoilColor}
     \pgfsetfillcolor{\TDCoilColor}
     \pgfsetstrokeopacity{\TDCoilOpacity}
     \pgfpathmoveto{\pgfpoint@onthreedcoil{1    }{ 1    }{3}}
     \pgfsetlinewidth{\TDCoilWidth} 
     % forward shifted +
     \pgfpathmoveto{\pgfpoint@onthreedcoil{1    }{-1    }{9}}
     \pgfpathcurveto
     {\pgfpoint@onthreedcoil{0.445}{-1    }{10}}
     {\pgfpoint@onthreedcoil{0    }{-0.555}{11.25}}
     {\pgfpoint@onthreedcoil{0    }{ 0    }{12.5}}
     \pgfpathcurveto
     {\pgfpoint@onthreedcoil{0    }{ 0.555}{13.25}}
     {\pgfpoint@onthreedcoil{0.445}{ 1    }{14.25}}
     {\pgfpointanchor{TD@coilfirst}{center}}
     % draw the curve back shifted 
     \pgfpathcurveto
     {\pgfpoint@onthreedcoil{0.445}{ 1    }{14}}
     {\pgfpoint@onthreedcoil{0    }{ 0.555}{12.75}}
     {\pgfpoint@onthreedcoil{0    }{ 0    }{11.5}}
     \pgfpathcurveto
     {\pgfpoint@onthreedcoil{0    }{-0.555}{10.75}}
     {\pgfpoint@onthreedcoil{0.445}{-1    }{10}}
     {\pgfpoint@onthreedcoil{1    }{-1    }{9}}
     \pgfusepath{stroke,fill} 
    \else
     %
     % white background for front thick part
     %
     \pgfsetstrokeopacity{1}
     \pgfsetstrokecolor{white}
     \pgfsetfillcolor{white}
     \pgfsetlinewidth{1.5*\TDCoilWidth+1.5*\TDCoilDist}
     \pgfpathmoveto{\pgfpoint@onthreedcoil{1    }{ 1    }{3}}
     \pgfpathmoveto{\pgfpoint@onthreedcoil{1    }{-1    }{9}}
     % draw forward
     \pgfpathcurveto
     {\pgfpoint@onthreedcoil{0.445}{-1    }{10}}
     {\pgfpoint@onthreedcoil{0    }{-0.555}{11.25}}
     {\pgfpoint@onthreedcoil{0    }{ 0    }{12.5}}
     \pgfpathcurveto
     {\pgfpoint@onthreedcoil{0    }{ 0.555}{13.25}}
     {\pgfpoint@onthreedcoil{0.445}{ 1    }{14.25}}
     {\pgfpoint@onthreedcoil{1    }{ 1    }{15}}
     % draw the curve back
     \pgfpathcurveto
     {\pgfpoint@onthreedcoil{0.445}{ 1    }{14}}
     {\pgfpoint@onthreedcoil{0    }{ 0.555}{12.75}}
     {\pgfpoint@onthreedcoil{0    }{ 0    }{11.5}}
     \pgfpathcurveto
     {\pgfpoint@onthreedcoil{0    }{-0.555}{10.75}}
     {\pgfpoint@onthreedcoil{0.445}{-1    }{10}}
     {\pgfpoint@onthreedcoil{1    }{-1    }{9}}
     \pgfusepath{stroke,fill} 
     \pgfsetstrokecolor{\TDCoilColor}
     \pgfsetfillcolor{\TDCoilColor}
     \pgfsetstrokeopacity{\TDCoilOpacity}
     \pgfpathmoveto{\pgfpoint@onthreedcoil{1    }{ 1    }{3}}
     \pgfsetlinewidth{\TDCoilWidth} 
     % forward shifted +
     \pgfpathmoveto{\pgfpoint@onthreedcoil{1    }{-1    }{9}}
     \pgfpathcurveto
     {\pgfpoint@onthreedcoil{0.445}{-1    }{10}}
     {\pgfpoint@onthreedcoil{0    }{-0.555}{11.25}}
     {\pgfpoint@onthreedcoil{0    }{ 0    }{12.5}}
     \pgfpathcurveto
     {\pgfpoint@onthreedcoil{0    }{ 0.555}{13.25}}
     {\pgfpoint@onthreedcoil{0.445}{ 1    }{14.25}}
     {\pgfpoint@onthreedcoil{1    }{ 1    }{15}}
     % draw the curve back shifted 
     \pgfpathcurveto
     {\pgfpoint@onthreedcoil{0.445}{ 1    }{14}}
     {\pgfpoint@onthreedcoil{0    }{ 0.555}{12.75}}
     {\pgfpoint@onthreedcoil{0    }{ 0    }{11.5}}
     \pgfpathcurveto
     {\pgfpoint@onthreedcoil{0    }{-0.555}{10.75}}
     {\pgfpoint@onthreedcoil{0.445}{-1    }{10}}
     {\pgfpoint@onthreedcoil{1    }{-1    }{9}}
     \pgfusepath{stroke,fill} 
    \fi
    \pgfpathmoveto{\pgfpoint@onthreedcoil{1    }{ 1    }{15}}
    \ifcoil@closed %TD@coilfirst
    \else
     \pgfpathcurveto
     {\pgfpoint@onthreedcoil{1.555}{ 1    }{16}}
     {\pgfpoint@onthreedcoil{2    }{ 0.555}{17}}
     {\pgfpoint@onthreedcoil{2    }{ 0    }{18}}
    \fi
    \pgfusepath{stroke} 
    %\pgfcoordinate{TD@coilast}{\pgfpoint@onthreedcoil{2    }{ 0    }{18}} 
  }
  \state{final}
  {
    \pgfpathmoveto{\pgfpointdecoratedpathlast}
    \tikzset{restore stroke color/.expand once=\pgfsavedstrokecolor}
  }
}
\makeatother

\begin{document}
\begin{tikzpicture}
 \begin{scope}[transform shape nonlinear=true,local bounding box=coil]
  \pgftransformnonlinear{\nltrafo}
  \draw[decoration={3d coil color=blue,aspect=0.2, segment length=1mm,
  amplitude=3mm,3d complete coil}, decorate] (0,0) -- (0,6);
 \end{scope}  
 \draw[blue] ([yshift=-4.5pt]coil.north) -- ++(0,0.5) coordinate(Y);
 \path[pattern=north west lines] (-2,0|-Y) rectangle ++(4,0.5);
 \draw (-2,0|-Y) -- ++(4,0);
\end{tikzpicture}
\end{document}

enter image description here

  • Very nice, the front part being thicker. – AlexG Oct 8 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.