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
veclen(a,b)
? – Ignasi Dec 16 '11 at 11:46