如何在2D中获得指向线段距离?

时间:2014-09-12 03:32:26

标签: matlab geometry

我只是想知道在Matlab中是否有一种简单的方法来获取2D和线段之间的距离?

1 个答案:

答案 0 :(得分:3)

x = [0,0]; %some point
a = [1,2]; %segment points a,b
b = [3,5];

d_ab = norm(a-b);
d_ax = norm(a-x);
d_bx = norm(b-x);

if dot(a-b,x-b)*dot(b-a,x-a)>=0
    A = [a,1;b,1;x,1];
    dist = abs(det(A))/d_ab;        
else
    dist = min(d_ax, d_bx);
end