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 do not understand how it is interpreted by a specified angle when using polar coordinates. I prepared three examples in my MWE:

    \documentclass{article}
 \usepackage{tikz}                      % TikZ and PGF picture 
     \usetikzlibrary{intersections}
     \usetikzlibrary{calc}
     \usetikzlibrary{positioning}     
 \usetikzlibrary{matrix,arrows,decorations.pathmorphing} 
 \usetikzlibrary{positioning}

\newdimen\XCoord
\newdimen\YCoord
\newcommand*{\ExtractCoordinate}[1]{\path (#1); \pgfgetlastxy{\XCoord}{\YCoord};}%

\begin{document}


\begin{figure}[htp]
        \begin{tikzpicture}
          \coordinate (A) at (0,0);
          \coordinate (B) at (0,1);
          \coordinate (C) at (1,1);
          \node[left, color=blue] at (A) {A};
          \node[left, color=blue] at (B) {B};
          \node[right, color=blue] at (C) {C};
          \draw (A) -- (B) -- (C);        
          \draw [color=cyan] let \p1=(A), \p2=(B), \p3=(C),
                \n1={atan2(\y2-\y1,\x3-\x1)} in
                (A) -- (\n1:2cm);
          \ExtractCoordinate{B};
          \node [below] at (1cm,-2cm) {B $(\XCoord,\YCoord)$};
          \ExtractCoordinate{C};
          \node [below] at (1cm,-2.5cm) {C $(\XCoord,\YCoord)$};          
        \end{tikzpicture} 

        \begin{tikzpicture}
          \coordinate (A) at (-90.58205pt, 119.0348pt);
          \coordinate (B) at (-90.58205pt, 133.26117pt);
          \coordinate (C) at (-40.43698pt, 119.0348pt);
          \node[left, color=blue] at (A) {A};
          \node[left, color=blue] at (B) {B};
          \node[right, color=blue] at (C) {C};
          \draw (B) -- (A) -- (C);        
          \draw [color=cyan] let \p1=(A), \p2=(B), \p3=(C),
                \n1={atan2(abs(\y2-\y1), abs(\x3-\x1))} in
                (B) -- (74.161134732:2cm);
          \draw[right, color=green] (B) -- (0:2cm);
          \draw[right, color=red] (B) -- (45:2cm);        
          \ExtractCoordinate{B};
          \node [below] at (1cm,-2cm) {B $(\XCoord,\YCoord)$};
          \ExtractCoordinate{C};
          \node [below] at (1cm,-2.5cm) {C $(\XCoord,\YCoord)$};          
        \end{tikzpicture}

        \begin{tikzpicture}
          \coordinate (A) at (2, 2); 
          \draw[right, color=green] (A) -- (0:2cm);
          \draw[right, color=red] (A) -- (45:2cm);
          \draw[right, color=blue] (A) -- (90:2cm);
          \draw[right, color=black] (A) -- (74.161134732:2cm);
        \end{tikzpicture}

\end{figure}
\end{document}
  • first example: everything works fine.
  • second example: problem:

In the following command:

\draw [color=cyan] let \p1=(A), \p2=(B), \p3=(C), \n1={atan2(abs(\y2-\y1), abs(\x3-\x1))} in (B) -- (74.161134732:2cm);

I tried to calculate the angle so I joined B to C. Unfortunately, unsuccessfully. I thought that atan2 function works wrong, so I replaced value in the variable \n1 by manually calculated angle, but again I did not connect the two points.

I reached to the conclusion that I can not properly use polar coordinates, and so I tried to test everything in the third example. I expected the green line will be horizontal, but instead, the line is vertical.

For illustration, I attach a picture:

enter image description here

Can you explain where I'm wrong?

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

There are two mistakes in your code. The first is mathematical: the arguments to the atan2 are the x and then the y (I know that this varies from program to program so is something you should always check when using atan2 functions); also, to get the angle correct you should not take the absolute values as this changes the quadrant at the least. So the correct syntax for the atan2 in your second example would be:

\draw [color=cyan] let \p1=(A), \p2=(B), \p3=(C),
            \n1={atan2(\x3-\x1, \y2-\y1)} in
            (B) -- (\n:2cm);

The reason that this doesn't show up in your first example is because both coordinate expressions evaluate to 1 whereupon the order doesn't matter and taking absolute values does nothing.

However, the above is still not right and this is the second error. You are drawing a line from (B) to (\n:2cm). The second position is specified in absolute coordinates and so is a point at \n degrees and 2cm from the origin. You want it to be from (B). To reorient the coordinate system, you should use relative coordinates. Thus (B) -- ++(\n:2cm) gets you what you want.

Again, this doesn't show up in the first example because your point of interest, (A) in that case, is located at the origin so relative and absolute coordinates give the same answer.

This is the same mistake in your third example. The coordinate (0:2cm) becomes (2,0) which is vertically down from (A) (located at (2,2)). To get a horizontal line you need to make it relative: ++(0:2cm).

Full code:

\documentclass{article}
 \usepackage{tikz}                      % TikZ and PGF picture 
     \usetikzlibrary{intersections}
     \usetikzlibrary{calc}
     \usetikzlibrary{positioning}     
 \usetikzlibrary{matrix,arrows,decorations.pathmorphing} 
 \usetikzlibrary{positioning}

\newdimen\XCoord
\newdimen\YCoord
\newcommand*{\ExtractCoordinate}[1]{\path (#1);
\pgfgetlastxy{\XCoord}{\YCoord};}%

\begin{document}


\begin{figure}[htp]
        \begin{tikzpicture}
          \coordinate (A) at (0,0);
          \coordinate (B) at (0,1);
          \coordinate (C) at (1,1);
          \node[left, color=blue] at (A) {A};
          \node[left, color=blue] at (B) {B};
          \node[right, color=blue] at (C) {C};
          \draw (A) -- (B) -- (C);        
          \draw [color=cyan] let \p1=(A), \p2=(B), \p3=(C),
                \n1={atan2(\y2-\y1,\x3-\x1)} in
                (A) -- (\n1:2cm);
          \ExtractCoordinate{B};
          \node [below] at (1cm,-2cm) {B $(\XCoord,\YCoord)$};
          \ExtractCoordinate{C};
          \node [below] at (1cm,-2.5cm) {C $(\XCoord,\YCoord)$};          
        \end{tikzpicture} 

        \begin{tikzpicture}
          \coordinate (A) at (-90.58205pt, 119.0348pt);
          \coordinate (B) at (-90.58205pt, 133.26117pt);
          \coordinate (C) at (0pt, 119.0348pt);
          \node[left, color=blue] at (A) {A};
          \node[left, color=blue] at (B) {B};
          \node[right, color=blue] at (C) {C};
          \draw (B) -- (A) -- (C);        
          \draw [color=cyan] let \p1=(A), \p2=(B), \p3=(C),
                \n1={atan2(\x3-\x1,\y1-\y2)} in
                (B) -- ++(\n1:4cm);
          \draw[right, color=green] (B) -- ++(0:2cm);
          \draw[right, color=red] (B) -- ++(45:2cm);        
          \ExtractCoordinate{B};
          \node [below] at (1cm,-2cm) {B $(\XCoord,\YCoord)$};
          \ExtractCoordinate{C};
          \node [below] at (1cm,-2.5cm) {C $(\XCoord,\YCoord)$};          
        \end{tikzpicture}

        \begin{tikzpicture}
          \coordinate (A) at (2, 2); 
          \draw[right, color=green] (A) -- ++(0:2cm);
          \draw[right, color=red] (A) -- ++(45:2cm);
          \draw[right, color=blue] (A) -- ++(90:2cm);
          \draw[right, color=black] (A) -- ++(74.161134732:2cm);
        \end{tikzpicture}

\end{figure}
\end{document}

Result:

relative polar coordinates

share|improve this answer
    
Thank you very much. It is very helpful for me. –  jafan Oct 23 '12 at 11:46
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.