# Chords between nodes on a clock-like diagram using TikZ

I'm trying to create a series of illustrations of clock faces with chords going between nodes. I'd like to start at the 12 o'clock position, then draw a chord to the number at n positions away repeatedly until returning to 12 (e.g., if n were 3 I'd like chords on the clock face going from 12-3-6-9-12; if n were 5 I'd want to go 12-5-10-3-8-1-6-11-4-9-2-7-12).

Based on other code I've found online I'm drawing the clock like this:

\newcommand{\TikZMGClockMath}[2]{% Diameter; num. of nodes to skip
\pgfmathsetmacro{\angle}{360/12};%
\draw (0,0) circle (#1);

\foreach \i in {1,...,12} {
\begin{scope}[rotate=-\i * \angle]
\node (p\i) at (0,#1) {$\bullet$};
\node at (0,#1+0.3) {\i};
\end{scope}
}
}

\begin{tikzpicture}
\TikZMGClockMath{3}{3}
\end{tikzpicture}


I was hoping that this would let me easily create a series of nodes to traverse (e.g., p12--p5--p10--,...,--p12), but as a newcomer to TikZ the task has proven to be beyond me. In a standard programming language I guess I'd create an array using mod(), but I'm not sure how to accomplish that here. Any suggestions as to how I might do this would be greatly appreciated.

-
 Can you please add the source of your code in your question even if it is open source (just for the sake of courtesy)? – percusse Aug 23 '12 at 9:51 I think that most of it came from here: tex.stackexchange.com/questions/4275/… – Tony Gonzalez Aug 23 '12 at 11:03

Ah, percusse was faster, but here's a slightly different version that uses the code from Peter Grill's answer to Automatically add fractions and reduce the result (if neccessary) to calculate the greatest common denominator of 12 and the step size, which can be used to determine the number of lines we need to draw. Of course we could always just draw 12 lines, but that leads to some lines being drawn several times.

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\makeatletter
\def\gcd#1#2{{% #1 = a, #2 = b
\ifnum#2=0 \edef\next{#1}\else
\@tempcnta=#1 \@tempcntb=#2 \divide\@tempcnta by\@tempcntb
\multiply\@tempcnta by\@tempcntb  % q*b
\@tempcntb=#1
\advance\@tempcntb by-\@tempcnta % remainder in \@tempcntb
\ifnum\@tempcntb=0
\@tempcnta=#2
\ifnum\@tempcnta < 0 \@tempcnta=-\@tempcnta\fi
\xdef\gcd@next{\noexpand%
\def\noexpand\thegcd{\the\@tempcnta}}%
\else
\xdef\gcd@next{\noexpand\gcd{#2}{\the\@tempcntb}}%
\fi
\fi}\gcd@next
}
\makeatother

\begin{document}
\newcommand{\TikZMGClockMath}[2]{% Diameter; num. of nodes to skip
\draw (0,0) circle (#1);

\foreach \i in {1,...,12} {
\path (-360/12*\i+90:#1)
node (p\i) [circle,fill,inner sep=2pt] {}
node [anchor=180-360/12*\i+90] {\i};
}
\gcd{12}{#2}
\pgfmathtruncatemacro\steps{12/\thegcd}

\foreach \pos in {1,...,\steps}{
\pgfmathtruncatemacro\start{Mod(#2*(\pos-1)-1,12)+1}
\pgfmathtruncatemacro\end{Mod(#2*(\pos)-1,12)+1}
\draw [thick,red] (p\start) -- (p\end);
}
}

\noindent
\foreach \stepsize in {1,...,12}{%
\begin{tikzpicture}
\TikZMGClockMath{1}{\stepsize}
\end{tikzpicture}
}

\end{document}

-
 percusse's answer is also usable, but this one gets style points for gracefulness, and is probably what I'll go with. Thanks, Jake! – Tony Gonzalez Aug 23 '12 at 12:18

I have added a \foreach loop without to much emphasis on the look. Also, to see if it is really working properly, I've added arrow heads via [-latex] etc. so you might want to remove them or change it with a better looking ones.

\documentclass{standalone}
\usepackage{tikz}
\newcommand{\TikZMGClockMath}[2]{% Diameter; num. of nodes to skip
\pgfmathsetmacro{\angle}{360/12};%
\draw (0,0) circle (#1);

\foreach \i in {1,...,12} {
\begin{scope}[rotate=-\i * \angle]
\node[inner sep=1pt,fill,circle] (p\i) at (0,#1) {};
\node at (0,#1+0.3) {\i};
\end{scope}
}
\draw[-latex] (p12) -- (p#2);
\foreach \x[remember=\x as \lastx (initially 1)] in {2,...,12}{
\pgfmathtruncatemacro{\modtwelve}{mod(#2*\x,12)}
\pgfmathtruncatemacro{\lastmodtwelve}{mod(#2*\lastx,12)}
\ifnum\modtwelve>0\relax
\draw[latex-] (p\modtwelve) -- (p\lastmodtwelve);
\else
\draw[latex-] (p12) -- (p\lastmodtwelve);
\breakforeach
\fi
}
}
\begin{document}
\begin{tikzpicture}
\TikZMGClockMath{3}{7}
\begin{scope}[xshift=7cm]
\TikZMGClockMath{3}{10}
\end{scope}
\begin{scope}[xshift=14cm]
\TikZMGClockMath{3}{5}
\end{scope}
\begin{scope}[yshift=7cm]
\TikZMGClockMath{3}{3}
\end{scope}
\begin{scope}[shift={(7cm,7cm)}]
\TikZMGClockMath{3}{4}
\end{scope}
\begin{scope}[shift={(14cm,7cm)}]
\TikZMGClockMath{3}{6}
\end{scope}
\end{tikzpicture}
\end{document}


-
 Thank you for the quick answer, percusse. – Tony Gonzalez Aug 23 '12 at 12:19 @TonyGonzalez My pleasure. – percusse Aug 23 '12 at 12:28