
我想將定義的曲線與\draw
沿路徑的隨機變化結合。
到目前為止,我在 tex.SE 上發現了一些有趣的討論,例如如何在 tikz/pgf 中繪製布朗運動,如何在 Beamer 幀中使用 tikz 修復由“rand”函數產生的布朗運動軌跡,並且(感謝 Kpym,我已經監督了這一點)模擬手繪線條,但它們沒有按預期工作。
使用 tikz-decorations 看起來很有前途,但僅限於相對筆直的曲線。
以下 MWE 給出了這個結果:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\begin{document}
\begin{tikzpicture}[x=5mm,y=5mm,decoration={random steps,segment length=3mm,amplitude=1mm}]
\draw[thick,green] (0, 1) -- (14.5, 1);
\draw[thick,red,decorate,rounded corners] (0,-0.5) -- (14.5,-0.5);
\draw[thick,blue,decorate,rounded corners] (0, 0.5) -- (1,0.5) -- (1.5,-4) -- (2,-3.5) -- (3.5,-2) -- (14.5, 0);
\end{tikzpicture}
\end{document}
第三個(藍色)\draw
命令產生的結果不是我試圖得到的結果 - 即使紅線在 x 方向的大約 75% 處也有一個「小突起」。
根據 tikz 手冊,原因是:每個步驟的末端在 x 和 y 方向上都受到從區間 [−d,d] 均勻繪製的兩個值的擾動,其中 d 是幅度值。這與曲線選項相衝突,在曲線選項中僅改變 y 值即可給出所需的輸出。由於這些圖表描述了值隨時間的變化,因此它們可能不會轉向負 x 方向。
有什麼方法可以限制random steps
裝飾只操作 x 值而不操作 y 值?另一種方法是透過離散間隔的隨機變形來覆蓋曲線,例如布朗運動...
希望結果在某種程度上是穩定的(這與隨機變化的想法相衝突 - 哈哈)沒有真正需要控制種子...
答案1
這是以下問題的答案:在 y 方向上可以有隨機步驟嗎?答案是肯定的,我們需要做的就是複製隨機步驟的定義並將移位設為x
零。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\pgfdeclaredecoration{random y steps}{start}
{%
\state{start}[width=+0pt,next state=step,persistent precomputation=\pgfdecoratepathhascornerstrue]{}%
\state{step}[auto end on length=1.5\pgfdecorationsegmentlength,
auto corner on length=1.5\pgfdecorationsegmentlength,
width=+\pgfdecorationsegmentlength]
{
\pgfpathlineto{
\pgfpointadd
{\pgfpoint{\pgfdecorationsegmentlength}{0pt}}
{\pgfpoint{0pt}{rand*\pgfdecorationsegmentamplitude}}
}
}%
\state{final}
{}%
}%
\begin{document}
\begin{tikzpicture}[x=5mm,y=5mm,decoration={random y steps,segment length=3mm,amplitude=1mm}]
\draw[thick,green] (0, 1) -- (14.5, 1);
\draw[thick,red,decorate,rounded corners] (0,-0.5) -- (14.5,-0.5);
\draw[thick,blue,decorate,rounded corners] (0, 0.5) -- (1,0.5) -- (1.5,-4) -- (2,-3.5) -- (3.5,-2) -- (14.5, 0);
\end{tikzpicture}
\end{document}
當然可以畫一條平滑的隨機曲線。此時,必須分兩步驟進行。
\path[decorate] <path>;
\draw[<options>] plot[variable=\x,samples at={1,...,\arabic{randymark}},smooth] (randymark\x);
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations.pathmorphing}
\newcounter{randymark}
\pgfdeclaredecoration{mark random y steps}{start}
{%
\state{start}[width=+0pt,next state=step,%
persistent precomputation={\pgfdecoratepathhascornerstrue%
\setcounter{randymark}{0}}]{
\stepcounter{randymark}
\pgfcoordinate{randymark\arabic{randymark}}{\pgfpoint{0pt}{0pt}}
}%
\state{step}[auto end on length=1.5\pgfdecorationsegmentlength,
auto corner on length=1.5\pgfdecorationsegmentlength,
width=+\pgfdecorationsegmentlength]
{ \stepcounter{randymark}
\pgfcoordinate{randymark\arabic{randymark}}{\pgfpoint{\pgfdecorationsegmentlength}{rand*\pgfdecorationsegmentamplitude}}
}%
\state{final}
{
\stepcounter{randymark}
\pgfcoordinate{randymark\arabic{randymark}}{\pgfpointdecoratedpathlast}}%
}%
\begin{document}
\begin{tikzpicture}[x=5mm,y=5mm,decoration={mark random y steps,segment length=3mm,amplitude=1mm}]
\path[decorate] (0,-0.5) -- (14.5,-0.5);
\draw[red,thick] plot[variable=\x,samples at={1,...,\arabic{randymark}},smooth]
(randymark\x);
\path[decorate] (0, 0.5) -- (1,0.5) -- (1.5,-4) -- (2,-3.5) -- (3.5,-2) -- (14.5, 0);
\draw[blue,thick] plot[variable=\x,samples at={1,...,\arabic{randymark}},smooth]
(randymark\x);
\path[decorate] (4,4) circle(3cm);
\draw[orange,thick] plot[variable=\x,samples at={1,...,\arabic{randymark}},smooth]
(randymark\x);
\end{tikzpicture}
\end{document}
請注意,這當然不是第一篇通過路徑繪製平滑隨機曲線的帖子,有幾篇較早的帖子,包括這個,這個,答案這個問題以及答案這個問題。