rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
dist_point_line.m
1 function d = dist_point_line(q, p1, p2)
2 %function d = dist_point_line(q, p1, p2)
3 %
4 % function computing the distance of the 2d-point q from the line
5 % through the points p1, p2.
6 % If q,p1,p2 is a matrix with columnwise points, then a vector of
7 % distances is generated.
8 
9 % Bernard Haasdonk 10.5.2007
10 
11 
12 if size(p1,1)== 2
13  p1 = p1';
14 end;
15 
16 if size(p2,1)== 2
17  p2 = p2';
18 end;
19 
20 if size(q,1)== 2
21  q = q';
22 end;
23 
24 if (size(p1,2)~=2) || (size(p2,2)~=2) || (size(q,2)~=2)
25  error('Only 2d points acceptable in distance computation');
26 end;
27 
28 % d = area(triangle(q,p1,p2)*2 / dist(p1,p2))
29 
30 % d = zeros(size(p1,2),1);
31 A = abs(area_triangle(q,p1,p2));
32 dp1p2 = p1 - p2;
33 d = A * 2 .* sqrt(dp1p2(:,1).^2 + dp1p2(:,2).^2).^(-1);
34 
35 %| \docupdate