是否可以连续应用两个路径变形/路径替换装饰?

是否可以连续应用两个路径变形/路径替换装饰?

假设有人想实现以下目标: 目标:连续两个路径变形/路径替换装饰

当前状态

根据另一篇帖子然后,我能够创建一个“MWE”,以说明我希望它如何工作:

\documentclass[border=5pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{decorations, decorations.pathmorphing, decorations.pathreplacing}

\def\width{5mm}

\pgfdeclaredecoration{make wide}{initial}
{
    \state{initial}[width=1pt]
    {
        \pgfpathmoveto{\pgfpoint{0pt}{-0.5*\width}}
        \pgfpathlineto{\pgfpoint{1pt}{-0.5*\width}}
        \pgfpathmoveto{\pgfpoint{0pt}{0.5*\width}}
        \pgfpathlineto{\pgfpoint{1pt}{0.5*\width}}
    }
    \state{final}
    {
        \pgfpathmoveto{\pgfpoint{0pt}{0.5*\width}}
        \pgfpathlineto{\pgfpoint{1pt}{0.5*\width}}
        \pgfpathmoveto{\pgfpoint{0pt}{-0.5*\width}}
        \pgfpathlineto{\pgfpoint{1pt}{-0.5*\width}}
    }
}

\tikzstyle{make wide and ticks} = [
    decoration={make wide},
    decorate,
    postaction={
        decoration={ticks},
        decorate
    }
]

\begin{document}
    \begin{tikzpicture}
        \draw[make wide and ticks](0,0)to[out=0, in=180](5,2);
    \end{tikzpicture}
\end{document}

但是输出是:
MWE:结果输出

为了排除我之前遇到的执行顺序奇怪的问题tikzstyle,我也尝试了不定义 ,tikzstyle结果相同(正如人们所期望的那样)。根据我对 实现的理解postaction,不可能在阶段decoration中使用 every postaction

问题:

  1. 有没有办法用decorations 实现所需的输出?
  2. 是否有另一种简单的方法也适用于任意形状的路径?

答案1

在此处输入图片描述

解决方案分两步实现:

  1. 表征路径某个管状邻域的点是通过一个装饰来构造的,towards tubular该装饰需要两个参数(步长和与路径的距离pt
  2. ticks通过命令L ticksR ticks(左、右管状边界)构建管状邻域的两条对应路径,并用预定义的装饰进行装饰。

请注意,在上图的第一幅图中,装饰的最后一个刻度存在问题;它没有“覆盖”初始路径的最后部分(以黑色绘制)。为此,添加了一些“尾巴”,然后towards tubular调用装饰(参见中间的图)。尾巴是通过选项添加的e

我希望最后的绘图能够接近你所寻找的效果。

代码

\documentclass[11pt, margin=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{math}
\usetikzlibrary{decorations.markings, decorations.pathreplacing}

\begin{document}
\tikzset{%
  e/.style={% step length -- in points
    % needed for the last tick of open curves
    insert path={-- ([turn]0: #1pt)}
  },
  towards tubular/.style 2 args={% step length, width -- in points
    % create the points describing the tubular neighborhood
    decoration={markings,
      mark=between positions 0 and 1 step #1pt with {
        \tikzmath{%
          integer \pNumber;
          \pNumber = \pgfkeysvalueof{/pgf/decoration/mark
            info/sequence number};
        }
        \pgfextra{\xdef\pNumber{\pNumber}}
        \path (0, #2pt) coordinate (TL-\pNumber);
        \path (0, -#2pt) coordinate (TR-\pNumber);
      }
    },
    postaction=decorate
  },
  L ticks/.style 2 args={% step length, tick length -- in points
    decorate,
    decoration={ticks, segment length=#1pt, amplitude=#2pt},
    insert path={%
      (TL-1) \foreach \i in {2, ..., \pNumber}{-- (TL-\i)}
    }
  },
  R ticks/.style 2 args={% step length, tick length -- in points
    decorate,
    decoration={ticks, segment length=#1pt, amplitude=#2pt},
    insert path={%
      (TR-1) \foreach \i in {2, ..., \pNumber}{-- (TR-\i)}
    }
  }
}

\begin{tikzpicture}
  \draw[help lines, opacity=.3] (-4, -3) grid (4, 4);

  % first drawing
  \draw[towards tubular={3}{6}]
  (-3, -2.5) .. controls ++(30:5) and ++(210:3) .. ++(-1, 6);
  \draw[blue, L ticks={4}{2}];
  \draw[red, R ticks={4}{2}];

  % second drawing
  \path[towards tubular={6}{6}]
  (0, -2.5) .. controls ++(30:5) and ++(210:3) .. ++(-1, 6) [e=5];
  \draw
  (0, -2.5) .. controls ++(30:5) and ++(210:3) .. ++(-1, 6);
  \draw[blue, L ticks={4}{2}];
  \draw[red, R ticks={4}{2}];

  % third drawing
  \path[towards tubular={3}{6}]
  (3, -2.5) .. controls ++(30:5) and ++(210:3) .. ++(-1, 6) [e=3];
  \draw[blue, L ticks={4}{2}];
  \draw[red, R ticks={4}{2}];
\end{tikzpicture}
\end{document}

相关内容