# [Tex/LaTex] Coordinates A, B: compute |B-A| and angle between +x and (B-A)

calculationscoordinatestikz-pgf

This question has two parts:

1. why is the angle computed always 0 (should be 45)

2. what is a straightforward way to compute distance between coordinates (there is How can I compute the distance between two points in TikZ, though I was hoping for something easier, posibly via defining \coordinate for both points and getting their distance.

The application I have in mind is to define a scope with local coordinate system with origin in A and local +x axis having the direction B-A. If there is an easier way for that, I will be happy to discover it.

\documentclass{minimal}
\usepackage{tikz}
\begin{document}
\tikz{
\def\A{(1,1)}
\def\B{(2,2)}
%% {1} this returns 0.0 although it should be 1.414213...
\pgfmathanglebetweenpoints{(1,0)}{\B-\A}
\let\abAngle\pgfmathresult
%% {2} how to compute eyclidean distance of coordinates?
%% we would have to extract x and y components of B-A to be able to use \pgfveclen{}{}
%\pgfveclen{??}{??}
\let\abLength\pgfmathresult
\message{|\B-\A| = \abLength, angle between +x and (\B-\A) = \abAngle}
}
\end{document}


with the result:

|(2,2)-(1,1)| = 0.0, angle between +x and ((2,2)-(1,1)) = 0.0


Another possibility to compute the length and the angle. It's possible to use \pgfmathanglebetweenpoints with coordinates or nodes. You need to use \pgfpointanchorto get the name of the nodes.

\documentclass{scrartcl}
\usepackage{tikz}

\makeatletter
\newcommand{\getLengthAndAngle}[2]{%
\pgfmathanglebetweenpoints{\pgfpointanchor{#1}{center}}
{\pgfpointanchor{#2}{center}}
\global\let\myangle\pgfmathresult % we need a global macro
\pgfpointdiff{\pgfpointanchor{#1}{center}}
{\pgfpointanchor{#2}{center}}
\pgf@xa=\pgf@x % no need to use a new dimen
\pgf@ya=\pgf@y
\pgfmathparse{veclen(\pgf@xa,\pgf@ya)/28.45274} % to convert from pt to cm
\global\let\mylength\pgfmathresult % we need a global macro
}
\makeatother

\begin{document}
\begin{tikzpicture}

\coordinate (A) at (0,0);
\coordinate (B) at (3,4);
% we get the  length and the angle between A and B
\getLengthAndAngle{A}{B}
% to test
\draw (0,0) -- (\myangle:\mylength);
% to use in a scope
\begin{scope}[shift={(\myangle:\mylength)},rotate=\myangle]
\draw[thick,red] (0,0) -- ++(-90:2);
\end{scope}

\end{tikzpicture}
\end{document}


Update simplest solution :

We can avoid to calculate the length of AB with ($(B)-(A)$) and the library calc. It's enough to extract the angle. I defined \pgfextractangle to get the angle with the same syntax.

\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{calc}

\newcommand{\pgfextractangle}[3]{%
\pgfmathanglebetweenpoints{\pgfpointanchor{#2}{center}}
{\pgfpointanchor{#3}{center}}
\global\let#1\pgfmathresult
}

\begin{document}
\begin{tikzpicture}

\coordinate (A) at (0,0);
\coordinate (B) at (3,4);

\pgfextractangle{\angle}{A}{B}
\draw (A) -- (B);

\begin{scope}[shift={($(B)-(A)$)},rotate=\angle]
\draw[thick,red] (0,0) -- ++(-90:3);
\end{scope}

\end{tikzpicture}

\end{document}