我想要创建两条不规则的线条,一条叠在另一条之上,遵循相同的模式。
关注 @vialrogo 对帖子的回答在技术图纸上随意绘制参差不齐的线条我已经走到这一步了,但我需要线条相同、不规则并且一个接一个地出现。
有没有办法可以精确复制随机线条?或者是否有其他代码可以让我生成两条不规则的线条,它们相等但相互重叠?
\documentclass[]{standalone}
\usepackage{tikz} % To plot almost everything.
\usetikzlibrary{fit, calc, matrix, positioning, arrows.meta, intersections, through, backgrounds, patterns}
\usepackage{xparse}
\begin{document}
\begin{tikzpicture}
\NewDocumentCommand{\irregularline}{%
O {2mm} % Amplitude of irregularity. Optional. Default value = 2mm
m % First point
m % Second point
D <> {20} % Number of peaks. Optional. Default value = 20
}{{%
\coordinate (old) at #2;
\foreach \i in {1,2,...,#4}{
\draw (old) -- ($ ($#2!\i/(#4+1)!#3$) + (0,#1*rand) $) coordinate (old);
}
\draw (old) -- #3;
}}
\irregularline[1mm]{(0,0)}{(5,0)}
\irregularline[1mm]{(0,1)}{(5,1)}
\irregularline[1mm]{(0,1.5)}{(5,1.5)}
\end{tikzpicture}
\end{document}
答案1
在 的定义中的循环之前添加\pgfmathsetseed{42}
(或使用您选择的其他数字)\irregularline
。
\documentclass[]{standalone}
\usepackage{tikz} % To plot almost everything.
\usetikzlibrary{fit, calc, matrix, positioning, arrows.meta, intersections, through, backgrounds, patterns}
\usepackage{xparse}
\begin{document}
\begin{tikzpicture}
\NewDocumentCommand{\irregularline}{%
O {2mm} % Amplitude of irregularity. Optional. Default value = 2mm
m % First point
m % Second point
D <> {20} % Number of peaks. Optional. Default value = 20
}{{%
\coordinate (old) at #2;
\pgfmathsetseed{42} % <-- added
\foreach \i in {1,2,...,#4}{
\draw (old) -- ($ ($#2!\i/(#4+1)!#3$) + (0,#1*rand) $) coordinate (old);
}
\draw (old) -- #3;
}}
\irregularline[1mm]{(0,0)}{(5,0)}
\irregularline[1mm]{(0,1)}{(5,1)}
\irregularline[1mm]{(0,1.5)}{(5,1.5)}
\end{tikzpicture}
\end{document}
答案2
只是为了完整性,也就是说,你不需要这个漂亮的宏来绘制一条随机线,也不需要你加载的所有库。使用Torbjørn T. 的回答以及 pgfmanual 第 48.2 节:
\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{decorations.pathmorphing}
\begin{document}
\begin{tikzpicture}
\pgfmathsetseed{42}
\draw[decorate,decoration={random steps,segment length=4pt,amplitude=2pt}]
(0, 0) -- (5,0);
\pgfmathsetseed{42}
\draw[decorate,decoration={random steps,segment length=4pt,amplitude=2pt}]
(0, -1) -- (5,-1);
\end{tikzpicture}
\end{document}