TikZ 随机步骤装饰“沿着网格”

TikZ 随机步骤装饰“沿着网格”

我正在尝试创建某些 TikZ 图像,其中有从一个点到另一个点的随机路径,使得这些随机路径遵循方形网格。更准确地说,我想调整以下示例:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}

\begin{document}
\begin{tikzpicture}
  \draw (0,0) rectangle (3,3);
  \draw (1,1) rectangle (2,2);

  \draw [olive,thick, decorate, decoration={random steps,segment
    length=5pt,amplitude=3pt}] (0.5,0) to (0.5,3);
  \draw [olive,thick, decorate, decoration={random steps,segment
    length=5pt,amplitude=3pt}] (0,0.5) to (3,0.5);
  \draw [olive,thick, decorate, decoration={random steps,segment
    length=5pt,amplitude=3pt}] (2.5,0) to (2.5,3);
  \draw [olive,thick, decorate, decoration={random steps,segment
    length=5pt,amplitude=3pt}] (0,2.5) to (3,2.5);
\end{tikzpicture}
\end{document}

此代码产生此(随机)图片:

现在,我希望随机(橄榄色)路径沿着方形网格行进,这意味着随机步骤只能沿着水平和垂直方向行进。

我已查阅过 PGF-TikZ 手册,但似乎没有针对此类行为的任何直接选项。

有没有简单的方法来实现这一点,最好使用 decoration.pathmorphing 库而不是一些手写算法?

答案1

欢迎使用 TeX-SE!您只需稍微修改装饰即可获得所需的效果。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}

\pgfdeclaredecoration{vertical random 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}
  \draw (0,0) rectangle (3,3);
  \draw (1,1) rectangle (2,2);

  \draw [olive,thick, decorate, decoration={vertical random steps,segment
    length=5pt,amplitude=3pt}] (0.5,0) to (0.5,3);
  \draw [olive,thick, decorate, decoration={vertical random steps,segment
    length=5pt,amplitude=3pt}] (0,0.5) to (3,0.5);
  \draw [olive,thick, decorate, decoration={vertical random steps,segment
    length=5pt,amplitude=3pt}] (2.5,0) to (2.5,3);
  \draw [olive,thick, decorate, decoration={vertical random steps,segment
    length=5pt,amplitude=3pt}] (0,2.5) to (3,2.5);
\end{tikzpicture}
\end{document}

在此处输入图片描述

请注意,装饰会在切线空间中变换您,即第二个方向沿法线(第一个方向沿切线)。

原则上,您不需要这里装饰,只需使用plotsamples at添加rnd噪音即可。

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \draw (0,0) rectangle (3,3);
  \draw (1,1) rectangle (2,2);

  \draw [olive,thick] plot[variable=\x,samples at={0,0.1,...,2.9,3}] 
  ({0.5cm+3pt*(2*rnd-1)},\x)
  plot[variable=\x,samples at={0,0.1,...,2.9,3}] 
  ({2.5cm+3pt*(2*rnd-1)},\x)
  plot[variable=\x,samples at={0,0.1,...,2.9,3}] 
  (\x,{0.5cm+3pt*(2*rnd-1)})
  plot[variable=\x,samples at={0,0.1,...,2.9,3}] 
  (\x,{2.5cm+3pt*(2*rnd-1)});
\end{tikzpicture}
\end{document}

在此处输入图片描述

再次尝试让您开心。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}

\pgfdeclaredecoration{fixed snake}{start}
{%
  \state{start}[width=+0pt,next state=step,
  persistent precomputation=\pgfdecoratepathhascornerstrue
  \pgfmathsetmacro{\mysegmentlength}{%
  int(\pgfdecoratedpathlength/\pgfdecorationsegmentlength)*%
  \pgfdecorationsegmentlength*\pgfdecorationsegmentlength/\pgfdecoratedpathlength}
  \setlength\pgfdecorationsegmentlength{\mysegmentlength pt}
  ]{}%
  \state{step}[auto end on length=1.5\pgfdecorationsegmentlength,
               auto corner on length=1.5\pgfdecorationsegmentlength,
               width=+\pgfdecorationsegmentlength]
  { 
    \pgfmathrandominteger{\myint}{1}{2}
    \pgfmathsetmacro{\myrand}{sign(\myint-1.5)*\pgfdecorationsegmentamplitude}
    \pgfpathlineto{
      \pgfpointadd
      {\pgfpoint{0pt}{0pt}}
      {\pgfpoint{0pt}{\myrand pt}}
    }
    \pgfpathlineto{
      \pgfpoint{\pgfdecorationsegmentlength}{\myrand pt}
    }
  }%
  \state{final}[width=0pt]
  {%\pgfpathlineto{\pgfpoint{-\pgfdecorationsegmentlength}{0pt}}
  \pgfpathmoveto{\pgfpointdecoratedpathlast}
  }%
}%


\begin{document}
\begin{tikzpicture}
  \draw[clip] (0,0) rectangle (3,3);
  \draw (1,1) rectangle (2,2);
  \draw [olive,thick, decorate, decoration={fixed snake,segment
    length=5pt,amplitude=3pt}] (0.5,0) to (0.5,3.5);
  \draw [olive,thick, decorate, decoration={fixed snake,segment
    length=5pt,amplitude=3pt}] (0,0.5) to (3.5,0.5);
  \draw [olive,thick, decorate, decoration={fixed snake,segment
    length=5pt,amplitude=3pt}] (2.5,0) to (2.5,3.5);
  \draw [olive,thick, decorate, decoration={fixed snake,segment
    length=5pt,amplitude=3pt}] (0,2.5) to (3.5,2.5);
\end{tikzpicture}
\end{document}

在此处输入图片描述

如果这又不是你想要的,请考虑用精确的术语解释你想要什么。如果你对想要得到的东西给出了非常明确的处方,我很乐观地找到一种方法来实现它。

相关内容