7

I'm wracking my brains over a seemingly simple problem. Given a coordinate, I would like to do some computation with its components. More specifically, I want to compute the elevation of a point.

Consider this M(not)WE:

\documentclass{article}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
    \coordinate (A) at (2,3);

    \newdimen\x
    \newdimen\y
    \pgfextractx{\x}{\pgfpointanchor{A}{center}}
    \pgfextracty{\y}{\pgfpointanchor{A}{center}}
    \pgfmathparse{atan2(\y,\x)}
    \node at (1,1) {\pgfmathresult};
\end{tikzpicture}
\end{document}

My goal is to extract the x and the y component from the point A, compute the elevation, and print this in a node. How can I achieve this?

Ingo
  • 20,035

2 Answers2

8

You can use

\documentclass{article}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
  \coordinate (A) at (2,3);
%
  \newdimen\x
  \newdimen\y
  \pgfextractx{\x}{\pgfpointanchor{A}{center}}
  \pgfextracty{\y}{\pgfpointanchor{A}{center}}
  \node at (1,1) {\pgfmathparse{atan2(\y,\x)}\pgfmathresult};
\end{tikzpicture}
\end{document}

or

\def\myresult{\pgfmathparse{atan2(\y,\x)}\pgfmathresult}
\node at (1,1) {\myresult};

or

\pgfmathsetmacro\myresult{atan2(\y,\x)}
\node at (1,1) {\myresult};
esdd
  • 85,675
  • Thanks! One question though. Say I want to use \myresult as a coordinate in a polar coordinate, as in \node at (\myresult:1) {test};, how can I do that? – Ingo Apr 25 '14 at 12:23
  • Nevermind, with \pgfmathsetmacro it works. – Ingo Apr 25 '14 at 12:26
2

There is a standard way to do it using let like this :

\begin{tikzpicture}
  \coordinate (A) at (2,3);
  \path let \p1=(A) in node{\x1};
\end{tikzpicture}

enter image description here

And if you want it as a number (or in cm) you can see this answer.

Kpym
  • 23,002