我想以一种方式装饰一个圆圈,使装饰的起点和终点重合一个好方法。当然,它们总是重合的,但我希望装饰元素重复整数次。有没有简单的方法来实现它?当然,调整装饰的amplitude
和/或segment length
可以有所帮助,但它需要手动微调,我想避免这种情况。
我看了pgf
手动的,特别是第 24 章,但我没有找到答案。顺便说一句,第 24.3 节包含我的部分代码,它们有同样的“问题”,而第 24.4 节(调整装饰)或 48.2 节(路径变形装饰)对我来说毫无用处。
\documentclass[tikz, border=3mm]{standalone}
\usetikzlibrary{decorations.pathmorphing}
\begin{document}
\begin{tikzpicture}
\draw[decorate, decoration=zigzag] (0,0) circle (1cm);
\draw[decorate, decoration=bumps] (3,0) circle (1cm);
\draw[red, thick] (1,0) circle (3mm) (4,0) circle (3mm); % <- Just to stress the problem
\end{tikzpicture}
\end{document}
答案1
一个临时建议是将线段长度设置为 pi 毫米(对于半径为 1 厘米的圆)。
\documentclass[tikz, border=3mm]{standalone}
\usetikzlibrary{decorations.pathmorphing}
\begin{document}
\begin{tikzpicture}
\draw[decorate, decoration={zigzag,segment length=3.14mm}] (0,0) circle (1cm);
\draw[decorate, decoration={bumps,segment length=3.14mm}] (3,0) circle (1cm);
\draw[red, thick] (1,0) circle (3mm) (4,0) circle (3mm); % <- Just to stress the problem
\end{tikzpicture}
\end{document}
更新:更完整的锯齿形解决方案。
\documentclass[tikz, border=3mm]{standalone}
\usetikzlibrary{decorations,decorations.pathmorphing}
\pgfkeys{/tikz/.cd,
pattern repetition/.store in=\PatternRepetition,
pattern repetition=20
}
\pgfdeclaremetadecoration{closing zigzag}{initial}{%
\state{initial}[width=0pt, next state=zigzag] {
\pgfmathdivide{\pgfmetadecoratedpathlength}{\PatternRepetition}
\let\pgfmetadecorationsegmentlength\pgfmathresult
\pgfset{/pgf/decoration/segment length=\pgfmathresult pt}
}
\state{zigzag}[width=\pgfmetadecorationsegmentlength, next state=zigzag] {
\decoration{zigzag}
}
\state{final}
{
\pgfpathlineto{\pgfpointdecoratedpathlast}
}
}
% does not quite work yet
\pgfdeclaremetadecoration{closing bumps}{initial}{%
\state{initial}[width=0pt, next state=bumps] {
\pgfmathdivide{\pgfmetadecoratedpathlength}{\PatternRepetition}
\pgfset{/pgf/decoration/segment length=\pgfmathresult pt}
\pgfmathdivide{\pgfmetadecoratedpathlength}{\PatternRepetition}
\let\pgfmetadecorationsegmentlength\pgfmathresult
}
\state{bumps}[width=\pgfmetadecorationsegmentlength, next state=bumps] {
\decoration{bumps}
}
\state{final}
{
\pgfpathlineto{\pgfpointdecoratedpathlast}
}
}
\newlength{\totallength}
\begin{document}
\begin{tikzpicture}
\draw[decorate, decoration={closing zigzag}] (0,0) circle (1cm);
\tikzset{pattern repetition=55}
\draw[decorate, decoration={closing zigzag}] (3,0) circle (1cm and 2cm);
\draw[red, thick] (1,0) circle (3mm) (4,0) circle (3mm); % <- Just to stress the problem
\end{tikzpicture}
\end{document}