How can I compute the distance between two coordinates in TikZ?

Given two points (defined, for instance using nodes), I want to compute the distance between them.

1. Is there some build in functionality in tikz to do this?
2. If not, how can it be done using the mathematical engine?

The application I have in mind is to draw an circular arc centered at (a) and passing through some second point (b), where only (a) and (b) are known.

• Have a look at the through library for this purpose. – Roelof Spijker Dec 16 '11 at 11:29
• What about veclen(a,b)? – Ignasi Dec 16 '11 at 11:46

As wh1t3 commented, there is a through library which even has the command circle through. Here is the example in the manual: After adding the line \usetikzlibrary{through} in the preamble,

\begin{tikzpicture}
\draw[help lines] (0,0) grid (3,2);
\node (a) at (2,1.5) {$a$};
\node [draw] at (1,1) [circle through={(a)}] {$c$};
\end{tikzpicture}

You can do this using the calc library with almost the same convenience (on which Ignasi commented while I was typing the answer). You can further use this for other purposes: Modfying the example slightly and using \usetikzlibrary{calc} in the preamble, you can get the vector length by using the veclen command as

\begin{tikzpicture}
\coordinate [label=left:$A$] (A) at (0,0);
\coordinate [label=right:$B$] (B) at (2,2);

\draw[red,line width=1mm] let \p1 = ($(B)-(A)$) in (A) -- ++(45:({veclen(\x1,\y1)}););
\draw (A) -- (B);
\draw[blue] (A) let \p1 = ($(B)-(A)$) in -- ++({veclen(\x1,\y1)},0) arc (0:45:({veclen(\x1,\y1)}););
\end{tikzpicture}

which would give • The minimal example you provided is almost what I was after. I didn't manage to get rid of the horizontal blue edge. I only want the arc... Thanks in advance! – Dror Dec 16 '11 at 12:18
• @Dror Just remove -- in the last line ;) – percusse Dec 16 '11 at 12:24
• Thanks! After correcting some unbalanced parentheses your code was what I was looking for! Thanks! – Dror Dec 16 '11 at 19:22
• @Dror Oops, fixed! – percusse Dec 17 '11 at 1:50

With TikZ, you have the answer with percusse, with pgfmath is the same method but you need to determine the coordinates vx, vy of the vector formed by the two points and then \pgfmathparse{veclen(vx,vy)} the result is in \pgfmathresult.

Personally, I take a few fantasies with the TikZ's syntax. I do not find very satisfactory the syntax let \p1 \n1 and I prefer to calculate the length before drawing the objects. In addition, in some cases the result is not very fine also I use fp to calculate the length. Lua is also a possibilty. It's also possible to use the library fpu with TikZ.

\documentclass[11pt]{scrartcl}
\usepackage{tikz,fp}
\usetikzlibrary{calc}

\makeatletter
\def\calcLength(#1,#2)#3{%
\pgfpointdiff{\pgfpointanchor{#1}{center}}%
{\pgfpointanchor{#2}{center}}%
\pgf@xa=\pgf@x%
\pgf@ya=\pgf@y%
\FPeval\@temp@a{\pgfmath@tonumber{\pgf@xa}}%
\FPeval\@temp@b{\pgfmath@tonumber{\pgf@ya}}%
\FPeval\@temp@sum{(\@temp@a*\@temp@a+\@temp@b*\@temp@b)}%
\FProot{\FPMathLen}{\@temp@sum}{2}%
\FPround\FPMathLen\FPMathLen5\relax
\global\expandafter\edef\csname #3\endcsname{\FPMathLen}
}
\makeatother

\begin{document}

5cm = 5*28.45274 pt =142.2637pt

\begin{tikzpicture}
\coordinate (A) at (1,2);
\coordinate (B) at (4,6);
\calcLength(A,B){mylen}
% \draw (A) circle (\mylen pt); % pt is important here
\end{tikzpicture}
With calclength the length of AB is : \mylen

\begin{tikzpicture}
\coordinate (A) at (1,2);
\coordinate (B) at (4,6);
\path (A) let   \p1 = ($(B) - (A)$),  \n1 = {veclen(\x1,\y1)}
in -- (B) node[draw]  {With veclen the length is :\n1};
\end{tikzpicture}

\end{document} to get the arc the code is

\calcLength(A,B){mylen}
\draw[red,line width=1mm]  (A) -- ++(45:\mylen pt);
\draw[blue] (A) -- ++(\mylen pt,0) arc (0:45:\mylen pt);

For me it is more readable but it is a matter of taste

• Completely agree. With calcLength the code is much more readable and avoiding the let syntax simplifies typing. 1 up! – loved.by.Jesus Oct 5 '15 at 11:57