|
a |
|
b/point_to_line.m |
|
|
1 |
% This functions calculates the perpendicular distance between line1 and |
|
|
2 |
% line 2 on every common region of these lines. |
|
|
3 |
% Here lines are input in this format = [x_start x_end y_start y_end] |
|
|
4 |
|
|
|
5 |
|
|
|
6 |
function D = point_to_line(line_1, line_2) |
|
|
7 |
|
|
|
8 |
% Slope of line 1 |
|
|
9 |
m1 = ( line_1(4) - line_1(3) )/ ( line_1(2) - line_1(1) ); |
|
|
10 |
|
|
|
11 |
% Slope of line 1 |
|
|
12 |
m2 = ( line_2(4) - line_2(3) )/ ( line_2(2) - line_2(1) ); |
|
|
13 |
|
|
|
14 |
% Intercept of line 1 |
|
|
15 |
c1 = line_1(3) - m1 * line_1(1); |
|
|
16 |
|
|
|
17 |
% Intercept of line 2 |
|
|
18 |
c2 = line_2(3) - m2 * line_2(1); |
|
|
19 |
|
|
|
20 |
% Common region between line 1 and 2 |
|
|
21 |
X1 = (max([line_1(1) line_2(1)]):min([line_1(2) line_2(2)]))'; |
|
|
22 |
|
|
|
23 |
% Define a function for a line |
|
|
24 |
Line_Fun = @(x, m, c) m.*x + c; |
|
|
25 |
|
|
|
26 |
% Slope of the line perpendicular to line 2 |
|
|
27 |
m1_per = tan(pi/2 + atan(m2)); |
|
|
28 |
|
|
|
29 |
% Calculate Y cordiantes on the line 1 |
|
|
30 |
Y1 = Line_Fun(X1, m1, c1); |
|
|
31 |
|
|
|
32 |
% y-Intercept of the line perpendicular to line 2 passing throught every |
|
|
33 |
% (X, Y1) |
|
|
34 |
c1_per = Y1 - m1_per * X1; |
|
|
35 |
|
|
|
36 |
% Perpendicular line meets line 2 at (X0, Y0) for every (X, Y1) |
|
|
37 |
X0 = -(c1_per - c2)/(m1_per - m2); |
|
|
38 |
Y0 = (m1_per*c2 - m2*c1_per)/(m1_per - m2); |
|
|
39 |
|
|
|
40 |
% Calculate distance for every (X, Y1) |
|
|
41 |
D = sqrt((X1 - X0).^2 + (Y1 - Y0).^2); |
|
|
42 |
end |