我正在尝试定义一个装饰器,在两个连接点之间画一个螺丝。到目前为止,我所拥有的就是这样的:
\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}
我也想改变线的振幅和间距。我该怎么做?
答案1
因此,我可以用以下代码解决我的问题:
\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}