将 Spirograph 代码与 \foreach 循环相结合

将 Spirograph 代码与 \foreach 循环相结合

我使用了以下代码(没有起作用)

\begin{frame}[t]
\frametitle{not working}
\begin{center}
\vskip -.8cm
\begin{tikzpicture}
\draw[line width=.04cm,looseness=1]
  \foreach \i/\clr in {3/orange,1/orange,2/brown,4/red,0/purple} {
    \pic[draw/.expanded=\clr!100,fill/.expanded=\clr!40,scale=2,rotate=\i*11.25]{spiro};
    \pic[draw/.expanded=\clr!100,fill/.expanded=\clr!40,scale=2,rotate=-\i*11.25]{spiro};
}
\end{tikzpicture}
\end{center}
\end{frame}

尝试将 Spirograph 代码与答案结合起来 这个问题\foreach loop 从答案这个问题

我的试用代码有什么错误?

\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{Spiropraph mine2}
\begin{center}
\vskip -.8cm
\begin{tikzpicture}[]
\draw%the angle of rotation is caclulated as 360/4(number of spikes in the drawing)/8(number of rotations wanted)*(order of rotation) 360/4/8=11.25
(0,0) pic[scale=0.75, orange, line width=0.6mm, rotate=360/4/8*3, fill=orange!40]{spiro={R=6,r=-1.5,p=1.52}}
(0,0) pic[scale=0.75, orange, line width=0.6mm, rotate=-360/4/8*3, fill=orange!40]{spiro={R=6,r=-1.5,p=1.52}}
(0,0) pic[scale=0.75, orange, line width=0.6mm, rotate=360/4/8*1, fill=orange!40]{spiro={R=6,r=-1.5,p=1.52}}
(0,0) pic[scale=0.75, orange, line width=0.6mm, rotate=-360/4/8*1, fill=orange!40]{spiro={R=6,r=-1.5,p=1.52}}
(0,0) pic[scale=0.75, brown, line width=0.6mm, rotate=360/4/8*2, fill=brown!40]{spiro={R=6,r=-1.5,p=1.52}}
(0,0) pic[scale=0.75, brown, line width=0.6mm, rotate=-360/4/8*2, fill=brown!40]{spiro={R=6,r=-1.5,p=1.52}}
(0,0) pic[scale=0.75, red, line width=0.6mm, rotate=360/4/8*4, fill=red!40]{spiro={R=6,r=-1.5,p=1.52}}
(0,0) pic[scale=0.75, purple, line width=0.6mm, rotate=360/4/8*0, fill=purple!40]{spiro={R=6,r=-1.5,p=1.52}}
;
\end{tikzpicture}
\end{center}
\end{frame}
\begin{frame}[t]
\frametitle{Spirifankerln using scale Fractal solution simple}
\begin{tikzpicture}[pics/spiro/.style={code={
 \draw[line width=.04cm,looseness=1,pic actions]
 (0,-2) arc (180:90:2) arc (270:180:2) arc (360:270:2) arc (90:0:2);
}}]
  \foreach \i/\clr in {3/orange,1/orange,2/brown,4/red,0/purple} {
    \pic[draw/.expanded=\clr!100,fill/.expanded=\clr!40,scale=2,rotate=\i*11.25]{spiro};
    \pic[draw/.expanded=\clr!100,fill/.expanded=\clr!40,scale=2,rotate=-\i*11.25]{spiro};
  }
\end{tikzpicture}
\end{frame}
\end{document}

在此处输入图片描述

答案1

正如@Cicada所指出的,

\draw[line width=.04cm,looseness=1]

不是完整的路径。但是,我认为您不想将其设为空路径,而是希望将这些选项应用于图片。至少有两种可能性:

  1. 将选项添加到 的选项中tikzpicture
  2. foreach删除和s前面的反斜杠pic,并删除;并在组合路径末尾添加一个。

显式代码:

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\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{document}
\begin{frame}[t]
\frametitle{now working}
\begin{center}
\begin{tikzpicture}
\draw[line width=.04cm,looseness=1]
  foreach \i/\clr in {3/orange,1/orange,2/brown,4/red,0/purple} {
    pic[draw/.expanded=\clr!100,fill/.expanded=\clr!40,scale=2/3,rotate=\i*11.25]{spiro}
    pic[draw/.expanded=\clr!100,fill/.expanded=\clr!40,scale=2/3,rotate=-\i*11.25]{spiro}
};
\end{tikzpicture}
\end{center}
\end{frame}
\begin{frame}[t] 
\frametitle{now working} 
\begin{center}
\begin{tikzpicture}[line width=.04cm,looseness=1]
 \foreach \i/\clr in {3/orange,1/orange,2/brown,4/red,0/purple} 
 { \pic[draw/.expanded=\clr!100,fill/.expanded=\clr!40,scale=2/3,rotate=\i*11.25]{spiro}; 
 \pic[draw/.expanded=\clr!100,fill/.expanded=\clr!40,scale=2/3,rotate=-\i*11.25]{spiro}; } 
\end{tikzpicture} 
\end{center} 
\end{frame}
\end{document}

在此处输入图片描述

相关内容