Paritian

Mathematics

Distance from a Point to a Line Calculator

The shortest distance from a point to a line through two points, the distance to the segment between them, and the exact spot where the perpendicular lands.

Results

Distance from P to the line 2.2361
Distance from P to the segment AB 2.2361
Signed distance (positive = P is left of A→B) 2.2361
Foot of the perpendicular, x 4.0000
Foot of the perpendicular, y 2.0000
Position of the foot along AB (0 = A, 1 = B) 0.4000

What this tool does

The shortest path from a point to a line is the perpendicular, and its length is the cross product of AB and AP divided by the length of AB. Its sign tells you which side of the line the point is on. A line goes on forever, a segment does not: if the foot of the perpendicular falls outside AB (t below 0 or above 1), the nearest point of the segment is an end, and the segment distance is longer than the line distance. This is the test behind snapping to a path, collision checks and the cross-track error of a route.

Formula

d = |(x_B − x_A)(y_P − y_A) − (y_B − y_A)(x_P − x_A)| ÷ |AB| · foot = A + t (B − A), t = AP·AB ÷ |AB|²

Variables

SymbolMeaningUnit
pxPoint P, x—
pyPoint P, y—
axLine point A, x—
ayLine point A, y—
bxLine point B, x—
byLine point B, y—
DDistance from P to the line—
DSDistance from P to the segment AB—
SDSigned distance (positive = P is left of A→B)—
FXFoot of the perpendicular, x—
FYFoot of the perpendicular, y—
TPosition of the foot along AB (0 = A, 1 = B)—

Worked example

  • Point P, x3
  • Point P, y4
  • Line point A, x0
  • Line point A, y0
  • Line point B, x10
  • Line point B, y5
  • Distance from P to the line2.2361
  • Distance from P to the segment AB2.2361
  • Signed distance (positive = P is left of A→B)2.2361
  • Foot of the perpendicular, x4.0000
  • Foot of the perpendicular, y2.0000
  • Position of the foot along AB (0 = A, 1 = B)0.4000

Limitations

  • Mixing units is the most common source of error. Convert every input to the units shown next to each field before calculating.
  • The calculation runs at full precision and only the display is rounded. If you copy an intermediate value and retype it, small differences can appear.