Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
add comment

1 Answer

up vote 6 down vote accepted

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.

share|improve this answer
    
perfect, thanks! This is hard to debug... any tipps on how I could have found this myself? –  Zoran Zaric Feb 4 '11 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. –  Martin Scharrer Feb 5 '11 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. –  Martin Scharrer Feb 5 '11 at 0:09
    
thanks a lot for the clarification –  Zoran Zaric Feb 5 '11 at 0:12
    
@Zoran: You are welcome. Happy TeXing! –  Martin Scharrer Feb 5 '11 at 0:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.