-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdist_2d.m
29 lines (23 loc) · 849 Bytes
/
dist_2d.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% DIST_2D Distance from a 2D point to a line in the plane
%% function [dist, phat] = dist_2d(p, abc)
%%
%% INPUT:
%% p - 2D point, [x y]
%% abc - line equation, ax + by + c = 0, or [a b c]
%%
%% OUTPUT:
%% dist - closest Euclidean distance
%% phat - location of closest point on line.
%%
%% DESCRIPTION:
%% https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line
%% See "cartesian coordinates".
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [dist, phat] = dist_2d(p, abc)
den = (abc(1)^2 + abc(2)^2);
dist = abs(abc(1)*p(1) + abc(2)*p(2) + abc(3)) ./ sqrt(den);
px = (abc(2)*(abc(2)*p(1) - abc(1)*p(2)) - abc(1)*abc(3)) ./ den;
py = (abc(1)*(-abc(2)*p(1) + abc(1)*p(2)) - abc(2)*abc(3)) ./ den;
phat = [px py];
end