答案1
最初的尝试
这是一个尝试,可能可以放大到您想要的任何尺寸:
\documentclass[tikz,border=2mm]{standalone}
\pgfmathsetseed{\pdfuniformdeviate 10000000} %Credit: https://tex.stackexchange.com/a/212755/117534
\begin{document}
\begin{tikzpicture}
\foreach \i in {0,...,4}
\foreach \j in {0,...,4}
\coordinate[] (n-\i\j) at (\i + 0.5*rand,\j + 0.5*rand) {};
\foreach \i in {0,...,4}
\foreach \j [count=\jj] in {0,...,3}
\draw (n-\i\j) -- (n-\i\jj) (n-\j\i) -- (n-\jj\i);
\end{tikzpicture}
\end{document}
一些输出:
带曲线
不过,现在有了弯曲的边缘,控制就变得不那么好了。我添加了代码:
\pgfmathparse{random(0,60)-30}
\draw (n-\i\j) to [bend right=\pgfmathresult] (n-\i\jj) (n-\j\i) to [bend right=\pgfmathresult] (n-\jj\i);
}
而是生成一个从 -30 到 30 的数字。也就是说,如果结果为负数,它将向左弯曲,如果结果为正数,它将向右弯曲。完整代码和示例:
\documentclass[tikz,border=2mm]{standalone}
\pgfmathsetseed{\pdfuniformdeviate 10000000} %Credit: https://tex.stackexchange.com/a/212755/117534
\begin{document}
\begin{tikzpicture}
\foreach \i in {0,...,4}
\foreach \j in {0,...,4}
\coordinate[] (n-\i\j) at (\i + 0.5*rand,\j + 0.5*rand) {};
\foreach \i in {0,...,4}
\foreach \j [count=\jj] in {0,...,3}{%
\pgfmathparse{random(0,60)-30}
\draw (n-\i\j) to [bend right=\pgfmathresult] (n-\i\jj) (n-\j\i) to [bend right=\pgfmathresult] (n-\jj\i);
}
\end{tikzpicture}
\end{document}
矩形网格
我添加了一些注释,并声明了两个命令,让您输入所需的行数/列数。这里与上面的代码的主要区别在于,我分开了水平线和垂直线的绘制,以适应矩形网格。
\documentclass[tikz,border=2mm]{standalone}
\pgfmathsetseed{\pdfuniformdeviate 10000000} %Credit: https://tex.stackexchange.com/a/212755/117534
\begin{document}
\begin{tikzpicture}
\pgfmathtruncatemacro{\rownum}{6} % Set number of rows here
\pgfmathtruncatemacro{\colnum}{3} % Set number of columns here
\pgfmathtruncatemacro{\rowtemp}{\rownum-1}
\pgfmathtruncatemacro{\coltemp}{\colnum-1}
% Place coordinates in a grid like fashion; randomized
\foreach \i in {0,...,\rownum}
\foreach \j in {0,...,\colnum}
\coordinate[] (n-\i\j) at (\i + 0.45*rand,\j + 0.45*rand) {};
% Draw vertical lines
\foreach \i in {0,...,\rownum}
\foreach \j [count=\jj] in {0,...,\coltemp}{%
\pgfmathparse{random(0,60)-30}
\draw (n-\i\j) to [bend right=\pgfmathresult] (n-\i\jj);
}
% Draw horizontal lines
\foreach \i [count=\ii] in {0,...,\rowtemp}
\foreach \j in {0,...,\colnum}{%
\pgfmathparse{random(0,60)-30}
\draw (n-\i\j) to [bend right=\pgfmathresult] (n-\ii\j);
}
\end{tikzpicture}
\end{document}
增加一点“随机性”
我认为这是我用这种方法能做到的极限。在这里,我使用\ifthenelse{}{}{}
条件语句删除一些水平线,使其较少的网格外观,并使其看起来更有机。请注意下图中并非所有节点都由线连接。
您可以轻松修改下面的代码,删除一些垂直线而不是水平线(不能同时删除两者!)。唯一的问题是,在随机区域会有尖角,我不知道如何使它们变得平滑。
\documentclass[tikz,border=2mm]{standalone}
\usepackage{ifthen}
\pgfmathsetseed{\pdfuniformdeviate 10000000} %Credit: https://tex.stackexchange.com/a/212755/117534
\begin{document}
\begin{tikzpicture}
\pgfmathtruncatemacro{\rownum}{6} % Set number of rows here
\pgfmathtruncatemacro{\colnum}{3} % Set number of columns here
\pgfmathtruncatemacro{\rowtemp}{\rownum-1}
\pgfmathtruncatemacro{\coltemp}{\colnum-1}
% Place coordinates in a grid like fashion; randomized
\foreach \i in {0,...,\rownum}
\foreach \j in {0,...,\colnum}
\coordinate[] (n-\i\j) at (\i + 0.45*rand,\j + 0.45*rand) {};
% Draw vertical lines
\foreach \i in {0,...,\rownum}
\foreach \j [count=\jj] in {0,...,\coltemp}{%
\pgfmathparse{random(0,60)-30}
\draw (n-\i\j) to [bend right=\pgfmathresult] (n-\i\jj);
}
% Draw horizontal lines
\foreach \i [count=\ii] in {0,...,\rowtemp}{%
\foreach \j in {0,...,\colnum}{%
\pgfmathtruncatemacro{\randnum}{random(0,60)-30}
\ifthenelse{\isodd{\randnum/2} \OR \j=0 \OR \j=\colnum}{% <------------------ Ensure end horz lines are drawn
\draw (n-\i\j) to [bend right=\randnum] (n-\ii\j)}{};
}}
\end{tikzpicture}
\end{document}