9

Question. Why does the following code produce a line from vertex 0 to vertex 1 which does not perfectly coincide with the analogous line drawn earlier (by way of the foreach-loop)?

  \begin{tikzpicture}
    \foreach \n/\a/\r in {0/0/100,1/72/100,2/144/100,3/216/100,4/288/100}{
      \node (v\n) at (\a:\r pt) []{};
    }
    \foreach \n in {0,1,2,3,4}{
      \pgfmathsetmacro\auxone{Mod(\n,5)};
      \pgfmathsetmacro\auxtwo{Mod(add(\n,1),5)};
      \pgfmathsetmacro\auxthree{Mod(add(\n,2),5)};          
      \draw[opacity=0.5] (v\auxone)--(v\auxtwo);
      \draw[opacity=0.5] (v\auxthree)--(v\auxone);          
    }
    \draw[opacity=0.5] (v0)--(v1);
  \end{tikzpicture}

The offending output is the following:

enter image description here

Remarks.

  • My wish is to have the second line from 0 to 1 coincide perfect with the previously drawn one (and hence create a darker hue, because of the superposition of two opacity-0.5-lines).

  • I fail to see any reason for the non-congruence of the two lines. (If one mentally substitues the variables \auxone and \auxtwo, then the two drawing-commands in question seem perfectly identical to me, hence can be expected to have exactly the same effect.)

  • I tried to exchange the line

    \draw[opacity=0.5] (v0)--(v1);
    

with the line

    \draw[opacity=0.5] (v0.center)--(v1.center);

producing a slighly different, yet still unexplained and undesirable, result, which I permit myself to also reproduce (in the hope that this might help someone understand the problem):

enter image description here

14

Because you were using \pgfmathsetmacro, which adds .0 at the end of the numbers, which get interpreted as anchors (.0 is equivalent to .east in this case). If you use \pgfmathtruncatemacro instead the lines match perfectly, as they should.

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
 \begin{tikzpicture}
    \foreach \n/\a/\r in {0/0/100,1/72/100,2/144/100,3/216/100,4/288/100}{
      \node (v\n) at (\a:\r pt) []{};
    }
    \foreach \n in {0,1,2,3,4}{
      \pgfmathtruncatemacro\auxone{Mod(\n,5)};
      \pgfmathtruncatemacro\auxtwo{Mod(add(\n,1),5)};
      \pgfmathtruncatemacro\auxthree{Mod(add(\n,2),5)};
      \draw[opacity=0.5] (v\auxtwo)--(v\auxone);
      \draw[opacity=0.5] (v\auxthree)--(v\auxone);          
    }
    \draw[opacity=0.5] (v0)--(v1);
  \end{tikzpicture}
\end{document}

enter image description here

If you want to close the gaps, you may want to use coordinates instead of nodes.

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
 \begin{tikzpicture}
    \foreach \n/\a/\r in {0/0/100,1/72/100,2/144/100,3/216/100,4/288/100}{
      \coordinate (v\n) at (\a:\r pt);
    }
    \foreach \n in {0,1,2,3,4}{
      \pgfmathtruncatemacro\auxone{Mod(\n,5)};
      \pgfmathtruncatemacro\auxtwo{Mod(add(\n,1),5)};
      \pgfmathtruncatemacro\auxthree{Mod(add(\n,2),5)};
      \draw[opacity=0.5] (v\auxtwo)--(v\auxone);
      \draw[opacity=0.5] (v\auxthree)--(v\auxone);          
    }
    \draw[opacity=0.5] (v0)--(v1);
  \end{tikzpicture}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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