Calculate and plot the shortest distance (norm) between a point and a line in 3D space
20 views (last 30 days)
Show older comments
I have a point P with coordinates [X Y Z] and a line AB (the coordinates of A and B are known)
I would like to calculate and to plot the segment representing the shortest distance between P and the line AB.
I am aware of matlab functions to calculate this distance (like this), but I do need to plot the segment in the 3D space.
I guess for this taks I need to find the coordinates of the point C along the line AB that is the end point of the segment connecting P to AB. How can I find these coordinates?
Thank you very much.
0 Comments
Accepted Answer
Matt J
on 21 Apr 2022
Edited: Matt J
on 21 Apr 2022
A=A(:); B=B(:); P=P(:); %column vectors
C=A + (B-A)\(P-A)*(B-A); %nearest point
xyz=num2cell([P,C],2);
line(xyz{:})
2 Comments
Bruno Luong
on 21 Apr 2022
@Giuseppe Matt's formula is correct and he already shows you plot the shortest line P to C, a projection of P on the line AB (not a segment).
More Answers (1)
KSSV
on 21 Apr 2022
It is an easy task to achieve. You can find the foot of the perpendicular. Refer this:
See Also
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!