![如何用随机步骤正确装饰椭圆](https://linux22.com/image/280919/%E5%A6%82%E4%BD%95%E7%94%A8%E9%9A%8F%E6%9C%BA%E6%AD%A5%E9%AA%A4%E6%AD%A3%E7%A1%AE%E8%A3%85%E9%A5%B0%E6%A4%AD%E5%9C%86.png)
为了创建类似湖泊形状的东西,以下代码以平滑的随机步骤变换蓝色椭圆的边框。这个想法取自另一个答案。
\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.pathmorphing}
\pgfmathsetseed{42}
% The seed is only set explicitly to always generate the same picture.
% The problem occurs independent of seed.
\begin{document}
\begin{tikzpicture}
\fill[blue, draw=brown,
decoration={random steps,segment length=1cm,amplitude=.5cm},
decorate,
rounded corners=.3cm
] (0, 0) ellipse (3 and 2);
\draw[ultra thick, red] (3.1, 0) circle (.3);
\end{tikzpicture}
\end{document}
如红色圆圈所示,在椭圆的起始点(0 度)处,随机步骤没有正确平滑,而是增加了一个难看的角。如何修复此问题?
答案1
您可以使用选项pre
和post
来控制装饰路径的开始和结束处发生的情况。更多信息请参见Tikz/PGF 手册,第 21.4.2 节,第 247 页。
我建议这样做:
decoration={random steps,segment length=1cm,amplitude=.5cm,
pre=lineto,pre length=.25cm,post=lineto,post length=.25cm}
完整的源文件和结果如下。
\documentclass{article}
\usepackage[a4paper,margin=2cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\newcommand\rndellipse[2][]{%
\pgfmathsetseed{#2}
\begin{tikzpicture}
\fill[blue, draw=brown,
decoration={random steps,segment length=1cm,amplitude=.5cm,#1},
decorate,
rounded corners=.3cm
] (0, 0) ellipse (3 and 2);
\end{tikzpicture}}
\newcommand\try[1]{%
\noindent
\textbf{Seed #1} \\
\begin{tabular}{@{}cc@{}}
\rndellipse{#1} &
\rndellipse[pre=lineto,pre length=.25cm,post=lineto,post length=.25cm]{#1}
\end{tabular}
\par
}
\begin{document}
\try{42}
\try{54}
\try{666}
\try{4217}
\end{document}