File Exchange

## Perpendiculars to line segments

version 1.0.0 (65.3 KB) by
Calculate the foot points fP of perpendiculars and the minimal distances D from a point P to a 2D or 3D line segment.

Updated 25 Dec 2020

FEATURES.
• 2D or 3D line segments
• Output of perpendicular foot coordinates
• Output of minimal distances to line segments
• Indicator for perpendicular foot position

INPUT:
P : Reference point, P=[xr,xr] or P=[xr,yr,zr]
sP: Starting points, sP=[Xs,Ys] or sP=[Xs,Ys,Zs]
eP: Ending points, eP=[Xe,Ye] or eP=[Xe,Ye,Ze]

OUTPUT:
fP: Foot points X,Y where the perpendiculars hits the line segments
If perpendicular is outside of line segment, take closest data point.
D : Distances from P to line segments
outside: Indicator for pependicular position
outside 0: Perpendicular inside line segment
outside 1: Perpendicular outside start of line segment
outside 2: Perpendicular outside end of line segment

EXAMPLE (2D):
P=[0,0];sP=[-4,1;-4,2;-1,2];eP=[-2,1;-2,4;1,2];
[fP,D,outside]=Perpendicular2Line(P,sP,eP)

The basic principle is from Ken Eaton, https://stackoverflow.com/questions/43991659/how-do-i-compute-the-shortest-distance-from-a-point-to-line-segments

### Cite As

Peter Seibold (2020). Perpendiculars to line segments, MATLAB Central File Exchange. Retrieved December 25, 2020.

##### MATLAB Release Compatibility
Created with R2016a
Compatible with R2020a and later releases
##### Platform Compatibility
Windows macOS Linux