7

I'm drawing nodes on a circle and switched from drawing lines based on coordinates to drawing them based on named nodes.

The problem now is, that with the following code the lines stop at the edges of the nodes, but don't point at the center.

How do I fix this?

\begin{tikzpicture}
    \xdef\nodes{16}
    \pgfmathsetmacro{\nodesminusone}{\nodes-1}
    \pgfmathsetmacro{\N}{log2(\nodes)}
    \xdef\deltadegree{360/\nodes}
    \draw (0,0) circle (6);

    \foreach \i in {0,...,\nodesminusone}
        \node[circle,fill=white,draw=black,thick] (node\i) at (-\i*\deltadegree+90:6) {\i};

    \foreach \i in {0,...,\nodesminusone}{
        \foreach \j in {0,...,\N}{
            \pgfmathsetmacro{\result}{mod(\i+2^\j,\nodes)}
            \draw[->,thick,color=black!10] (node\i) -- (node\result);

        }
    }

    \foreach \i in {2} {
        % predecessor
        \pgfmathsetmacro{\result}{mod(\i-1,\nodes)}
        \draw (node\i) -- (node\result);


        % successor
        \pgfmathsetmacro{\result}{mod(\i+1,\nodes)}
        \draw (node\i) -- (node\result);

        % fingers
        \foreach \j in {0,...,\N}{
            \pgfmathsetmacro{\result}{mod(\i+2^\j,\nodes)}
        \draw[->,color=blue,thick] (node\i) -- (node\result);
        }
    }
\end{tikzpicture}
0

1 Answer 1

8

Replace \pgfmathsetmacro with \pgfmathtruncatemacro.

The reason why this happens is that \pgfmathsetmacro returns a floating (or fixed?) point number, not an integer. For example:

    \foreach \j in {0,...,\N}{
        \pgfmathsetmacro{\result}{mod(\i+2^\j,\nodes)}
    \draw[->,color=blue,thick] (node\i) -- (node\result);
    }

Here \result will be 0.0, 1.0, etc. (whatever the result of mod is). However (node1.0) means "Node 'node1' at angle 0degr", which draws the arrows to this point. With \pgfmathtruncatemacro the .0 will be truncated and only (node1) etc. is used which gives the wanted result.

4
  • perfect, thanks! This is hard to debug... any tipps on how I could have found this myself? Feb 4, 2011 at 23:55
  • The most difficult thing is to know where to start. But it is always good to look at your intermediate results. You could have looked at the definition of \result, i.e. using \show\result or better \edef\A{(node\i) -- (node\result)}\show\A to see what it passed to the \draw. Feb 5, 2011 at 0:07
  • Also, minimizing the document to two nodes plus one line might have shown the error more easily. Your code is already to big and complicated to post it like it is anyway. Feb 5, 2011 at 0:09
  • @Zoran: You are welcome. Happy TeXing! Feb 5, 2011 at 0:22

Not the answer you're looking for? Browse other questions tagged .