PGF 手册第 1024 页有一小段代码,用于绘制“包含”的随机路径,或者不会偏离起始点太远的随机路径。我复制了部分代码,以便在已经包含随机元素的图片背景上绘制这种曲线;我通过在坐标计算中添加宏\xmax
和修改了代码。结果是这条随机路径总是在离第一张图片很远的地方结束。无论随机路径是否绘制在背景层上,无论中的参数是什么,也无论我在绘制时是否再次调用种子函数,这个结果都会持续存在。这是代码:\ymax
\pgfmathsetseed{<param>}
\documentclass[tikz]{standalone}
\usetikzlibrary{
calc,
math,
backgrounds,
shapes
}
\pgfmathsetseed%{\number\pdfrandomseed}
{1}
\newcommand\RandNum[1]{random(1,#1)/#1}
\definecolor{almond}{rgb}{0.94, 0.87, 0.8}
\begin{document}
\begin{tikzpicture}[scale = 3.5]
\pgfmathsetmacro{\xmax}{5.5}
\pgfmathsetmacro{\ymax}{3.1}
\pgfmathtruncatemacro{\totfigs}{1.5*(\ymax*\xmax)}
\pgfmathsetmacro{\MaxRand}{\xmax*\ymax}
\foreach ~ in {0,0.2, ..., \totfigs}{
\pgfmathsetmacro{\R}{random(3,12)}
\pgfmathsetmacro{\rsizex}{\RandNum{\MaxRand}}
\pgfmathsetmacro{\rsizey}{\RandNum{\MaxRand}}
\pgfmathsetmacro{\rsize}{\RandNum{\MaxRand}}
\pgfmathsetmacro{\rot}{rnd}
\pgfmathsetmacro{\blau}{rnd}
\pgfmathsetmacro{\grun}{rnd}
\definecolor{coul}{rgb}{\rot,\grun,\blau}
\definecolor{pigm}{cmy}{\rot,\grun,\blau}
\fill[
fill = coul,
opacity = \RandNum{8},
] (rnd*\xmax,rnd*\ymax)%
circle [x radius = \rsizex*0.35, y radius = \rsizey*0.25];
\node[
fill = pigm,
regular polygon,
regular polygon sides = \R,
minimum size = \rsize*2 cm,
opacity = \RandNum{4},
rotate = {random(0,27)}
] at (rnd*\xmax,rnd*\ymax) {};
}
\begin{scope}[on background layer, scale = 0.3, x = 10pt, y = 10pt]
\fill[almond] (current bounding box.south west) rectangle (current bounding box.north east);
\pgfmathsetseed{1}
%%%% THIS PIECE OF CODE: FROM HERE
\coordinate (current point) at (0,0);
\coordinate (old velocity) at (current point);
\coordinate (new velocity) at (rand*\xmax,rand*\ymax);
\foreach ~ in {0,...,100}{
\pgfmathsetmacro{\rot}{rnd}
\pgfmathsetmacro{\blau}{rnd}
\pgfmathsetmacro{\grun}{rnd}
\definecolor{col}{rgb}{\rot,\grun,\blau}
\draw[col, thick]
(current point) .. controls ++([scale=-1]old velocity) and ++(new velocity) ..
++(rnd,rnd) coordinate (current point);
\coordinate (old velocity) at (new velocity);
\coordinate (new velocity) at (rand*\xmax,rand*\ymax);
}
%%%%% UP TO HERE
\end{scope}
\end{tikzpicture}
\end{document}
如果我在单独的文档上运行相同的代码(大写注释之间的代码),它会像预期的那样产生“包含”的路径。我附上了这两种行为的图片。然而,如果我从那段代码中删除宏\xmax
并运行它,我还会获得一条“逃逸”的随机路径;也就是说,如果我编译以下代码:\ymax
\documentclass[tikz]{standalone}
\usetikzlibrary{calc, math}
\pgfmathsetseed{\number\pdfrandomseed}
\begin{document}
\pgfmathsetmacro{\xmax}{5.5}
\pgfmathsetmacro{\ymax}{3.1}
\begin{tikzpicture}
\coordinate (current point) at (0,0);
\coordinate (old velocity) at (current point);
\coordinate (new velocity) at (rand,rand);
\foreach ~ in {0,...,100}{
\pgfmathsetmacro{\rot}{rnd}
\pgfmathsetmacro{\blau}{rnd}
\pgfmathsetmacro{\grun}{rnd}
\definecolor{col}{rgb}{\rot,\grun,\blau}
\draw[col, thick]
(current point) .. controls ++([scale=-1]old velocity) and ++(new velocity) ..
++(rnd,rnd) coordinate (current point);
\coordinate (old velocity) at (new velocity);
\coordinate (new velocity) at (rand,rand);
}
\end{tikzpicture}
\end{document}
我得到了第三张图片。我该如何纠正这个问题并每次都得到第二张图片中的路径?谢谢!
答案1
pgf 函数产生一个介于和rnd
之间的随机数,因此您始终沿正 x 和 y 方向前进。您可能需要。0
1
rnd-0.5
\documentclass[tikz]{standalone}
\usetikzlibrary{calc, math}
\pgfmathsetseed{\number\pdfrandomseed}
\begin{document}
\pgfmathsetmacro{\xmax}{5.5}
\pgfmathsetmacro{\ymax}{3.1}
\begin{tikzpicture}
\coordinate (current point) at (0,0);
\coordinate (old velocity) at (current point);
\coordinate (new velocity) at (rand,rand);
\foreach ~ in {0,...,100}{
\pgfmathsetmacro{\rot}{rnd}
\pgfmathsetmacro{\blau}{rnd}
\pgfmathsetmacro{\grun}{rnd}
\definecolor{col}{rgb}{\rot,\grun,\blau}
\draw[col, thick]
(current point) .. controls ++([scale=-1]old velocity) and ++(new velocity) ..
++(rnd-0.5,rnd-0.5) coordinate (current point);
\coordinate (old velocity) at (new velocity);
\coordinate (new velocity) at (rand,rand);
}
\end{tikzpicture}
\end{document}
如果你想把路径挤进一个盒子里,我看不出除了跟踪坐标之外还有其他什么办法。
\documentclass[tikz]{standalone}
\usetikzlibrary{calc, math}
\pgfmathsetseed{\number\pdfrandomseed}
\begin{document}
\pgfmathsetmacro{\xmax}{5.5}
\pgfmathsetmacro{\ymax}{3.1}
\begin{tikzpicture}
\draw (0,0) rectangle (\xmax,\ymax);
\coordinate (current point) at (0,0);
\coordinate (old velocity) at (current point);
\pgfmathsetmacro{\currentx}{rnd}
\pgfmathsetmacro{\currenty}{rnd}
\coordinate (new velocity) at (\currentx,\currenty);
\foreach \X [remember=\currentx as \currentx,remember=\currenty as \currenty]
in {0,...,100}{
\pgfmathsetmacro{\rot}{rnd}
\pgfmathsetmacro{\blau}{rnd}
\pgfmathsetmacro{\grun}{rnd}
\definecolor{col}{rgb}{\rot,\grun,\blau}
\pgfmathsetmacro{\currentx}{max(0,min(\xmax,\currentx+rand))}
\pgfmathsetmacro{\currenty}{max(0,min(\ymax,\currenty+rand))}
\draw[col, thick]
(current point) .. controls ++([scale=-1]old velocity) and ++(new velocity) ..
(\currentx,\currenty) coordinate (current point);
\coordinate (old velocity) at (new velocity);
\coordinate (new velocity) at (rand,rand);
}
\end{tikzpicture}
\end{document}
可以看出,这仍然可能超调。如果要消除超调,还必须跟踪控制点。