如何绘制在背景和前景中交替的路径?

如何绘制在背景和前景中交替的路径?

我想画一个“环绕”杆的弹簧,这意味着每条交替的线都应该画在背景上。是否可以为此定义一个 tikzstyle?

\documentclass[tikz, border=3mm]{standalone}

\usetikzlibrary{decorations.pathmorphing}
\usepackage{kinematikz}

\tikzset{
    spring/.style = {
        thick,decorate,green!40!black,decoration={zigzag,amplitude=13,segment length=12}
    },
}

\begin{document}
    
\begin{tikzpicture}
    \coordinate (P1) at (0,0);
    \coordinate (P2) at (5,0);
    \pic (bar) at (P1) {link bar generic=P2/0};
    \draw[spring] (P1) -- (P2);
\end{tikzpicture}
    
\end{document}

输出图像

kinematikz套餐仅适用于酒吧pic

答案1

这可能不是一个优雅的方法。但似乎有效。您可以声明两个装饰:一个用于右倾斜弹簧,一个用于左倾斜弹簧。然后将一个放在条形图之前,另一个放在条形图之后(将右倾斜弹簧放在前景可能会更好)。

\documentclass[tikz, border=3mm]{standalone}
\usetikzlibrary{decorations.pathmorphing}
\usepackage{kinematikz}

\pgfdeclaredecoration{lspr}{initial}
{
\state{initial}[width=12.5pt]
{
\pgfpathmoveto{\pgfpoint{4pt}{12pt}}
\pgfpathlineto{\pgfpoint{8.5pt}{-12pt}}
}
\state{final}
{
\pgfpathmoveto{\pgfpointdecoratedpathlast}
}
}

\pgfdeclaredecoration{rspr}{initial}
{
\state{initial}[width=12.5pt]
{
\pgfpathlineto{\pgfpoint{4pt}{12pt}}
\pgfpathmoveto{\pgfpoint{8.5pt}{-12pt}}
\pgfpathlineto{\pgfpoint{12.5pt}{0pt}}
}
\state{final}
{
\pgfpathlineto{\pgfpointdecoratedpathlast}
}
}

\tikzset{
    rspring/.style = {
        thick,decorate,green!40!black,decoration={rspr, pre length=0.3cm}
    },
    lspring/.style = {
        thick,decorate,green!40!black,decoration={lspr, pre length=0.3cm}
    },
}

\begin{document}
\begin{tikzpicture}
    \coordinate (P1) at (0,0);
    \coordinate (P2) at (5,0);
    \draw[lspring] (P1) -- (P2);
    \pic (bar) at (P1) {link bar generic=P2/0};
    \draw[rspring] (P1) -- (P2);
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

这只是对 @Tom 非常好的回答的补充。有了这些定义,就可以使用amplitude和参数化装饰segmengt length。它们的结果分别存储在 TeX 维度\pgfdecorationsegmentamplitude\pgfdecorationsegmentlength中。我还在定义中添加了一个参数style,这样就可以在本地覆盖默认值。

\documentclass[tikz, border=3mm]{standalone}

\usetikzlibrary{decorations.pathmorphing}
\usepackage{kinematikz}

\pgfdeclaredecoration{lspr}{initial}
{
    \state{initial}[width=\pgfdecorationsegmentlength]
    {
        \pgfpathmoveto{\pgfpoint{\pgfdecorationsegmentlength/3}{\pgfdecorationsegmentamplitude}}
        \pgfpathlineto{\pgfpoint{\pgfdecorationsegmentlength/3*2}{-\pgfdecorationsegmentamplitude}}
    }
    \state{final}
    {
        \pgfpathmoveto{\pgfpointdecoratedpathlast}
    }
}

\pgfdeclaredecoration{rspr}{initial}
{
    \state{initial}[width=\pgfdecorationsegmentlength]
    {
        \pgfpathlineto{\pgfpoint{\pgfdecorationsegmentlength/3}{\pgfdecorationsegmentamplitude}}
        \pgfpathmoveto{\pgfpoint{\pgfdecorationsegmentlength/3*2}{-\pgfdecorationsegmentamplitude}}
        \pgfpathlineto{\pgfpoint{\pgfdecorationsegmentlength}{0pt}}
    }
    \state{final}
    {
        \pgfpathlineto{\pgfpointdecoratedpathlast}
    }
}

\tikzset{
    rspring/.style = {
        thick,decorate,green!40!black,decoration={rspr, amplitude=12pt, segment length=12pt, #1}
    },
    lspring/.style = {
        thick,decorate,green!40!black,decoration={lspr, amplitude=12pt, segment length=12pt, #1}
    },
}

\begin{document}
    
\begin{tikzpicture}
    \coordinate (P1) at (0,0);
    \coordinate (P2) at (5,0);
    \draw[lspring] (P1) -- (P2);
    \pic (bar) at (P1) {link bar generic=P2/0};
    \draw[rspring] (P1) -- (P2);
    
    \begin{scope}[yshift=-1.2cm]
        \coordinate (P3) at (0,0);
        \coordinate (P4) at (5,0);
        \draw[lspring={amplitude=14pt, segment length=17pt}] (P3) -- (P4);
        \pic (bar) at (P3) {link bar generic=P4/0};
        \draw[rspring={amplitude=14pt, segment length=17pt}] (P3) -- (P4);
    \end{scope}
\end{tikzpicture}
    
\end{document}

不同的弹簧

我再一次惊讶于TikZ/PGF它是多么的简单和灵活!

相关内容