Perpendicular to an infinite line & distance to line segment

Calculate the foot point of perpendiculars on infinite lines and calculate the minimal distances D from a point P to a 2D or 3D line segment
Updated 20 Sep 2021

View License

  • 2D or 3D infinite lines and line segments
  • Output of perpendicular foot coordinates for infinite lines and distance to point
  • Output of minimal distances to line segments
  • Indicator for perpendicular foot position for line segments
  • Process multiple lines at once
2D 3D
P : Reference point, P=[xp, xp] or P=[xp, yp, zp]
A1: Starting points of line segments, A1=[X1, Y 1] or A1=[X1, Y1, Z1]
A2: Ending points of line segments, A2=[X2, Y 2] or A2=[X2, Y2, Z2]
F: Foot points X, Y where the perpendiculars hits the infinite line.
D: Distances from P to lines (length of perpendiculars)
Fs: Point on line segment closest to P. May be, but must not be the foot point F.
Ds: Shortest distance from P to line segments
outside: Indicator for perpendicular 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, three line segments:
P=[0,0]; %Reference point
A1=[ -4, 1 ; %x1 and y1 of 1st segment
-4, 2 ; %x1 and y1 of 2nd segment
-1, 2 ]; %x1 and y1 of 3rd segment
A2=[ -2, 1 ; %x2 and y2 of 1st segment
-2, 4 ; %x2 and y2 of 2nd segment
1, 2 ]; %x2 and y2 of 3rd segment
[F, D, Fs, Ds, outside]=Perpendicular2Line(P, A1, A2)
See also the demonstration file Examples.m and ReadMe.pdf

Cite As

Peter Seibold (2021). Perpendicular to an infinite line & distance to line segment, MATLAB Central File Exchange.

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

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!


Version Published Release Notes

Added perpendiculars to infinite lines