![使用 \foreach 循环绘制螺旋图案](https://linux22.com/image/406692/%E4%BD%BF%E7%94%A8%20%5Cforeach%20%E5%BE%AA%E7%8E%AF%E7%BB%98%E5%88%B6%E8%9E%BA%E6%97%8B%E5%9B%BE%E6%A1%88.png)
我使用了以下代码来回答 这个问题,绘制螺旋形图案
\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\begin{document}
\tikzset{pics/spiro/.style={code={
\tikzset{spiro/.cd,#1}
\def\pv##1{\pgfkeysvalueof{/tikz/spiro/##1}}
\draw[trig format=rad,pic actions]
plot[variable=\t,domain=0:2*pi*\pv{nRotations}, samples=90*\pv{nRotations}+1, smooth cycle]
(
{(\pv{R}+\pv{r})*cos(\t)+\pv{p}*cos((\pv{R}+\pv{r})*\t/\pv{r})},
{(\pv{R}+\pv{r})*sin(\t)+\pv{p}*sin((\pv{R}+\pv{r})*\t/\pv{r})}
);
}},
spiro/.cd,R/.initial=6,r/.initial=-1.5,p/.initial=1,nRotations/.initial=1}
\begin{frame}[t]
\frametitle{}
\begin{center}
\begin{tikzpicture}[line width=.2mm]
\foreach \i/\clr in {5/blue,10/blue,15/blue,20/blue,25/blue,30/blue,35/blue,40/blue,45/blue,50/green,55/green,60/green,65/green,70/green,75/green,80/green,85/green,90/green,95/orange,100/orange,105/orange,110/orange,115/orange,120/orange,125/orange,130/orange,135/orange,140/purple,145/purple,150/purple,155/purple,160/purple,165/purple,170/purple,175/purple,180/purple}
{
(0,0) \pic[draw=\clr,rotate=\i,scale=.4]{spiro={R=10.5,r=-5.25,p=3,nRotations=1}};
}
\end{tikzpicture}
\end{center}
\end{frame}
\end{document}
我尝试使用以下代码来简化重复每个旋转角度,但有些不对劲!
\begin{frame}[t]
\frametitle{}
\begin{center}
\begin{tikzpicture}[line width=.2mm]
\foreach \b in {5,10,...,45}
\foreach \g in {50,55,...,90}
\foreach \o in {95,100,...,135}
\foreach \p in {140,145,...,180}
\foreach \i/\clr in {\b/blue,\g/green,\o/orange,\p/purple}
{
(0,0) \pic[draw=\clr,rotate=\i,scale=.4]{spiro={R=10.5,r=-5.25,p=3,nRotations=1}};
}
\end{tikzpicture}
\end{center}
\end{frame}
我也尝试将答案中的代码应用到这个问题以避免最后的图案与旧图案重叠以产生以下图形(使用选项 fill=\clr!40)但我不知道如何应用它。
答案1
你可能想要这样的东西:
\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\begin{document}
\tikzset{pics/spiro/.style={code={
\tikzset{spiro/.cd,#1}
\def\pv##1{\pgfkeysvalueof{/tikz/spiro/##1}}
\draw[trig format=rad,pic actions]
plot[variable=\t,domain=0:2*pi*\pv{nRotations}, samples=90*\pv{nRotations}+1, smooth cycle]
(
{(\pv{R}+\pv{r})*cos(\t)+\pv{p}*cos((\pv{R}+\pv{r})*\t/\pv{r})},
{(\pv{R}+\pv{r})*sin(\t)+\pv{p}*sin((\pv{R}+\pv{r})*\t/\pv{r})}
);
}},
spiro/.cd,R/.initial=6,r/.initial=-1.5,p/.initial=1,nRotations/.initial=1}
\begin{frame}[t]
\frametitle{}
\begin{center}
\begin{tikzpicture}[line width=.2mm]
\path foreach \clr [count=\X starting from 0] in {blue,green,orange,purple}
{foreach \Y in {1,...,9}
{(0,0) pic[draw=\clr,rotate=45*\X+5*\Y,scale=.4]{spiro={R=10.5,r=-5.25,p=3,nRotations=1}}
}};
\end{tikzpicture}
\end{center}
\end{frame}
\end{document}
这将循环遍历颜色,并为每个颜色绘制 9 个版本的图形,每个版本相对旋转 5 度。还请注意,(0,0)
在您的代码中没有效果,我稍微更改了foreach
路径中的 s。
人们可能还想在颜色之间进行插值。
\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\begin{document}
\tikzset{pics/spiro/.style={code={
\tikzset{spiro/.cd,#1}
\def\pv##1{\pgfkeysvalueof{/tikz/spiro/##1}}
\draw[trig format=rad,pic actions]
plot[variable=\t,domain=0:2*pi*\pv{nRotations}, samples=90*\pv{nRotations}+1, smooth cycle]
(
{(\pv{R}+\pv{r})*cos(\t)+\pv{p}*cos((\pv{R}+\pv{r})*\t/\pv{r})},
{(\pv{R}+\pv{r})*sin(\t)+\pv{p}*sin((\pv{R}+\pv{r})*\t/\pv{r})}
);
}},
spiro/.cd,R/.initial=6,r/.initial=-1.5,p/.initial=1,nRotations/.initial=1}
\begin{frame}[t]
\frametitle{}
\begin{center}
\begin{tikzpicture}[line width=.2mm]
\path foreach \clr [count=\X starting from 0,
remember=\clr as \lastclr (initially purple)] in {blue,green,orange,purple}
{foreach \Y [evaluate=\Y as \mycf using {int(100*\Y/9)}] in {1,...,9}
{(0,0) pic[draw=\clr!\mycf!\lastclr,rotate=45*\X+5*\Y-22.5,scale=.4]{spiro={R=10.5,r=-5.25,p=3,nRotations=1}}
}};
\end{tikzpicture}
\end{center}
\end{frame}
\end{document}
附录:您还可以生成填充图。但是,在这种情况下,您的定义可能不是最佳的。所以我改变了这一点。
\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\begin{document}
\tikzset{pics/spiro/.style={code={
\tikzset{spiro/.cd,#1}
\def\pv##1{\pgfkeysvalueof{/tikz/spiro/##1}}
\draw[trig format=rad,pic actions]
plot[variable=\t,domain=pi/2:3*pi/2, samples=31, smooth]
(
{(\pv{R}+\pv{r})*cos(\t)+\pv{p}*cos((\pv{R}+\pv{r})*\t/\pv{r})},
{(\pv{R}+\pv{r})*sin(\t)+\pv{p}*sin((\pv{R}+\pv{r})*\t/\pv{r})}
);
}},
spiro path/.code={\def\pv##1{\pgfkeysvalueof{/tikz/spiro/##1}}
\tikzset{insert path={
plot[trig format=rad,variable=\t,domain=pi/2:3*pi/2, samples=31,
smooth,domain=pi:pi/2]
(
{(\pv{R}+\pv{r})*cos(\t)+\pv{p}*cos((\pv{R}+\pv{r})*\t/\pv{r})},
{(\pv{R}+\pv{r})*sin(\t)+\pv{p}*sin((\pv{R}+\pv{r})*\t/\pv{r})}
)
|- (\pv{R}+\pv{r}+\pv{p},-\pv{R}-\pv{r}-\pv{p})
--
(\pv{R}+\pv{r}+\pv{p},\pv{R}+\pv{r}+\pv{p}) --
(-\pv{R}-\pv{r}-\pv{p},\pv{R}+\pv{r}+\pv{p})
-- cycle
}}},
spiro/.cd,R/.initial=6,r/.initial=-1.5,p/.initial=1}
\begin{frame}[t]
\frametitle{}
\begin{center}
\begin{tikzpicture}[line width=.2mm,spiro/.cd,R=10.5,r=-5.25,p=3]
\begin{scope}
\foreach\Z in {0,1}
{\foreach \clr [count=\X starting from 0] in {blue,green,orange,purple}
{\foreach \Y in {1,...,9}
{\ifnum\Z\X\Y=102
\clip[scale=.4,rotate=5,spiro path];
\fi
\pic[draw=\clr,rotate=45*\X+5*\Y+\Z*180,scale=.4,fill=\clr!40]{spiro};
}}}
\end{scope}
\fill[white] circle[radius={0.4*(\pgfkeysvalueof{/tikz/spiro/R}+
\pgfkeysvalueof{/tikz/spiro/r}-\pgfkeysvalueof{/tikz/spiro/p})}];
\end{tikzpicture}
\end{center}
\end{frame}
\end{document}
在我看来,通过颜色插值它看起来并不太糟糕。
\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\begin{document}
\tikzset{pics/spiro/.style={code={
\tikzset{spiro/.cd,#1}
\def\pv##1{\pgfkeysvalueof{/tikz/spiro/##1}}
\draw[trig format=rad,pic actions]
plot[variable=\t,domain=pi/2:3*pi/2, samples=31, smooth]
(
{(\pv{R}+\pv{r})*cos(\t)+\pv{p}*cos((\pv{R}+\pv{r})*\t/\pv{r})},
{(\pv{R}+\pv{r})*sin(\t)+\pv{p}*sin((\pv{R}+\pv{r})*\t/\pv{r})}
);
}},
spiro path/.code={\def\pv##1{\pgfkeysvalueof{/tikz/spiro/##1}}
\tikzset{insert path={
plot[trig format=rad,variable=\t,domain=pi/2:3*pi/2, samples=31,
smooth,domain=pi:pi/2]
(
{(\pv{R}+\pv{r})*cos(\t)+\pv{p}*cos((\pv{R}+\pv{r})*\t/\pv{r})},
{(\pv{R}+\pv{r})*sin(\t)+\pv{p}*sin((\pv{R}+\pv{r})*\t/\pv{r})}
)
|- (\pv{R}+\pv{r}+\pv{p},-\pv{R}-\pv{r}-\pv{p})
--
(\pv{R}+\pv{r}+\pv{p},\pv{R}+\pv{r}+\pv{p}) --
(-\pv{R}-\pv{r}-\pv{p},\pv{R}+\pv{r}+\pv{p})
-- cycle
}}},
spiro/.cd,R/.initial=6,r/.initial=-1.5,p/.initial=1}
\begin{frame}[t]
\frametitle{}
\begin{center}
\begin{tikzpicture}[line width=.2mm,spiro/.cd,R=10.5,r=-5.25,p=3]
\foreach\Z in {0,1}
{\foreach \clr [count=\X starting from 0,
remember=\clr as \lastclr (initially purple)] in {blue,green,orange,purple}
{\foreach \Y [evaluate=\Y as \mycf using {int(100*\Y/9)}] in {1,...,9}
{\ifnum\Z\X\Y=102
\clip[scale=.4,rotate=5,spiro path];
\fi
\pic[draw=\clr!\mycf!\lastclr,rotate=45*\X+5*\Y+\Z*180,scale=.4,fill=\clr!\mycf!\lastclr!40]{spiro};
}}}
\end{tikzpicture}
\end{center}
\end{frame}
\end{document}