使用 Tikz 随机装饰圆圈获得封闭路径

使用 Tikz 随机装饰圆圈获得封闭路径

我想使用 Tikz 装饰库来创建一些随机形状的对象。但是,我通常会获得开放的周长。有没有办法使用这种方法获得封闭的路径?

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary {decorations,decorations.pathmorphing}

\begin{document}
    \begin{tikzpicture}
        \foreach \x in {0,1,2,3}
            \foreach \y in {0,1,2}
                {\draw [decorate, decoration={random steps, segment length=4pt}] (\x,\y) circle [radius=.3cm];}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

有趣的!

以下是三种解决方法……

  1. 仅需decorate自己画圈并关闭路径。

    \tikz
      \foreach \x in {0,1,2,3}
        \foreach \y in {0,1,2}
          \draw [decoration={random steps, segment length=4pt}]
            (\x,\y) decorate {circle[radius=.3cm]} -- cycle;
    
  2. 使用非常小的post lengthand post=lineto(它并没有真正关闭路径,但至少线在它开始的地方结束):

    \tikz
      \foreach \x in {0,1,2,3}
        \foreach \y in {0,1,2}
          \draw [decorate, decoration={
            random steps, segment length=4pt, post length=0.01pt, post=lineto
          }] (\x,\y) circle[radius=.3cm];
    
  3. 与 2 相同,但close在其处使用了修饰post

    \pgfdeclaredecoration{close}{initial}{%
      \state{initial}[width=\pgfdecoratedremainingdistance]{\pgfpathclose}%
      \state{final}{}}%
    % …
    \tikz
      \foreach \x in {0,1,2,3}
        \foreach \y in {0,1,2}
          \draw [decorate, decoration={
            random steps, segment length=4pt, post length=0.01pt, post=close
          }] (\x,\y) circle[radius=.3cm];
    
    

…以及一种完全不同的方法:

\tikz
  \foreach \xxx in {0,1,2,3}
    \foreach \yyy in {0,1,2}
      \draw plot [
        sharp cycle,
        samples at = {0,...,9},
        shift={(\xxx,\yyy)}
      ] (360*\x/10+rnd*30:.3cm+.1cm*rnd);

当然,我们可以尝试各种不同的值来获得不同类型的“圆圈”。

sharp cycle密钥(手册中缺少)可确保路径再次关闭。

代码

\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.pathmorphing}
\pgfdeclaredecoration{close}{initial}{%
  \state{initial}[width=\pgfdecoratedremainingdistance]{\pgfpathclose}%
  \state{final}{}}%

\begin{document}
\tikz
  \foreach \x in {0,1,2,3}
    \foreach \y in {0,1,2}
      \draw [decoration={random steps, segment length=4pt}]
        (\x,\y) decorate {circle[radius=.3cm]} -- cycle;

\tikz
  \foreach \x in {0,1,2,3}
    \foreach \y in {0,1,2}
      \draw [decorate, decoration={
        random steps, segment length=4pt, post length=0.01pt, post=lineto
      }] (\x,\y) circle[radius=.3cm];

\tikz
  \foreach \x in {0,1,2,3}
    \foreach \y in {0,1,2}
      \draw [decorate, decoration={
        random steps, segment length=4pt, post length=0.01pt, post=close
      }] (\x,\y) circle[radius=.3cm];

\tikz
  \foreach \xxx in {0,1,2,3}
    \foreach \yyy in {0,1,2}
      \draw plot [
        sharp cycle,
        samples at = {0,...,9},
        shift={(\xxx,\yyy)}
      ] (360*\x/10+rnd*30:.3cm+.1cm*rnd);
\end{document}

输出(最后一个例子)

在此处输入图片描述

答案2

这是一个选项,用于生成具有直线和尖角的随机形状数组。答案改编自:tikz 中的随机非规则域

\documentclass{standalone}
\usepackage{tikz}

% https://tex.stackexchange.com/questions/218475/random-non-erratic-domain-in-tikz 
% Answer by Kpym

% create some random points arround 0
% #1 is the number of points
% #2 is the minimal radius
% #3 is the maximal deviation (if =0 no randomness)
\newcommand{\rndpts}[3]{
    \def\pts{}
    \foreach[
    evaluate=\x as \r using {#2+#3*rnd},
    evaluate=\x as \a using {\la+720*rnd/#1},
    remember=\a as \la (initially 0)]
    \x in {0,...,#1}
    {
        \pgfmathparse{int(\a)}
        \ifnum\pgfmathresult > 360\relax
        \breakforeach
        \else
        \xdef\pts{\pts (\a:\r)}
        \fi
    }
}
\begin{document}

    \begin{tikzpicture}
        \foreach \x in {0,1,2,3}
        \foreach \y in {0,1,2}
            {\rndpts{9}{.3}{0}
                \draw[black, ultra thick] plot [smooth cycle, tension=0, xshift=\x cm, yshift=\y cm]  coordinates {\pts};}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容