# Angle and distance between two points in tikz?

Given two (A) and (B) 2-D points in tikzpicture environment, I need to collect both angle of the line (A)-(B) with respect to the vector(1,0) and the half of its distance in two variables, say \Aab and \Dab respectively. SOLVED (see Coordinates A, B: compute |B-A| and angle between +x and (B-A))

\documentclass{article}
\usepackage{pgf,tikz}
\usetikzlibrary{calc}
\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}
\clip (0,4) rectangle (7,-5);
\coordinate (A) at (1,1);
\coordinate (B) at (3,4);
\getLengthAndAngle{A}{B}
\draw[help lines,gray] (0,-3) grid (5,5);
\begin{scope}[blue, thick]
\draw (A) -- (B)--+(\mylength,0);
\draw[rotate around={-\myangle:(A)}] (A)--+(\mylength,0);
\end{scope}
\draw (B) circle (\mylength cm);
\end{tikzpicture}
\end{document}

• Did you try something? Nov 14 '18 at 19:54
• look angle library. Nov 14 '18 at 20:06
• Nov 14 '18 at 20:14
• @Zarko If you mean the angles library that is as you know for drawing an angle symbol between two lines, the OP wants to calculate an angle and a distance. Nov 14 '18 at 20:16
• It would be helpful if you composed a fully compilable MWE including \documentclass and the appropriate packages that at least sets up the problem. While solving problems can be fun, setting them up is not. Then, those trying to help can simply cut and paste your MWE and get started on solving the problem. This will also serve as a test case and ensure that the solution actually works for you. As it is it is not clear what you mean by "collect". Nov 14 '18 at 20:23

Here is an alternative solely based on calc, i.e. not using extra macros, and with a little helper that allows you to "export" the length outside the path (and scope).

\documentclass{article}
\usepackage{pgf,tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[globalize/.code n args={2}{\xdef#2{#1}}]
\clip (0,4) rectangle (7,-5);
\coordinate (A) at (1,1);
\coordinate (B) at (3,4);
\draw[help lines,gray] (0,-3) grid (5,5);
\begin{scope}[blue, thick]
\draw let \p1=($(B)-(A)$), \n1={veclen(\y1,\x1)}, \n2={veclen(\y1,\x1)}
in [globalize={\n2}{\mylength}] (A) -- (B)--+(\n2,0)
[rotate around={-\n1:(A)}] (A)--+(\n2,0);
\end{scope}
\draw (B) circle (\mylength);
\end{tikzpicture}
\end{document}


• \n1={atan2(\y1,\x1)}. Can we globalize both variables... and no drawing at all?
– JOM
Nov 14 '18 at 22:31
• @JOM Sure: \path let \p1=($(B)-(A)$), \n1={veclen(\y1,\x1)}, \n2={veclen(\y1,\x1)} in [globalize={\n2}{\mylength},globalize={\n1}{\myangle}];. Just watch out for globalization critics! ;-)
– user121799
Nov 15 '18 at 0:03