修改 \foreach 循环以包含嵌套绘图

修改 \foreach 循环以包含嵌套绘图

使用以下代码从答案这个问题;如何在每个尖刺上嵌套圆圈,并且让其颜色与各个图画的颜色相匹配。

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\begin{document}
\begin{frame}[t]
\frametitle{spiro arcs}%Spirifankerln using scale Fractal solution simple-
\definecolor{camel}{rgb}{0.76, 0.6, 0.42}
\definecolor{apricot}{rgb}{0.98, 0.81, 0.69}
\definecolor{burlywood}{rgb}{0.87, 0.72, 0.53}
\definecolor{fawn}{rgb}{0.9, 0.67, 0.44}
\definecolor{lighttaupe}{rgb}{0.7, 0.55, 0.43}
\definecolor{palebrown}{rgb}{0.6, 0.46, 0.33}
\begin{tikzpicture}[pics/spiro/.style={code={
  \draw[line width=.04cm,looseness=1,pic actions]
    (0,-2) node [circle, draw, blue, fill=blue!40!white, scale=0.2]{} arc (180:90:2) arc (270:180:2) arc (360:270:2) arc (90:0:2);
  }
}]
  \foreach \i/\clr in {1/apricot, 3/apricot, 5/apricot, 7/apricot, 9/apricot, 11/apricot, 13/apricot, 15/apricot, 2/burlywood, 6/burlywood, 10/burlywood, 14/burlywood, 12/fawn, 4/fawn, 8/camel, 16/lighttaupe, 0/palebrown}
 {
    \pic[draw/.expanded=\clr!100,fill/.expanded=\clr!60,scale=2,rotate=\i*2.8125]{spiro};
    \pic[draw/.expanded=\clr!100,fill/.expanded=\clr!60,scale=2,rotate=-\i*2.8125]{spiro};
  }
\end{tikzpicture}
\end{frame}
\end{document}

在此处输入图片描述

答案1

您基本上已经准备好了所需的一切。主要问题是您只在每个象限的第一组尖峰之后放置圆圈。要在每个尖峰上都放置圆圈,您需要\node在每个圆弧后添加命令,最后一个圆弧除外。

\clr您可以通过将当前颜色作为参数传递给 spiro pic 来适应颜色的变化。通过这些更改,您的代码会产生:

在此处输入图片描述

由于现在有许多节点,我已将这些节点的样式分离到另一个中dot/.style,最终接收当前颜色\clr作为其#1参数。以下是修改后的代码:

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\begin{document}
\begin{frame}[fragile,t]
\frametitle{spiro arcs}%Spirifankerln using scale Fractal solution simple-
\definecolor{camel}{rgb}{0.76, 0.6, 0.42}
\definecolor{apricot}{rgb}{0.98, 0.81, 0.69}
\definecolor{burlywood}{rgb}{0.87, 0.72, 0.53}
\definecolor{fawn}{rgb}{0.9, 0.67, 0.44}
\definecolor{lighttaupe}{rgb}{0.7, 0.55, 0.43}
\definecolor{palebrown}{rgb}{0.6, 0.46, 0.33}
\begin{tikzpicture}[
  dot/.style={circle, draw=#1, fill=#1!60, scale=0.2},
  pics/spiro/.style={code={
  \draw[line width=.04cm,looseness=1,pic actions]
  (0,-2) node [dot=#1]{} arc (180:90:2) node[dot=#1]{}  arc (270:180:2) node[dot=#1]{}  arc (360:270:2) node[dot=#1]{} arc (90:0:2);
  }
}]
  \foreach \i/\clr in {1/apricot, 3/apricot, 5/apricot, 7/apricot, 9/apricot, 11/apricot, 13/apricot, 15/apricot, 2/burlywood, 6/burlywood, 10/burlywood, 14/burlywood, 12/fawn, 4/fawn, 8/camel, 16/lighttaupe, 0/palebrown}
 {
    \pic[draw=\clr!100,fill=\clr!60,scale=2,rotate=\i*2.8125]{spiro=\clr};
    \pic[draw=\clr!100,fill=\clr!60,scale=2,rotate=-\i*2.8125]{spiro=\clr};
  }
\end{tikzpicture}
\end{frame}
\end{document}

最后,请注意,必须添加fragile环境参数frame,否则您将收到编译错误。我还删除了.expanded处理程序,因为它们不是必需的。您还可以\foreach通过删除\i并计算旋转来简化命令,但就其工作原理而言,这仅具有美学编码价值:)

答案2

有点类似于 Andrew 的答案,但将圆圈作为了 的一部分pic。也就是说,我替换了

(0,-2) node [circle, draw, blue, fill=blue!40!white, scale=0.2]{}

经过

foreach \X in {0,90,180,270} {(\X:2) node [circle, draw, scale=0.2]{}} 

我同意安德鲁的观点,/.expanded这里不需要。

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\begin{document}
\begin{frame}[t]
\frametitle{spiro arcs}%Spirifankerln using scale Fractal solution simple-
\definecolor{camel}{rgb}{0.76, 0.6, 0.42}
\definecolor{apricot}{rgb}{0.98, 0.81, 0.69}
\definecolor{burlywood}{rgb}{0.87, 0.72, 0.53}
\definecolor{fawn}{rgb}{0.9, 0.67, 0.44}
\definecolor{lighttaupe}{rgb}{0.7, 0.55, 0.43}
\definecolor{palebrown}{rgb}{0.6, 0.46, 0.33}
\begin{tikzpicture}[pics/spiro/.style={code={
  \draw[line width=.04cm,looseness=1,pic actions]
    foreach \X in {0,90,180,270} {(\X:2) node [circle, draw, scale=0.2]{}} 
    arc (180:90:2) arc (270:180:2) arc (360:270:2) arc (90:0:2);
  }
}]
  \foreach \i/\clr in {1/apricot, 3/apricot, 5/apricot, 7/apricot, 9/apricot, 11/apricot, 13/apricot, 15/apricot, 2/burlywood, 6/burlywood, 10/burlywood, 14/burlywood, 12/fawn, 4/fawn, 8/camel, 16/lighttaupe, 0/palebrown}
 {
    \pic[draw=\clr!100,fill=\clr!60,scale=2,rotate=\i*2.8125]{spiro};
    \pic[draw=\clr!100,fill=\clr!60,scale=2,rotate=-\i*2.8125]{spiro};
  }
\end{tikzpicture}
\end{frame}
\end{document}

在此处输入图片描述

相关内容