# How to use loop with arc in tikz appropriately?

This is my code, as you can see it is pretty simple.

But I know it can be written way more easily with the appropriate use of \for.

Unfortunately, I am still learning and can't seem to make it work with arc. My main concern is with the origin of the arc, it should go around somehow...

\documentclass[letterpaper, 12pt]{article}
\usepackage{tikz}
\begin{document}

\begin{center}
\begin{tikzpicture}[yscale=-1]

\foreach \a in {0, 5,...,45}
\draw[Azure4] (\a:6.7) -- (\a:7);
\draw[very thick] (7,0) arc (0:45:7cm);
\foreach \a in {90, 95,...,135}
\draw[Azure4] (\a:6.7) -- (\a:7);
\draw[very thick] (0,7) arc (90:135:7cm);
\foreach \a in {180, 185,...,225}
\draw[Azure4] (\a:6.7) -- (\a:7);
\draw[very thick] (-7,0) arc (180:225:7cm);
\foreach \a in {270, 275,...,315}
\draw[Azure4] (\a:6.7) -- (\a:7);
\draw[very thick] (0,-7) arc (270:315:7cm);

\foreach \a in {45, 50,...,90}
\draw[Azure4] (\a:4.7) -- (\a:5);
\draw[very thick] (0,5) arc (90:45:5cm);
\foreach \a in {135, 140,...,180}
\draw[Azure4] (\a:4.7) -- (\a:5);
\draw[very thick] (-5,0) arc (180:135:5cm);
\foreach \a in {225, 230,...,270}
\draw[Azure4] (\a:4.7) -- (\a:5);
\draw[very thick] (0,-5) arc (270:225:5cm);
\foreach \a in {315, 320,...,359}
\draw[Azure4] (\a:4.7) -- (\a:5);
\draw[very thick] (5,0) arc (360:315:5cm);

%Main rays
\foreach \a in {0, 45,...,359}
\draw[very thick] (\a:0) -- (\a:7);

%Main rays
\foreach \a in {0, 90,...,359}
\draw[very thick] (0, 0) -- (\a:7);

%Central point
\draw[fill=black] (0,0) circle(0.7mm);
\end{tikzpicture}
\end{center}

\end{document}


So I already can simplify some stuff, but I am still not able to simplify the arc part.

\documentclass[letterpaper, 12pt]{article}
\usepackage{tikz}
\begin{document}

\begin{center}
\begin{tikzpicture}[yscale=-1]

\foreach \a in {0, 5,...,45, 90, 95,...,135, 180, 185,...,225, 270, 275,...,315}
\draw[Azure4] (\a:6.7) -- (\a:7);

\draw[very thick] (7,0) arc (0:45:7cm);

\draw[very thick] (0,7) arc (90:135:7cm);

\draw[very thick] (-7,0) arc (180:225:7cm);

\draw[very thick] (0,-7) arc (270:315:7cm);

\foreach \a in {45, 50,...,90, 135, 140,...,180, 225, 230,...,270, 315, 320,...,359}
\draw[Azure4] (\a:4.7) -- (\a:5);
\draw[very thick] (0,5) arc (90:45:5cm);

\draw[very thick] (-5,0) arc (180:135:5cm);

\draw[very thick] (0,-5) arc (270:225:5cm);

\draw[very thick] (5,0) arc (360:315:5cm);

%Main rays
\foreach \a in {0, 45,...,359}
\draw[very thick] (\a:0) -- (\a:7);

%Main rays
\foreach \a in {0, 90,...,359}
\draw[very thick] (0, 0) -- (\a:7);

%Central point
\draw[fill=black] (0,0) circle(0.7mm);
\end{tikzpicture}
\end{center}

\end{document}


Mainly because of the origin, I know where it goes but don't know how to implement it in a for loop. I am able to do the math necessary for the (beginAngle:endAngle:5cm) though... :)

As center is (0,0), here is a simple solution:

\documentclass{standalone}
\usepackage{tikz}
\colorlet{tickcol}{cyan!50!blue}
\begin{document}
\begin{tikzpicture}[yscale=-1]
0/45/7/orange,
45/90/5/violet,
90/135/7/cyan,
135/180/5/blue,
180/225/7/green,
225/270/5/red,
270/315/7/magenta,
315/360/5/lime%
}{
% sector
\draw[very thick,fill=\fillcol!50]
\pgfmathsetmacro\nextas{\as+5}
% ticks
\foreach \a in {\as,\nextas,...,\ae} {
}
}
% center
\draw[fill=red] (0,0) circle(1mm);
\end{tikzpicture}
\end{document}


• where is the definition for \as and \ae ? – Sebastien Comtois Mar 4 '16 at 12:33
• @SebastienComtois The \foreach loop uses four variables : \as, \ae, \radius and \fillcol. – Paul Gaborit Mar 4 '16 at 13:32
• oooh, I see now. Let's take the first one for example; 0/45/7/orange is \as \ae \radius \fillcolor. By the way it's really neet with the color (I did that on my side too, but your's are somehow transparent: which is nicer) -> I'll implement that in my final draw. Thks – Sebastien Comtois Mar 4 '16 at 17:43
\documentclass[tikz,border=5]{standalone}
\begin{document}
\tikz\foreach \i [evaluate={\r=mod(\i/45,2)*1+4;}] in {0,45,...,315}{
\draw [very thick] (0:0) -- (\i:\r) arc (\i:\i+45:\r) -- cycle;
\foreach \j in {1,...,8} \draw [thin] (\i+\j*5:\r) -- (\i+\j*5:\r-0.25);
};
\end{document}


• I really think I will choose your answer as it is the "simplest" in terms of repetition. But could you please explain in detail the behavior of your code and how everything match together. Like, why and how you use \tikz\foreach instead of the "usual" \begin{tikz}. Also, if you could explain the use of [evaluate={\r=mod(\i/45,2)*1+4;}] which I think is the most important part to understand, in order to modify the drawing. thanks in advance. – Sebastien Comtois Mar 4 '16 at 17:56

This is the code I came up with.

If anyone as a better, cleaner example; feel free to post it bellow, as I am willing to accept any (equal or) better answer. (rather than my own)

I will temporarely accept this one as it is a working example of what I was looking for. But as I am still learning, everything you could think of, would be really appreciated.

\begin{center}
\begin{tikzpicture}[yscale=-1]

% center c1
\coordinate (c1) at (0,0);

%----  Large  -------%
\foreach \a in {0, 5,...,45, 90, 95,...,135, 180, 185,...,225, 270, 275,...,315}
\draw[Azure4] (\a:6.7) -- (\a:7);

\foreach \b in {0,90,...,359}
\draw[very thick] ($(c1) + (\b:7cm)$) arc (\b:\b+45:7cm) ;

%----  Small  -------%
\foreach \a in {45, 50,...,90, 135, 140,...,180, 225, 230,...,270, 315, 320,...,359}
\draw[Azure4] (\a:4.7) -- (\a:5);

\foreach \b in {0,90,...,359}
\draw[very thick] ($(c1) + (\b:5cm)$) arc (\b:\b-45:5cm) ;

\end{tikzpicture}
\end{center}


Thanks to this post, and Paul Gaborit answer's, I managed to make it work with \for loop.

The main idea (I think) was to fix the origin, and make it turn afterward. I assume this ($(c1) + (\b:5cm)$) is the way to "rotate" the origin.

But still, I'm not sure of how it really work.

(for the rest it's obvious once you play with it a bit)

Note: all the rest of the code is the same(ticks and all)