[Tex/LaTex] TikZ: using the draw let in syntax to create an arc but the arc and its difference angle doesnt add up to 360

syntaxtikz-pgf

Consider this code snippet:

\draw[-latex] let
        \p0 = (P6),
        \p1 = (P5),
        \p2 = (P4),
        \n1 = {atan2(\x1 - \x0, \y1 - \y0)},
        \n2 = {atan2(\x2 - \x0, \y2 - \y0)},
        \n3 = {.75cm},
        \n4 = {(\n2 + \n1) / 2}
      in (P6) +(\n1:\n3) arc[radius = \n3, start angle = \n1, end angle = \n2];

This comes from the code below (at the bottom):

\documentclass[convert = false]{standalone}

\usepackage{tikz}
\usetikzlibrary{calc, intersections, backgrounds, arrows}

\begin{document}

\tikzset{circle with radius/.style = {shape = circle, inner sep = 0pt,
      outer sep = 0pt, minimum size = {2 * (#1)}}}
  \begin{tikzpicture}[line join = round, line cap = round,
    every label/.append style = {font = \scriptsize},
    dot/.style = {inner sep = +0pt, shape = circle,
      draw = black, label = {#1}},
    small dot/.style = {minimum size = .05cm, dot = {#1}},
    big dot/.style = {minimum size = .1cm, dot = {#1}},
    ]
    \begin{scope}
      %\clip ($(B) + (1.5, -2)$) rectangle ($(B) + (-3, 2)$);                       

      \coordinate (O) at (0, 0);

      \pgfmathsetmacro{\as}{3}
      \pgfmathsetmacro{\bs}{2.25}
      \pgfmathsetmacro{\c}{sqrt(\as^2 - \bs^2)}
      \pgfmathsetmacro{\al}{3.75}
      \pgfmathsetmacro{\bl}{2.9}
      \pgfmathsetmacro{\cl}{sqrt(\al^2 - \bl^2)}
      \pgfmathsetmacro{\xs}{abs(\c - \cl)}

      \coordinate (O) at (0, 0);

      \node[fill = black, big dot = {below left: \(F\)}] (F) at (\c, 0) {};

      \path[name path global = line1] (\c, 0) -- ++(60:{\as} and \bs);
      \path[name path global = line2] (\c, 0) -- ++(150:6cm);
    
    \draw[name path global = ell1, blue] (O) ellipse
        (\as cm and \bs cm);

        \draw[name path global = ell2, red] (-\xs, 0)
        ellipse (\al cm and \bl cm);

      \path[name intersections = {of = line1 and ell1, by = P1}];
      \node[fill = black, big dot = {right: \(A\)}] (A) at (P1) {};

      \path[name intersections = {of = line2 and ell2, by = P2}];
      \node[fill = black, big dot = {above: \(B\)}] (B) at (P2) {};

      \begin{scope}[declare function = {doubleA = 5.8cm;},
        ]
        \clip ($(A.center) + (1, 0)$) rectangle ($(B.center) + (0, 1)$);
        \begin{pgfinterruptboundingbox}
          \path let
            \p1 = ($(A) - (F)$),
            \p2 = ($(B) - (F)$),
            \n1 = {veclen(\x1, \y1)},
            \n2 = {veclen(\x2, \y2)}
          in
          (A) node[name path global = aCircle, circle with radius = doubleA-\n1]
          {}
          (B) node[name path global = bCircle, circle with radius = doubleA-\n2]
          {}
          (F) node[name path global = fCircle,
          circle with radius = .5 * doubleA] {};

          \tikzset{name intersections = {of = aCircle and bCircle, name = F'} }
          \foreach \solA in {2} {
            \path ($(F)!.5!(F'-\solA)$) coordinate (C'-\solA)
            ($(C'-\solA)!doubleA/2!(F)$) coordinate (xDir-\solA)
            (F'-\solA) node[name path global/.expanded = f'Circle-\solA,
            circle with radius = .5 * doubleA] {};
          }                         %!?      
          \foreach \solA in {2} { %!?                                               
            \path[name intersections = {of = fCircle and f'Circle-\solA,
              by = {yDir-\solA}}]
            ($(xDir-\solA)-(C'-\solA)$) coordinate (xDir'-\solA)
            ($(yDir-\solA)-(C'-\solA)$) coordinate (yDir'-\solA)
            ;
          }
        \end{pgfinterruptboundingbox}
        \foreach \solA in {2}
        \draw[x = (xDir'-\solA), y = (yDir'-\solA), name path global = traj]
        (C'-\solA) circle [radius = 1];
      \end{scope}

      \path[name path = circ] (B) circle [radius = 1bp];

      \draw[name intersections = {of = circ and traj}, -latex] (B) --
      ($(intersection-1)!1.25cm!(intersection-2)$) coordinate (P3);
      \draw[name intersections = {of = circ and ell2}, -latex, red] (B) --
      ($(intersection-1)!2cm!(intersection-2)$) coordinate (P4);
      \draw[-latex, name path = line3] (P3) -- (P4);
      \draw[name path = line4] (B) -- ($(B)!2.1cm!-90:(F)$) coordinate (P5);

      \path[name path = circ2] (P3) circle [radius = 1bp];
      \path[name intersections = {of = circ2 and line3}, name path = line5]
      (P3) -- ($(intersection-1)!2cm!(P3)$);
      \path[name intersections = {of = line5 and line4, by = P6}];

      \path[name intersections = {of = circ and traj}] (B) --
      ($(intersection-1)!1.5cm!(intersection-2)$) coordinate (P7);

      \path[name intersections = {of = circ and ell2}] (B) --
      ($(intersection-1)!2.5cm!(intersection-2)$) coordinate (P8);
      
      \draw[-latex] let
        \p0 = (P6),
        \p1 = (P5),
        \p2 = (P4),
        \n1 = {atan2(\x1 - \x0, \y1 - \y0)},
        \n2 = {atan2(\x2 - \x0, \y2 - \y0)},
        \n3 = {.75cm},
        \n4 = {(\n2 + \n1) / 2}
      in (P6) +(\n1:\n3) arc[radius = \n3, start angle = \n1, end angle = \n2];
    \end{scope}
  \end{tikzpicture}

\end{document}

So in this draw command, I set 0th point as the focus, the 1st point as the starting angle line, and the 2nd point as the ending angle line. However, the TikZ picture arcs the opposite direction (see below):

enter image description here

So I then took \n2 = {2 * pi - atan2(..)} which should be wedge not encircled with the arc but this generate this picture:

enter image description here

Those two arcs should theoretically add up to a full circle.

What is going wrong?

How can I get the command to arc to the correct distance?

If I compile both commands, we can see without a doubt they aren't adding up:

enter image description here


Edit 2:

Consider the image below:

enter image description here

As to why I used the code above as MWE, we can see that I have arcs in the same vicinity that had negative angles as well. I defined those arcs just as I did the one that posed a problem. Therefore, it appeared to be an issue with that area not negative angles.

For instance, gamma2 was defined focus, lower line, and upper line but it drew the arc without needing special treatment unlike the phi angle I asked here about.

Best Answer

The trigonometric functions in PGF use degrees, so you'll have to use 360 instead of 2*pi, and you'll have to deduct the full circle from the angle, not the other way round.

This becomes clear if you make a minimal example and look at the angles. The delta angle of the arc is the end angle minus the start angle. In your case, you have a delta angle that's larger than 180°, so you get the large arc. By subtracting a full circle from the end angle, the absolute value of the difference angle becomes smaller than 180°:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}
\coordinate [label=right:O] (O) at (0,0);
\coordinate [label=below:A] (A) at (-1,-1.5);
\coordinate [label=left:B] (B) at (-2,1);
\draw (A) -- (O) -- (B);

\draw [-latex] 
    let
        \p0 = (O),
        \p1 = (A),
        \p2 = (B),
        \n1 = {atan2(\x1 - \x0, \y1 - \y0)},
        \n2 = {atan2(\x2 - \x0, \y2 - \y0)},
        \n3 = {1cm}
    in
        (\p0) + (\n1:\n3) arc [radius=\n3, start angle=\n1, end angle=\n2]
    node [anchor=west, align=right] at (1.5,0) {\verb|\n1|: \pgfmathparse{\n1}\pgfmathprintnumber{\pgfmathresult}\\ \verb|\n2|: \pgfmathparse{\n2}\pgfmathprintnumber{\pgfmathresult}\\
    delta: \pgfmathparse{\n2-\n1}\pgfmathprintnumber{\pgfmathresult}};
\end{tikzpicture}

\begin{tikzpicture}
\coordinate [label=right:O] (O) at (0,0);
\coordinate [label=below:A] (A) at (-1,-1.5);
\coordinate [label=left:B] (B) at (-2,1);
\draw (A) -- (O) -- (B);

\draw [-latex] 
    let
        \p0 = (O),
        \p1 = (A),
        \p2 = (B),
        \n1 = {atan2(\x1 - \x0, \y1 - \y0)},
        \n2 = {atan2(\x2 - \x0, \y2 - \y0) - 360},
        \n3 = {1cm}
    in
        (\p0) + (\n1:\n3) arc [radius=\n3, start angle=\n1, end angle=\n2]
    node [anchor=west, align=right] at (1.5,0) {\verb|\n1|: \pgfmathparse{\n1}\pgfmathprintnumber{\pgfmathresult}\\ \verb|\n2|: \pgfmathparse{\n2}\pgfmathprintnumber{\pgfmathresult}\\
    delta: \pgfmathparse{\n2-\n1}\pgfmathprintnumber{\pgfmathresult}};
\end{tikzpicture}


\end{document}

\documentclass[convert = false]{standalone}

\usepackage{tikz}
\usetikzlibrary{calc, intersections, backgrounds, arrows}

\begin{document}

\tikzset{circle with radius/.style = {shape = circle, inner sep = 0pt,
      outer sep = 0pt, minimum size = {2 * (#1)}}}
  \begin{tikzpicture}[line join = round, line cap = round,
    every label/.append style = {font = \scriptsize},
    dot/.style = {inner sep = +0pt, shape = circle,
      draw = black, label = {#1}},
    small dot/.style = {minimum size = .05cm, dot = {#1}},
    big dot/.style = {minimum size = .1cm, dot = {#1}},
    ]
    \begin{scope}
      %\clip ($(B) + (1.5, -2)$) rectangle ($(B) + (-3, 2)$);                       

      \coordinate (O) at (0, 0);

      \pgfmathsetmacro{\as}{3}
      \pgfmathsetmacro{\bs}{2.25}
      \pgfmathsetmacro{\c}{sqrt(\as^2 - \bs^2)}
      \pgfmathsetmacro{\al}{3.75}
      \pgfmathsetmacro{\bl}{2.9}
      \pgfmathsetmacro{\cl}{sqrt(\al^2 - \bl^2)}
      \pgfmathsetmacro{\xs}{abs(\c - \cl)}

      \coordinate (O) at (0, 0);

      \node[fill = black, big dot = {below left: \(F\)}] (F) at (\c, 0) {};

      \path[name path global = line1] (\c, 0) -- ++(60:{\as} and \bs);
      \path[name path global = line2] (\c, 0) -- ++(150:6cm);

    \draw[name path global = ell1, blue] (O) ellipse
        (\as cm and \bs cm);

        \draw[name path global = ell2, red] (-\xs, 0)
        ellipse (\al cm and \bl cm);

      \path[name intersections = {of = line1 and ell1, by = P1}];
      \node[fill = black, big dot = {right: \(A\)}] (A) at (P1) {};

      \path[name intersections = {of = line2 and ell2, by = P2}];
      \node[fill = black, big dot = {above: \(B\)}] (B) at (P2) {};

      \begin{scope}[declare function = {doubleA = 5.8cm;},
        ]
        \clip ($(A.center) + (1, 0)$) rectangle ($(B.center) + (0, 1)$);
        \begin{pgfinterruptboundingbox}
          \path let
            \p1 = ($(A) - (F)$),
            \p2 = ($(B) - (F)$),
            \n1 = {veclen(\x1, \y1)},
            \n2 = {veclen(\x2, \y2)}
          in
          (A) node[name path global = aCircle, circle with radius = doubleA-\n1]
          {}
          (B) node[name path global = bCircle, circle with radius = doubleA-\n2]
          {}
          (F) node[name path global = fCircle,
          circle with radius = .5 * doubleA] {};

          \tikzset{name intersections = {of = aCircle and bCircle, name = F'} }
          \foreach \solA in {2} {
            \path ($(F)!.5!(F'-\solA)$) coordinate (C'-\solA)
            ($(C'-\solA)!doubleA/2!(F)$) coordinate (xDir-\solA)
            (F'-\solA) node[name path global/.expanded = f'Circle-\solA,
            circle with radius = .5 * doubleA] {};
          }                         %!?      
          \foreach \solA in {2} { %!?                                               
            \path[name intersections = {of = fCircle and f'Circle-\solA,
              by = {yDir-\solA}}]
            ($(xDir-\solA)-(C'-\solA)$) coordinate (xDir'-\solA)
            ($(yDir-\solA)-(C'-\solA)$) coordinate (yDir'-\solA)
            ;
          }
        \end{pgfinterruptboundingbox}
        \foreach \solA in {2}
        \draw[x = (xDir'-\solA), y = (yDir'-\solA), name path global = traj]
        (C'-\solA) circle [radius = 1];
      \end{scope}

      \path[name path = circ] (B) circle [radius = 1bp];

      \draw[name intersections = {of = circ and traj}, -latex] (B) --
      ($(intersection-1)!1.25cm!(intersection-2)$) coordinate (P3);
      \draw[name intersections = {of = circ and ell2}, -latex, red] (B) --
      ($(intersection-1)!2cm!(intersection-2)$) coordinate (P4);
      \draw[-latex, name path = line3] (P3) -- (P4);
      \draw[name path = line4] (B) -- ($(B)!2.1cm!-90:(F)$) coordinate (P5);

      \path[name path = circ2] (P3) circle [radius = 1bp];
      \path[name intersections = {of = circ2 and line3}, name path = line5]
      (P3) -- ($(intersection-1)!2cm!(P3)$);
      \path[name intersections = {of = line5 and line4, by = P6}];

      \path[name intersections = {of = circ and traj}] (B) --
      ($(intersection-1)!1.5cm!(intersection-2)$) coordinate (P7);

      \path[name intersections = {of = circ and ell2}] (B) --
      ($(intersection-1)!2.5cm!(intersection-2)$) coordinate (P8);

      \draw[-latex] let
        \p0 = (P6),
        \p1 = (P5),
        \p2 = (P4),
        \n1 = {atan2(\x1 - \x0, \y1 - \y0)},
        \n2 = {atan2(\x2 - \x0, \y2 - \y0)-360},
        \n3 = {.75cm}
      in (P6) +(\n1:\n3) arc[radius = \n3, start angle = \n1, end angle = \n2];

          \end{scope}
  \end{tikzpicture}

\end{document}
Related Question