我正在尝试使用仿射变换生成分形。我需要在区间 (0,1) 内生成一个随机数,并根据此随机数的值选择一个函数来计算我要绘制的点的坐标。
但是编译器报告了一个错误,我不知道如何解决。
这是描述情况的代码。
\documentclass{article}
\usepackage{color}
\usepackage{tikz}
\usepackage{ifthen}
\begin{document}
%
\begin{center}
\begin{tikzpicture}
\draw[help lines] (0,0) grid (5,5);
% Here I generate a random number
\pgfmathparse{rnd}
% I store the random number generated in memory
\pgfmathsetmacro{\aleatorio}{\pgfmathresult}
\ifthenelse{\aleatorio<0.5}{
\node[red] at (5*rnd,5*rnd){\footnotesize\aleatorio};
}{
\node[blue] at (5*rnd,5*rnd){\footnotesize\aleatorio};
}
\end{tikzpicture}
\end{center}
%
\end{document}
\ifthenelse
当涉及到做出决定的命令行时,它会给我一个错误消息。
我想是因为\ifnum
只能比较整数。说实话,我不确定。无论如何,我需要帮助来比较非整数(带小数部分)数字,然后继续...
我感谢您的帮助。
答案1
\ifthenelse{\lengthtest{\aleatorio pt<0.5pt}}{true}{false}
更好的实现是使用 PGF 函数:
\documentclass{article}
\usepackage{color}
\usepackage{tikz}
\usepackage{ifthen}
\begin{document}
\begin{center}
\begin{tikzpicture}
\draw[help lines] (0,0) grid (5,5);
% generate a random number and store it
\pgfmathsetmacro{\aleatorio}{rnd}
\pgfmathsetmacro{\rndcolor}{ \aleatorio<0.5 ? "red" : "blue" }
\node[\rndcolor] at (5*rnd,5*rnd){\footnotesize\aleatorio} ;
\end{tikzpicture}
\begin{tikzpicture}
\draw[help lines] (0,0) grid (5,5);
% generate a random number and store it
\pgfmathsetmacro{\aleatorio}{rnd}
\pgfmathsetmacro{\rndcolor}{ \aleatorio<0.5 ? "red" : "blue" }
\node[\rndcolor] at (5*rnd,5*rnd){\footnotesize\aleatorio} ;
\end{tikzpicture}
\begin{tikzpicture}
\draw[help lines] (0,0) grid (5,5);
% generate a random number and store it
\pgfmathsetmacro{\aleatorio}{rnd}
\pgfmathsetmacro{\rndcolor}{ \aleatorio<0.5 ? "red" : "blue" }
\node[\rndcolor] at (5*rnd,5*rnd){\footnotesize\aleatorio} ;
\end{tikzpicture}
\begin{tikzpicture}
\draw[help lines] (0,0) grid (5,5);
% generate a random number and store it
\pgfmathsetmacro{\aleatorio}{rnd}
\pgfmathsetmacro{\rndcolor}{ \aleatorio<0.5 ? "red" : "blue" }
\node[\rndcolor] at (5*rnd,5*rnd){\footnotesize\aleatorio} ;
\end{tikzpicture}
\end{center}
\end{document}