draw a complex shape with "for each"

You draw dotted circles to guide you

\documentclass[tikz,border=9]{standalone}

\begin{document}

\tikz{
    \draw[dotted]
        (0,0)foreach\r in{1,3,...,9}{circle(\r)}
        (0,2)foreach\r in{1,3,...,9}{circle(\r)};
    \draw[red]
        foreach\r in{1,3,...,9}{(0,-\r)arc(-90:90:\r)}
        foreach\r in{1,3,...,9}{(0,\r+2)arc(90:270:\r)}
    ;
}

\end{document}


To answer your question about the foreach loop literally, one could post

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}
\def\R{3}
\draw (0:\R) \foreach \X in {0,...,3}{
            --(\X*90:\R)
            -- ++ (135+\X*90:{\R/2})
            foreach \Y in {1,...,5}
            {arc[start angle=-135+\Y*90+\X*90,end angle=-45+\Y*90+\X*90,
                radius=\R/5-\Y*\R/60]}
            arc[start angle=-135+6*90+\X*90,end angle=-45+7*90+\X*90,
                radius=\R/60]   
            foreach \Y in {5,...,0}
            {arc[start angle=-45+\Y*90+\X*90,end angle=-135+\Y*90+\X*90,
                radius=\R/5-\Y*\R/60-\R/30]}
            arc[start angle=-135+\X*90,end angle=-225+\X*90,
                radius=\R/5-\R/60]              
        } -- cycle;
\end{tikzpicture}
\end{document}

enter image description here

However, for repeating tasks like this I prefer to use pics. This is in particular the case if you want to highlight some reflection symmetries because these pics can be transformed. That is, in order to have something that respects some Euclidean transformations, you just need to act on this with these transformations until you reach the original object. This here is the conjugacy class of the group element "rotation by 90 followed by reflection about the y axis", i.e. a Z_4 subgroup of the Euclidean group.

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}[pics/spiral/.style={code={
 \tikzset{spiral/.cd,#1}
 \def\pv##1{\pgfkeysvalueof{/tikz/spiral/##1}}
  \draw (0:{\pv{R}}) coordinate (-a) --
    plot[variable=\t,domain=0:\pv{tmax},samples=61,smooth]
    (\t:{\pv{r}-\pv{r}*abs(\t/\pv{trel})})
    -- ++({abs(\pv{tmax})+90}:1pt) \pgfextra{\pgfmathsetmacro{\myan}{abs(\pv{tmax})+90}}
    to[out=\myan,in=\myan]
   (\pv{tmax}:{\pv{r}+\pv{dr}-\pv{r}*abs(\pv{tmax}/\pv{trel})}) --
   plot[variable=\t,domain=\pv{tmax}:-180,samples=61,smooth]
   (\t:{\pv{r}+\pv{dr}-\pv{r}*abs(\t/\pv{trel})})
   -- (-180:\pv{R}) coordinate (-b);
  }},
  spiral/.cd,tmax/.initial=-630,trel/.initial=450,dr/.initial=0.1,
  r/.initial=1,R/.initial=2]
 \begin{scope}[rotate=45,transform shape,line cap=rect,thick,
    spiral/.cd,r=0.6,trel=720,R=1.5]
  \path (90:1.5) pic{spiral} 
    (180:1.5) pic[rotate=90,xscale=-1]{spiral}
    (270:1.5) pic[rotate=180,scale=-1]{spiral}
    (0:1.5) pic[rotate=270,yscale=-1]{spiral};  
 \end{scope}
 \draw[red] (-90:3) -- (90:3) node[pos=1.05] {$\mathsf{a}$}
  (45:3) -- (-135:3) node[pos=1.05] {$\mathsf{c}$};
 \draw[green!60!black] (180:3) -- (0:3) node[pos=1.05] {$\mathsf{b}$}
  (-45:3) -- (135:3) node[pos=1.05] {$\mathsf{d}$};
 \node[fill,circle,inner sep=2pt,label=below left:$O$] {};
\end{tikzpicture}
\end{document}

enter image description here

Tags:

Tikz Pgf