Measure/dimension line (line parallel to a line)
You can just use (A)
and (B)
. Then the segment is long enough to get a good result.
\documentclass[border=2pt,tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (1,1);
\coordinate (B) at (4,4);
\fill (A) circle(.5mm);
\fill (B) circle(.5mm);
\draw (A)--(B);
\coordinate (P1) at ($(A)!.2cm!90:(B)$);
\coordinate (P2) at ($(B)!.2cm!-90:(A)$);
\draw[|<->|] (P1)--(P2);
\end{tikzpicture}
\end{document}
You can also use a dimline
like this:
\documentclass[tikz, border=2pt]{standalone}
\usepackage{tikz-dimline, calc}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (1,1);
\coordinate (B) at (4,4);
\fill (A) circle(.5mm);
\fill (B) circle(.5mm);
\draw (A)--(B);
\dimline[extension start length=0.4 cm, extension end length=0.4 cm] {($(A)!.4cm!90:(B)$)} {($(B)!.4cm!-90:(A)$)}{10};
\end{tikzpicture}
\end{document}
Or without a label:
\dimline[extension start length=0.4 cm, extension end length=0.4 cm, label style=transparent] {($(A)!.4cm!90:(B)$)} {($(B)!.4cm!-90:(A)$)}{};
You can also use the - in my opinion - amazing package tkz-euclide
and do something like:
\documentclass[border=2mm, tikz]{standalone}
\usepackage{tkz-euclide}
\begin{document}
\begin{tikzpicture}
\tkzDefPoints{
1/1/A,
4/4/B}
\tkzDrawSegment[dim={,4mm,transparent}](A,B)
\tkzDrawPoints(A,B)
\end{tikzpicture}
\end{document}