% distance of a point T to line segment K(X,Y) Usage: dist = calc_distance_3D_point_from_linesegment(T,X,Y) T, X, Y - 3D points, euclidean dist - double, shortest distance from T to segment X-Y Wolfgang Förstner wfoerstn@uni-bonn.de See also calc_distance_3D_point_from_3D_line, calc_distance_3D_point_from_triangle
0001 %% distance of a point T to line segment K(X,Y) 0002 % 0003 % Usage: 0004 % dist = calc_distance_3D_point_from_linesegment(T,X,Y) 0005 % 0006 % T, X, Y - 3D points, euclidean 0007 % dist - double, shortest distance from T to segment X-Y 0008 % 0009 % Wolfgang Förstner 0010 % wfoerstn@uni-bonn.de 0011 % 0012 % See also calc_distance_3D_point_from_3D_line, calc_distance_3D_point_from_triangle 0013 0014 function dist = calc_distance_3D_point_from_linesegment(T,X,Y) 0015 0016 % direction 0017 Lh = Y - X; 0018 t = (T-X)'*Lh/norm(Lh)^2; 0019 if t < 0 0020 dist = norm(T - X); 0021 else 0022 if t>1 0023 dist = norm(T - Y); 0024 else 0025 L0 = cross(X,Y); 0026 dist = norm(cross( T,Lh)-L0)/norm(Lh); 0027 end 0028 end 0029