1

For the life of me, I can't get the angle between two coordinate points and the x-axis. Ultimately I am trying to dynamically connect two nodes with slightly spread out paths.

\begin{tikzpicture}
    \usetikzlibrary{calc}

    \tikzset{roundnode/.style={circle, draw=black, fill=white, thick, minimum size=0.1mm}}

    \coordinate (X) at (8,4);
    \coordinate (Y) at (8,1);
    
    \def\r{3}

    \begin{scope}
        \coordinate (A) at (Y);
        \coordinate (E) at (X);

        \pgfmathanglebetweenpoints{A}{E}
        \def\angleAE{\pgfmathresult}
        
        \foreach \x in {-2, ..., 2}
            \draw[red!50, ultra thick, dotted] (A) .. controls ($(A) + ({\r*cos(\angleAE + \x*5)}, {\r*sin(\angleAE + \x*5)})$) and ($(E) - ({\r*cos(\angleAE + \x*5)}, {-\r*sin(\angleAE + \x*5)})$) .. (V);

        \draw (A) node[roundnode] (A) {\angleAE};
        \draw (E) node[roundnode] (E) {\angleAE};
    \end{scope}
\end{tikzpicture}

For some reason the angle always results in 0.8, regardless of the positions of the coordinates X and Y. This of course also messes up the curves

resulting image

Why does it not calculate the angle between A-E and the x-axis properly?

1

1 Answer 1

4

There seem to be two issues in the code

  1. tikz also uses pgfmath for its own calculations. Thus, \pgfmathresult is overwritten, before it is used. You can force evaluation and saving by using \edef instead of \def.
  2. \pgfmathanglebetweenpoints expects coordinates and not node names. See: pgfmathanglebetweenpoints always returns 90

So this seems to work:

\documentclass[tikz]{standalone}
\begin{document}
    \begin{tikzpicture}
        \usetikzlibrary{calc}
        
        \tikzset{roundnode/.style={circle, draw=black, fill=white, thick, minimum size=0.1mm}}
        
        \coordinate (X) at (0,0);
        \coordinate (Y) at (4,4);
        
        \def\r{3}
        
        \begin{scope}
            \coordinate (A) at (Y);
            \coordinate (E) at (X);
            
            \pgfmathanglebetweenpoints{\pgfpointanchor{A}{center}}{\pgfpointanchor{E}{center}1}
            \edef\angleAE{\pgfmathresult}
            
            \foreach \x in {-2, ..., 2}
            \draw[red!50, ultra thick, dotted] (A) .. controls ($(A) + ({\r*cos(\angleAE + \x*5)}, {\r*sin(\angleAE + \x*5)})$) and ($(E) - ({\r*cos(\angleAE + \x*5)}, {-\r*sin(\angleAE + \x*5)})$) .. (E);
            
            \draw (A) node[roundnode] (A) {\angleAE};
            \draw (E) node[roundnode] (E) {\angleAE};
        \end{scope}
    \end{tikzpicture}
\end{document}

btw: the third error was on the house ;-) There was a (V) at the end of the arcs, which was not defined. I assumed this should be (E).

enter image description here

2
  • Thank you for the quick solution! It works now Jun 2, 2021 at 19:14
  • 1
    The (V) is a relict from my real code, as I created a minimal case for this post Jun 2, 2021 at 19:15

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.