7

I am trying to define a decorator that draws a screw between the two connected points. Until now this is what I have:

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

\pgfdeclaredecoration{screw}{initial}
    {
    \state{initial}[width=1.5pt,next state=midd]
        {
        % First line
        \pgfpathlineto{\pgfpoint{0.0pt}{3.0pt}}
        \pgfpathlineto{\pgfpoint{1.5pt}{1.5pt}}
        % Second line
        \pgfpathmoveto{\pgfpoint{0.0pt}{0.0pt}}
        \pgfpathlineto{\pgfpoint{0.0pt}{-3.0pt}}
        \pgfpathlineto{\pgfpoint{1.5pt}{-1.5pt}}
        }
    \state{midd}[width=3.2pt]
        {
        % First line
        \pgfpathmoveto{\pgfpoint{0pt}{1.5pt}}
        \pgfpathlineto{\pgfpoint{0.6pt}{2.0pt}}
        \pgfpathlineto{\pgfpoint{1.6pt}{1.5pt}}
        \pgfpathlineto{\pgfpoint{2.6pt}{1.0pt}}
        \pgfpathlineto{\pgfpoint{3.2pt}{1.5pt}}
        % Second line
        \pgfpathmoveto{\pgfpoint{0pt}{-1.5pt}}
        \pgfpathlineto{\pgfpoint{0.6pt}{-2.0pt}}
        \pgfpathlineto{\pgfpoint{1.6pt}{-1.5pt}}
        \pgfpathlineto{\pgfpoint{2.6pt}{-1.0pt}}
        \pgfpathlineto{\pgfpoint{3.2pt}{-1.5pt}}
        }
    \state{final}
        {
        %\pgfpathlineto{\pgfpointdecoratedpathlast}
        \pgfpathmoveto{\pgfpoint{0pt}{1.5pt}}
        \pgfpathlineto{\pgfpoint{2.0pt}{0pt}}
        \pgfpathlineto{\pgfpoint{0pt}{-1.5pt}}
        }
    }
\begin{document}
\begin{tikzpicture}[scale=1]
    \draw[decorate,decoration=screw] (0,100pt) -- (0,0);
\end{tikzpicture}
\end{document}

I would like to be able to change the amplitude and the spacing of the threads, too. How can I do this?

Cristobal
  • 151
  • Welcome to [tex.se]! I would suggest looking at the definition of the coil decoration and seeing how it introduces parameters. – Andrew Swann Aug 16 '16 at 14:24
  • Thanks! Could you tell me in which file it is defined, please? ... I can't find it. – Cristobal Aug 16 '16 at 14:39
  • it's in the manual – percusse Aug 16 '16 at 14:47
  • I once drew a screw using a pic for one thread, repeating as needed. – John Kormylo Aug 17 '16 at 02:32
  • But if you use a picture, you cannot control parameters such as the separation of threads. – Cristobal Aug 17 '16 at 11:27
  • I think @JohnKormylo was suggesting a pic - not a picture. Is your question solved? If so, it would be better to present your solution as an answer, rather than incorporating it into the question. The question space is best reserved for ... , well, for questions, really ;). – cfr Aug 18 '16 at 22:53
  • 1
    @cfr Yes, my question is solved. I didn't know that I could answer my own question :P I'll do it then. – Cristobal Aug 19 '16 at 08:04

1 Answers1

8

So, I could solve my problem with this code:

