有人能解释一下这段代码的某些部分吗?我是 LaTeX 新手,有些部分看不懂。
这\foreach \y in {86,38,15}
意味着什么?
你能详细解释一下这个循环在做什么吗
\foreach \x in {1,...,\y}{ ... %here the dots represent all the code included in the main code below }
?
如果您愿意提供任何帮助,我将不胜感激。谢谢。
% Rooty helix
% Author: Felix Lindemann
\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\pagestyle{empty}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}
\xdefinecolor{darkgreen}{RGB}{175, 193, 36}
\newcounter{cntShader}
\newcounter{cntRoot}
\setcounter{cntShader}{20}
\def\couleur{darkgreen}
\begin{tikzpicture}
\foreach \y in {86,38,15}{
\setcounter{cntShader}{1}
\coordinate (a) at (0,0);
\coordinate (b) at (0:1);
\foreach \x in {1,...,\y}{%
\coordinate (c) at ($ (b)!1cm!270:(a) $);
\begin{pgfonlayer}{background}
\draw[fill=\couleur!\thecntShader] (a)--(b)--(c)--cycle;
\end{pgfonlayer}
\setcounter{cntRoot}{\x}
\addtocounter{cntRoot}{1}
\node[fill=white,draw,circle,inner sep=1pt] at (c)
{$\sqrt{\thecntRoot}$};
\coordinate (b) at (c);
\pgfmathsetcounter{cntShader}{\thecntShader+4}
\setcounter{cntShader}{\thecntShader}
}
}
\node[fill=white,draw,circle,inner sep=1pt] at (0:1) {$\sqrt{1}$};
\end{tikzpicture}
\end{document}
答案1
这里有一个建议,以更易于理解的方式重写代码。它是否真的更易于理解可能取决于谁在看它。无论如何,此代码只有一个循环而不是两个循环。它使用decorations.marking
(当然内部也使用循环)来放置节点。从我对代码的理解来看,计算算法用于使圆之间的距离大致相等,但这decorations.markings
是免费的。阴影是在背景层上完成的,如上面的代码所示,但在单个循环中。特别是,如果您想要不同的最大值或不同的螺旋,我认为下面的方法将更容易调整。
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations.markings}
\begin{document}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}
\xdefinecolor{darkgreen}{RGB}{175, 193, 36}
\begin{tikzpicture}
\draw[decorate,decoration={markings,
mark=between positions 0 and 1 step 1/86 with {%
\node[fill=white,circle,draw,inner sep=1pt]
(n-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}){%
$\sqrt{\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}}$};}}]
plot[variable=\x,domain=45:945,samples=86]
(\x:{0.5+pow(\x/66,4/5)});
\begin{pgfonlayer}{background}
\foreach \X [remember=\X as \LastX,evaluate=\X as \Y using
{int(3*\X+255-3*87)}] in {87,86,...,1}
{\ifnum\X=87
\else
\draw[fill=darkgreen!\Y] (n-\LastX.center) -- (n-\X.center) -- (0,0) -- cycle;
\fi}
\end{pgfonlayer}
\end{tikzpicture}
\end{document}
也可以一次完成所有操作,即通过一个循环有效地完成。
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations.markings}
\begin{document}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}
\xdefinecolor{darkgreen}{RGB}{175, 193, 36}
\begin{tikzpicture}
\pgfmathtruncatemacro{\xmax}{87}
\pgfmathtruncatemacro{\xnmax}{\xmax-1}
\coordinate (O) at (0,0);
\draw[decorate,decoration={markings,
mark=between positions 0 and 1 step 1/\xnmax with {%
\pgfmathtruncatemacro{\myx}{1+\xmax-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}}
\pgfmathtruncatemacro{\myy}{\myx+1}
\pgfmathtruncatemacro{\myz}{3*\myx+255-3*\xmax}
\node[fill=white,circle,draw,inner sep=1pt]
(n-\myx){%
$\sqrt{\myx}$};
\ifnum\myx=\xmax
\else
\begin{pgfonlayer}{background}
\draw[fill=darkgreen!\myz] (n-\myx.center) -- (n-\myy.center) -- (O) -- cycle;
\end{pgfonlayer}
\fi
}}]
plot[variable=\x,domain=930:0,samples=\xmax]
(\x:{0.5+pow(\x/66,4/5)+ifthenelse(\x<0.1,0.3,0)});
\end{tikzpicture}
\end{document}
如果要绘制更多或更少的根,则需要更改\xmax
并调整域,即domain=930:0
用其他内容替换。由于半径增大,域的上限(930
在本例中)和之间的关系\xmax
不是线性的。相反,存在一个经验定律(我通过积分弧长并拟合一个函数“推导”出该定律,该定律表明域的上限类似于pow(\xmax/87,0.65)*930
,其中\xmax
是最大数字,即上述示例中的 87。
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations.markings}
\begin{document}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}
\xdefinecolor{darkgreen}{RGB}{175, 193, 36}
\foreach \X in {10,15,...,100}
{\begin{tikzpicture}[declare
function={Rad(\x)=1+pow(\x/66,4/5)+ifthenelse(\x<1,0.3,0);}]
\path[use as bounding box] (-12,-10.5) rectangle (12,9.5);
\pgfmathtruncatemacro{\xmax}{\X}
\pgfmathtruncatemacro{\xnmax}{\xmax-1}
\pgfmathsetmacro{\Xmax}{pow(\xmax/87,0.65)*930}
\coordinate (O) at (0,0);
\draw[decorate,decoration={markings,
mark=between positions 0 and 1 step 1/\xnmax with {%
\pgfmathtruncatemacro{\myx}{1+\xmax-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}}
\pgfmathtruncatemacro{\myy}{\myx+1}
\pgfmathtruncatemacro{\myz}{3*\myx+255-3*\xmax}
\node[fill=white,circle,draw,inner sep=1pt]
(n-\myx){%
$\sqrt{\myx}$};
\ifnum\myx=\xmax
\else
\begin{pgfonlayer}{background}
\draw[fill=darkgreen!\myz] (n-\myx.center) -- (n-\myy.center) -- (O) -- cycle;
\end{pgfonlayer}
\fi
}}]
plot[variable=\x,domain=\Xmax:0,samples=\xmax]
(\x:{Rad(\x)});
\end{tikzpicture}}
\end{document}