This function returns the shortest distance from a 3D point P to a line segment defined by two 3D points A and B. It projects P onto the line, then checks if this projected point lies between A and B. If it does, then the distance is calculated using this projected point.
If the projection is outside the line segment, then the distance is calculated to either point A or B, depending on which is closer.
I wanted to get feedback on if this is a clean solution to the problem.
function d = point_seg_dist(P, A, B) % Returns distance from a 3D point to a line segment % % Inputs % % P : vector % Position in space % % A : vector % Position of line segment endpoint % % B : vector % Position of other line segment endpoint % % Outputs % % d : double % Distance from point to line segment AP = P - A; % Vector from A to point AB = B - A; % Vector from A to B % Project point onto line P_line = A + dot(AP, AB) / dot(AB, AB) * AB; if all(A < P_line) && all(P_line < B) % The point projected onto the line is in between A and B % Projection of point onto segment is the same % as projection of point onto line P_seg = P_line; else % The point projected onto the line is outside of A and B if all(P_line <= A) % The projected point is closer to A P_seg = A; else % The projected point is closer to B P_seg = B; end end d = norm(P - P_seg); % Distance to line segment end