Vereinfachen mehrerer \foreach-Schleifen

Vereinfachen mehrerer \foreach-Schleifen

Ich habe den folgenden Code aus der Antwort aufdiese Frage, (mithilfe einer Schleifenmethode), um eine konzentrische Figur zu zeichnen.

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\begin{document}
\begin{frame}[t]
\frametitle{Spiro using loop}%rounded corners solution 1
\vskip -.6cm
\begin{tikzpicture}
\begin{scope}[scale=2]%transparency group,opacity=.4, 
\draw[line width=.2cm,blue!80!black, fill=blue, looseness=1] 
(0,-2.2) foreach \X in {0,90,180,270}
{[rotate=\X] -- (0,-2.2) to [out=0,in=-120] ++ (0.2,0.1) to [out=60,in=-150] ++ (1.9,1.9) to [out=30,in=-90] ++ (0.1,0.2)} -- cycle;
\end{scope}  
\begin{scope}[scale=2]
\draw[line width=.2cm,blue, fill=blue!80!white, looseness=1] 
(0,-2) foreach \X in {0,90,180,270}
{[rotate=\X] -- (0,-2) to [out=0,in=-120] ++ (0.2,0.1) to [out=60,in=-150] ++ (1.7,1.7) to [out=30,in=-90] ++ (0.1,0.2)} -- cycle;
\end{scope}  
\begin{scope}[scale=2]
\draw[line width=.2cm,blue!80!white, fill=blue!70!white, looseness=1] 
(0,-1.8) foreach \X in {0,90,180,270}
{[rotate=\X] -- (0,-1.8) to [out=0,in=-120] ++ (0.2,0.1) to [out=60,in=-150] ++ (1.5,1.5) to [out=30,in=-90] ++ (0.1,0.2)} -- cycle;
\end{scope}  
\begin{scope}[scale=2]
\draw[line width=.2cm,blue!70!white, fill=blue!60!white, looseness=1] 
(0,-1.6) foreach \X in {0,90,180,270}
{[rotate=\X] -- (0,-1.6) to [out=0,in=-120] ++ (0.2,0.1) to [out=60,in=-150] ++ (1.3,1.3) to [out=30,in=-90] ++ (0.1,0.2)} -- cycle;
\end{scope}  
\begin{scope}[scale=2]
\draw[line width=.2cm,blue!60!white, fill=blue!50!white, looseness=1] 
(0,-1.4) foreach \X in {0,90,180,270}
{[rotate=\X] -- (0,-1.4) to [out=0,in=-120] ++ (0.2,0.1) to [out=60,in=-150] ++ (1.1,1.1) to [out=30,in=-90] ++ (0.1,0.2)} -- cycle;
\end{scope}  
\begin{scope}[scale=2]
\draw[line width=.2cm,blue!50!white, fill=blue!40!white, looseness=1] 
(0,-1.2) foreach \X in {0,90,180,270}
{[rotate=\X] -- (0,-1.2) to [out=0,in=-120] ++ (0.2,0.1) to [out=60,in=-150] ++ (.9,.9) to [out=30,in=-90] ++ (0.1,0.2)} -- cycle;
\end{scope}  
\begin{scope}[scale=2]
\draw[line width=.2cm,blue!40!white, fill=blue!30!white, looseness=1] 
(0,-1.) foreach \X in {0,90,180,270}
{[rotate=\X] -- (0,-1.) to [out=0,in=-120] ++ (0.2,0.1) to [out=60,in=-150] ++ (.7,.7) to [out=30,in=-90] ++ (0.1,0.2)} -- cycle;
\end{scope}  
\begin{scope}[scale=2]
\draw[line width=.2cm,blue!30!white, fill=blue!20!white, looseness=1] 
(0,-.8) foreach \X in {0,90,180,270}
{[rotate=\X] -- (0,-.8) to [out=0,in=-120] ++ (0.2,0.1) to [out=60,in=-150] ++ (.5,.5) to [out=30,in=-90] ++ (0.1,0.2)} -- cycle;
\end{scope}  
\begin{scope}[scale=2]
\draw[line width=.2cm,blue!20!white, fill=blue!10!white, looseness=1] 
(0,-.6) foreach \X in {0,90,180,270}
{[rotate=\X] -- (0,-.6) to [out=0,in=-120] ++ (0.2,0.1) to [out=60,in=-150] ++ (.3,.3) to [out=30,in=-90] ++ (0.1,0.2)} -- cycle;
\end{scope}  
\begin{scope}[scale=2]
\draw[line width=.2cm,blue!10!white, fill=blue!0!white, looseness=1] 
(0,-.4) foreach \X in {0,90,180,270}
{[rotate=\X] -- (0,-.4) to [out=0,in=-120] ++ (0.2,0.1) to [out=60,in=-150] ++ (.1,.1) to [out=30,in=-90] ++ (0.1,0.2)} -- cycle;
\end{scope}  
\end{tikzpicture}
\end{frame}
\end{document}

Bildbeschreibung hier eingeben

So vereinfachen Sie diesen Code.

Antwort1

Diesen Effekt kannst du auch mit Skalieren erzielen. Es entspricht zwar nicht ganz deinem Bild, aber das ist Absicht, denn ich glaube, die Bögen der effektiven Ecken sollen auch skaliert werden.

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\begin{document}
\begin{frame}[t]
\frametitle{Spirifankerln}
\begin{tikzpicture}[pics/spiro/.style={code={
 \draw[line width=.1cm,looseness=1,pic actions]
  (0,-2) foreach \X in {0,90,180,270}
  {[rotate=\X] -- (0,-2) to[out=0,in=-120] ++ (0.2,0.1) to[out=60,in=-150] ++ (1.7,1.7)
  to[out=30,in=-90] ++ (0.1,0.2)} -- cycle;}}]
\foreach \X in {100,90,...,0}
{\pic[draw=blue!\X,fill=blue!\X,scale={1.8*(\X+10)/100}]{spiro};}
\end{tikzpicture}
\end{frame}
\end{document}

Bildbeschreibung hier eingeben

Antwort2

Suchen Sie das Muster und verwenden Sie verschachtelte Schleifen.

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\begin{document}
\begin{frame}[t]
\frametitle{Spiro using loop}
\vskip -.6cm
\begin{tikzpicture}
  \foreach \i in {0,.2,.4,...,1.8} {
    \pgfmathsetmacro\firstcolor{scalar(100-\i*50)}
    \pgfmathsetmacro\secondcolor{scalar(90-\i*50)}
    \begin{scope}[scale=2]
      \draw [
        line width=.2cm,color/.expanded=blue!\firstcolor!white,
        fill/.expanded=blue!\secondcolor!white, looseness=1
      ] (0,-2.2+\i) foreach \X in {0,90,180,270} {
        [rotate=\X] -- (0,-2.2+\i) to [out=0,in=-120] ++ (0.2,0.1) to 
        [out=60,in=-150] ++ (1.9-\i,1.9-\i) to [out=30,in=-90] ++ (0.1,0.2)
      } -- cycle;
    \end{scope}
  }
\end{tikzpicture}
\end{frame}
\end{document}

Bildbeschreibung hier eingeben

verwandte Informationen