如何在 TikZ 中反转任意路径中节点的顺序?我想填充由相同路径的移位版本所包围的形状,这些形状在 TikZ 中使用贝塞尔曲线构建(而不是像在 TikZ 中反转路径)。
我想到唯一的方法是使用 LaTeX \def 定义一组点,然后对该组点进行 \foreach \fill,但重复几次,这与简单地构建命名路径(甚至是 \def 的路径描述)一次,然后在填充操作等中移动它并反转遍历的顺序相比非常笨拙和冗长。能够轻松选择子路径也很好......总之就像这张手绘草图,但上半部分和下半部分由 Γ 分隔,并填充了不同的颜色或图案。
在 MetaPost 中,我记得这些操作相对轻松(我已经有一段时间没使用它了),但在其他方面它比 TikZ 更笨拙。
答案1
不确定这是否正是您想要的,但它可以扭转路径。
首先是一种用于pic
转移路径的方法......
\documentclass[tikz,border=10]{standalone}
\usetikzlibrary{decorations.pathreplacing}
\makeatletter
\tikzset{%
reverse path/.style={
decoration={show path construction,
reverse path,
moveto code={\pgfpathmoveto{\pgf@decorate@inputsegment@first}},
lineto code={\pgfpathlineto{\pgf@decorate@inputsegment@last}},
curveto code={\pgfpathcurveto{\pgf@decorate@inputsegment@supporta}%
{\pgf@decorate@inputsegment@supportb}{\pgf@decorate@inputsegment@last}},
closepath code={\pgfpathclose}
},
decorate
}
}
\begin{document}
\begin{tikzpicture}[very thick,
bezier path/.pic={
\draw [#1] (-1,1)
.. controls ++( 45:1/2) and ++(225:1/2) .. ( 1, 1)
.. controls ++(315:1/2) and ++(135:1/2) .. ( 1,-1)
.. controls ++(225:1/2) and ++( 45:1/2) .. (-1,-1);
}]
\draw ( 90:2) pic {bezier path={red}};
\draw (210:2) pic {bezier path={green, ->}};
\draw (330:2) pic {bezier path={blue, ->, reverse path}};
\end{tikzpicture}
\end{document}
...使用密钥进行以下操作insert path
将产生相同的结果:
\documentclass[tikz,border=10]{standalone}
\usetikzlibrary{decorations.pathreplacing}
\makeatletter
\tikzset{%
reverse path/.style={
decoration={show path construction,
reverse path,
moveto code={\pgfpathmoveto{\pgf@decorate@inputsegment@first}},
lineto code={\pgfpathlineto{\pgf@decorate@inputsegment@last}},
curveto code={\pgfpathcurveto{\pgf@decorate@inputsegment@supporta}%
{\pgf@decorate@inputsegment@supportb}{\pgf@decorate@inputsegment@last}},
closepath code={\pgfpathclose}
},
decorate
}
}
\begin{document}
\begin{tikzpicture}[very thick,
bezier path/.style={insert path={
[shift={#1}] (-1,1)
.. controls ++( 45:1/2) and ++(225:1/2) .. ( 1, 1)
.. controls ++(315:1/2) and ++(135:1/2) .. ( 1,-1)
.. controls ++(225:1/2) and ++( 45:1/2) .. (-1,-1)
}}]
\draw [bezier path={( 90:2)}, red];
\draw [bezier path={(210:2)}, green, ->];
\draw [bezier path={(330:2)}, blue, ->, reverse path];
\end{tikzpicture}
\end{document}