Perpendicular line to two-points-line given the length

I have two points A and B, I call the line consisting of A and B as LAB:ax+by+c=0, I would like to draw a line segment starting from the point B perpendicular to LAB and with length len such that the line segment stays on positive side of the line LAB, which is the semi-space ax+by+c>0. How is it possible in Tikz?

I managed to calculate and draw such line in Matlab, here is the code. It might be helpful to understand what I mean:

%% input data
A.x = 2;
A.y = 2;
B.x = 6;
B.y = 5;
d = 2.5; % the length len
P = B; % selected end point
%% calculation
v.x = B.x - A.x;
v.y = B.y - A.y;

X = v.y*d/sqrt(1+v.x^2);
Y = -v.x/v.y*X;

% in one side of the line LAB
C.x = X + P.x;
C.y = Y + P.y;

% in another side of the line LAB
D.x = P.x - X;
D.y = P.y - Y;
%% visualization
figure(1);hold on; axis equal;
plot(A.x,A.y,'ro',B.x,B.y,'bo',C.x,C.y,'go',D.x,D.y,'yo');
plot([A.x B.x],[A.y B.y],'-k');
plot([B.x C.x],[B.y C.y],'-k');
plot([B.x D.x],[B.y D.y],'-k');


-
Does this help? tex.stackexchange.com/questions/19348/… – Torbjørn T. Aug 16 '12 at 11:08
In that link one has a point (P) outside the line LAB and wants to draw a line perpendicular to LAB through the point P, but I don't have the point (P)! I need to calculate it somehow given the length... Anyway, thanks for the link. – Rasoul Aug 16 '12 at 11:11
Could you add a sketch (possibly hand-drawn) of what you want? I'm having trouble visualising it. In particular, it would seem that len is unbounded as I can continue drawing the perpendicular as long as I like in the positive direction. Also, how automatic do you want it to be? Can you input the information as to which side of the line is positive or do you want TikZ to work that out? – Loop Space Aug 16 '12 at 11:24

I'm not entirely sure whether I understood what you're trying to do, but perhaps the calc library can be of help here. To draw a line starting at point (B) perpendicular to the line between point (A) and (B) with a given <length>, you can use the syntax \draw (B) -- ($(B)!<length>!-90:(A)$);

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}[dot/.style={circle,inner sep=1pt,fill,label={#1},name=#1}]

\node [dot=A] at (0,0) {};
\node [dot=B] at (3,1) {};

\draw (A) -- (B);

\draw (B) -- ($(B)!1cm!-90:(A)$);

\end{tikzpicture}
\end{document}

-
Thanks a lot! This is what I was looking for... By the way! Where can I find the documentation for the calc library? – Rasoul Aug 16 '12 at 11:56
@Rasoul: In the pgfmanual starting on p. 134 of the current version. – Jake Aug 16 '12 at 11:57
This works for perpendicular to from a point. Wow does this works when i want perpendicular to a unknown point on a line. let say a line from '(2,-2) -- (3,2)' and i want a ine from '(0,0)' to somewhere on this line. What do i have to do? – Jan-Bert Apr 14 '15 at 7:12
@Jan-Bert: See tex.stackexchange.com/questions/19348/… – Jake Apr 15 '15 at 17:00

Just for fun with PSTricks.

\documentclass[pstricks,border=12pt,12pt]{standalone}
\usepackage{pst-eucl}

\def\y{2*x-2}

\begin{document}
\begin{pspicture}(3,3)
\pstGeonode[CurveType=polyline](*1 {\y}){A}(*2 {\y}){B}
\pcline(B)([offset=-1]{A}B)
\end{pspicture}
\end{document}


-