问题描述:求点到直线的垂直距离
% 求点d1到直线d2-d3的距离。
function [d,dl] = VerticalDis(d1,d2,d3)
x1 = d1(1,1); y1 = d1(1,2);
x2 = d2(1,1); y2 = d2(1,2);
x3 = d3(1,1); y3 = d3(1,2);
if x2 == x3
x4 = x1 – 1;
y4 = y1;
elseif y2 == y3;
x4 = x1;
y4 = y1 – 1;
else
k23 = (y3-y2)/(x3-x2);
k1 = -1/k23;
x4 = x1 + 1;
y4 = k1*x4 + y1 – k1*x1;
end
d4 = [x4,y4];
[xx,yy] = node_solve(d1,d4,d2,d3);
dl = [xx,yy];
d = norm(dl-d1);
end
function [X Y] = node_solve( X1,Y1,X2,Y2 )
if X1(1) == Y1(1)
X = X1(1);
k2 = (Y2(2)-X2(2))/(Y2(1)-X2(1));
b2 = X2(2)-k2*X2(1);
Y = k2*X+b2;
end
if X2(1) == Y2(1)
X = X2(1);
k1 = (Y1(2)-X1(2))/(Y1(1)-X1(1));
b1 = X1(2)-k1*X1(1);
Y = k1*X+b1;
end
if X1(1) ~= Y1(1) & X2(1) ~= Y2(1)
k1 = (Y1(2)-X1(2))/(Y1(1)-X1(1));
k2 = (Y2(2)-X2(2))/(Y2(1)-X2(1));
b1 = X1(2)-k1*X1(1);
b2 = X2(2)-k2*X2(1);
if k1 == k2
X = [];
Y = [];
else
X = (b2-b1)/(k1-k2);
Y = k1*X+b1;
end
end
end