[Tex/LaTex] How to compute the distance between two coordinates in TikZ

calculationscoordinatestikz-pgf

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.

Best Answer

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

enter image description here