# Coordinates A, B: compute |B-A| and angle between +x and (B-A)

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

• You don't need to use (1,0) with \pgfmathanglebetweenpoints. This macro gives the angle formed by a line AB with the axe x'x or (1,0) if you prefer. You need only to give two points with coordinates (\pgfpointis possible but pgfpointanchor is very useful. – Alain Matthes Dec 25 '11 at 23:08

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}{%
\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}{%
\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} Fixed the code and applied Altermundus' nice snippet to remove extra registers!

I don't understand exactly why the qustion linked is not an answer (maybe you elaborate on that later) but here is a pgf-based take. By the way, the code is far from proper, let alone optimal, but I wanted to do it as slowly as possible.

The answer to your first question is: \pgfmathanglebetweenpoints accepts point rather than coordinate. Hence if you supply with \pgfpoint{}{} it works as expected.

Also, I guess the code below is close to what you want with the respect to scope options

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
%Define some length registers
\newdimen\lengthAB

% Define two points
\def\pointA{\pgfpoint{0cm}{0cm}}
\pgfcoordinate{A}{\pointA}
\def\pointB{\pgfpoint{1.5cm}{2cm}}
\pgfcoordinate{B}{\pointB}

%The function accepts points, otherwise zero!
\pgfmathanglebetweenpoints{\pointA}{\pointB}
\edef\angleAB{\pgfmathresult}

%Altermundus taught me this
\makeatletter
\pgfpointdiff{\pointA}{\pointB}
\pgf@xa=\pgf@x % no need to use a new dimen
\pgf@ya=\pgf@y
\pgfmathparse{veclen(\pgf@xa,\pgf@ya)}
\makeatother
\pgfmathsetlength{\lengthAB}{\pgfmathresult}

% Test if they give the same result
\draw[ultra thick,red] (0,0) -- (B) ;
\draw (0,0) -- (\angleAB:\lengthAB);

\begin{scope}[shift={(\angleAB:\lengthAB)},rotate=\angleAB,sloped]
% Test if the origin is translated
\node[above] (a) at (0,0) {$(\pgfmathparse{\lengthAB/28.45274}\pgfmathresult \text{cm}, \angleAB^\circ )$};
%Test the orientation with a line that should be going down
\draw[thick] (a) -- ++(-90:2);
\end{scope}

\end{tikzpicture}
\end{document} 