我想在 tikz 中沿平滑路径绘制直线。我尝试decorations.markings-solution
使用在 Tikz 中沿路径绘制路径/节点并像这样使用它:
\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\begin{document}
\pagestyle{empty}
\begin{tikzpicture}[
decoration={
markings,
mark=between positions 0.125 and 0.90 step 0.0625 with {\draw[thin] (0,-0.2)--(0,0.2);},
}
]
\draw[thick,postaction={decorate}] (-2.5,-1) to (-2,-0.5)
to [out=45,in=180] (0,-1.5)
to [out=0,in=180] (2,-1)
to [out=0,in=-90] (2.5,0)
to [out=90,in=0] (1.5,1.5)
to [out=170,in=0] (0,1.5)
to [out=180,in=45] (-2,-0.2)
to [out=220,in=45] (-2.5,-1);
\end{tikzpicture}
\end{document}
结果如下:
但是,这不是我想要的。我想要两张图片,在第一张图片中,直线应该保持笔直,也就是说,我希望它们只移动而不是旋转。我该如何实现呢?根据 tikz 文档,旋转线条的原因是它decoration.markings
自动使用坐标系,其中 x 轴与曲线相切。有没有办法使用全局坐标系?在第二张图片中,我希望直线旋转,但我想自己做。要计算旋转度数,我需要我的线的当前编号,它应该由\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}
和线的总数给出。我该如何得到这个?
答案1
诀窍是使用 重置变换角度\pgftransformresetnontranslations
,我遇到了同样的问题,我只能通过挖掘 找到它<tex-installation-path>/tex/generic/pgf/frontendlayer/tikz/tikz.code.tex
。只有当我知道命令后,我才在 pgfmanual 的第 75.2 节中发现它。
如果您想以设定的角度绘制直线,极坐标系是最佳选择。然后您可以指定绘制线条的角度,数学引擎会解析该角度,因此您可以简单地举例说明20*\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}
。第二个参数是到坐标系原点的距离。因此,如果您想要相对于原点以外的点的恒定线长,则需要指定相对坐标,例如(0,-0.2) -- +(20:4mm)
。
编辑如果您想要曲线上的这些线的中点,最简单的方法是绘制两条线的一半,第二条线要么具有相同的角度 +180,要么具有负半径。第二种选择可能更安全,因为角度应为 -360 度到 +720 度之间的值,而添加 180 度则是范围的很大一部分。
\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\begin{document}
\pagestyle{empty}
\begin{tikzpicture}[
decoration={
markings,
mark=between positions 0.125 and 0.90 step 0.0625 with {
\pgftransformresetnontranslations
\draw[thin] (0,-0.2)--(0,0.2);},
}
]
\draw[thick,postaction={decorate}] (-2.5,-1) to (-2,-0.5)
to [out=45,in=180] (0,-1.5)
to [out=0,in=180] (2,-1)
to [out=0,in=-90] (2.5,0)
to [out=90,in=0] (1.5,1.5)
to [out=170,in=0] (0,1.5)
to [out=180,in=45] (-2,-0.2)
to [out=220,in=45] (-2.5,-1);
\end{tikzpicture}
\begin{tikzpicture}[
decoration={
markings,
mark=between positions 0.125 and 0.90 step 0.0625 with {
\pgftransformresetnontranslations
\draw[thin] (0,0)--+(canvas polar cs:angle=20*\pgfkeysvalueof{/pgf/decoration/mark info/sequence number},radius=2mm);
\draw[thin] (0,0)--+(canvas polar cs:angle=20*\pgfkeysvalueof{/pgf/decoration/mark info/sequence number},radius=-2mm);},
}
]
\draw[thick,postaction={decorate}] (-2.5,-1) to (-2,-0.5)
to [out=45,in=180] (0,-1.5)
to [out=0,in=180] (2,-1)
to [out=0,in=-90] (2.5,0)
to [out=90,in=0] (1.5,1.5)
to [out=170,in=0] (0,1.5)
to [out=180,in=45] (-2,-0.2)
to [out=220,in=45] (-2.5,-1);
\end{tikzpicture}
\end{document}
答案2
这是针对问题第一部分的解决方案:(红色)直线只移动而不旋转。
(我不明白你的问题的第二部分......)
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\begin{document}
\begin{tikzpicture}
\tikzset{
decoration={
markings,
mark=between positions 0.125 and 0.90 step 0.0625 with {
\draw[thin] (0,-0.2)--(0,0.2);
\xdef\maxseq{\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}}
\coordinate (pt-\maxseq)
at (0,0);
},
}
}
\draw[thick,postaction={decorate}] (-2.5,-1) to (-2,-0.5)
to [out=45,in=180] (0,-1.5)
to [out=0,in=180] (2,-1)
to [out=0,in=-90] (2.5,0)
to [out=90,in=0] (1.5,1.5)
to [out=170,in=0] (0,1.5)
to [out=180,in=45] (-2,-0.2)
to [out=220,in=45] (-2.5,-1);
\foreach \c in {1,...,\maxseq}{
\draw[red] (pt-\c) +(0,-0.2) -- +(0,0.2);
}
\end{tikzpicture}
\end{document}