[Tex/LaTex] retrieve the length of a segment

lengthstikz-pgf

I want to retrieve the length of a segment in cm for display in a node

As in the example below, I would like to display the length of three vectors

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at  (0,0);
\coordinate (B) at (4,2);
\coordinate (C) at (1,3);
\coordinate (D) at (5,3);

\draw[-latex, green, thick] (A) node[left,black]{A} -- (B)node[above left,black]{B} ;
\draw (A) -- (C)node[left]{C} ;
\draw (B) -- (D) node[left]{D};

\draw[thick,-latex,red] (B) -- (intersection of A--C and B--D) coordinate(I) node[left,black]{I} ;
\draw[thick, -latex]  (I) -- (A) node[left,midway]{lenght cm};

\end{tikzpicture}
\end{document}

enter image description here

Best Answer

TikZ provides the calculation via veclen. To calculate the length in cm I used 1cm=28.4pt.

The example below is taken from the manual where you can find more examples.

Here an example:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at  (0,0);
\coordinate (B) at (4,2);
\coordinate (C) at (1,3);
\coordinate (D) at (5,3);

\draw[-latex, green, thick] (A) node[left,black]{A} -- (B)node[above left,black]{B} ;
\draw (A) -- (C)node[left]{C} ;
\draw (B) -- (D) node[left]{D};

\draw[thick,-latex,red] (B) -- (intersection of A--C and B--D) coordinate(I) node[left,black]{I} ;
\draw[thick, -latex] let \p1 = ($ (I) - (A) $) in (I) -- (A) node[left,midway]{ \pgfmathparse{veclen(\x1,\y1)/28.4}\pgfmathresult cm};

\end{tikzpicture}
\end{document}