在 TikZ 中绘制一条曲线,其中一侧长有毛发

在 TikZ 中绘制一条曲线,其中一侧长有毛发

我希望能够绘制概括以下内容的 tikz 图片:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw (0,0) to (1,0);
\foreach \n in {.1,.2,...,.9} \draw (\n,0) to (\n,.1);
\end{tikzpicture}
\end{document}

上述代码的结果是一条水平线,其中一侧长出短“毛发”,垂直于曲线。我想实现这种效果,但对于以自定义方式弯曲的曲线,例如通过指定它们经过的点数以及每个点的入角和出角。在这种情况下,我不能只是重复上述策略,因为毛发的确切端点是通过用于定义曲线的点和入角/出角以复杂的方式确定的。有什么想法吗?

答案1

无耻地使用这个伟大的切线答案解决您的问题。尝试以下代码:

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{decorations.markings}

\begin{document}
\begin{tikzpicture}[
    tangent/.style={decoration={markings,mark=at position #1 with {
      \coordinate (tangent point-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (0pt,0pt);
      \coordinate (tangent unit vector-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (1,0pt);
      \coordinate (tangent orthogonal unit vector-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (0pt,1);
      }},postaction=decorate},
    use tangent/.style={
        shift=(tangent point-#1),
        x=(tangent unit vector-#1),
        y=(tangent orthogonal unit vector-#1)
    },
    use tangent/.default=1
]
% Define your path here
\def\PTH{(0,0) to [out=20,in=120] (5,2) to [out=-60, in=110] (9,1)}
% Choose the number of "hairs" here
\def\tans{30}
\draw \PTH;
\foreach \x in {1,...,\tans}{
  \path[tangent=\x/\tans] \PTH;
  \draw [blue, thick, use tangent=1] (0,0) -- (0,.5);
}
\end{tikzpicture}
\end{document}

这应该会给你以下图片:

在此处输入图片描述

请注意,您可以更改“毛发”的长度,现在循环中将其设置为 0.5 foreach。此外,如果您将“毛发”的端点改为(.5,0),那么您将获得与路径相切的短毛发。

相关内容