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
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
| Symbol | Meaning | Unit |
|---|---|---|
px | Point P, x | — |
py | Point P, y | — |
ax | Line point A, x | — |
ay | Line point A, y | — |
bx | Line point B, x | — |
by | Line point B, y | — |
D | Distance from P to the line | — |
DS | Distance from P to the segment AB | — |
SD | Signed distance (positive = P is left of A→B) | — |
FX | Foot of the perpendicular, x | — |
FY | Foot of the perpendicular, y | — |
T | Position 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.