\documentclass[]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations}
% New decoration for screws
\tikzset{/pgf/decoration/.cd,
    head width/.initial=6pt,
    head length/.initial=1.5pt,
    thread separation/.initial=1.0pt,
    thread amplitude/.initial=0.5pt,
    screw radius/.initial=1.2pt,
}
% definition of the decoration
\pgfdeclaredecoration{screw}{initial}
{
  \state{initial}[width=\pgfkeysvalueof{/pgf/decoration/head length},%
                  next state=midd]
  {
    \def\headlength{%
      \pgfkeysvalueof{/pgf/decoration/head length}%
    }
    \def\headwidth{%
      \pgfkeysvalueof{/pgf/decoration/head width}%
    }
    \def\screwradius{%
      \pgfkeysvalueof{/pgf/decoration/screw radius}%
    }
    % First line
    \pgfpathlineto{\pgfpoint{0.0pt}{\headwidth/2}}
    \pgfpathlineto{\pgfpoint{\headlength}{\screwradius}}
    % Second line
    \pgfpathmoveto{\pgfpoint{0.0pt}{0.0pt}}
    \pgfpathlineto{\pgfpoint{0.0pt}{-\headwidth/2}}
    \pgfpathlineto{\pgfpoint{\headlength}{-\screwradius}}
  }
  \state{midd}[width=\pgfkeysvalueof{/pgf/decoration/thread separation}*2]
  {
    \def\threadseparation{%
      \pgfkeysvalueof{/pgf/decoration/thread separation}%
    }
    \def\threadamplitude{%
      \pgfkeysvalueof{/pgf/decoration/thread amplitude}%
    }
    \def\screwradius{%
      \pgfkeysvalueof{/pgf/decoration/screw radius}%
    }
    % First line
    \pgfpathmoveto{\pgfpoint{0pt}{\screwradius}}
    \pgfpathlineto{\pgfpoint{0.5*\threadseparation}{\screwradius+\threadamplitude}}
    \pgfpathlineto{\pgfpoint{1.0*\threadseparation}{\screwradius}}
    \pgfpathlineto{\pgfpoint{1.5*\threadseparation}{\screwradius-\threadamplitude}}
    \pgfpathlineto{\pgfpoint{2.0*\threadseparation}{\screwradius}}
    % Second line
    \pgfpathmoveto{\pgfpoint{0pt}{-\screwradius}}
    \pgfpathlineto{\pgfpoint{0.5*\threadseparation}{-\screwradius-\threadamplitude}}
    \pgfpathlineto{\pgfpoint{1.0*\threadseparation}{-\screwradius}}
    \pgfpathlineto{\pgfpoint{1.5*\threadseparation}{-\screwradius+\threadamplitude}}
    \pgfpathlineto{\pgfpoint{2.0*\threadseparation}{-\screwradius}}
    % Thread
    \pgfpathmoveto{\pgfpoint{0.5*\threadseparation}{\screwradius+\threadamplitude}}
    \pgfpathlineto{\pgfpoint{1.5*\threadseparation}{-\screwradius+\threadamplitude}}
  }
  \state{final}
  {
    \def\screwradius{%
      \pgfkeysvalueof{/pgf/decoration/screw radius}%
    }
    %\pgfpathlineto{\pgfpointdecoratedpathlast}
    \pgfpathmoveto{\pgfpoint{0pt}{\screwradius}}
    \pgfpathlineto{\pgfpoint{2.0pt}{0pt}}
    \pgfpathlineto{\pgfpoint{0pt}{-\screwradius}}
  }
}
\begin{document}
\begin{tikzpicture}[scale=1]
  \node (a) at (0pt,50pt) {};
  \node (b) at (0pt,0pt) {};
  \draw[decorate, decoration={screw}] (a) -- (b);
\end{tikzpicture}

\begin{tikzpicture}[scale=1]
  \node (c) at (0pt,50pt) {};
  \draw[decorate, decoration={screw, thread separation=0.5pt}] (c) arc(-180:-90:40pt);
\end{tikzpicture}

\begin{tikzpicture}[scale=1]
  \node (d) at (0pt,0pt) {};
  \node (e) at (70pt,0pt) {};
  \draw[decorate, decoration={screw, screw radius=5pt, head width=15pt, head length=3pt}] (a) -- (b);
\end{tikzpicture}

\end{document}

enter image description here

Cristobal
  • 151