Search Ctrl K
Home / Tools
THE FEUTEX TOOLKIT

Nearest Point on a Line Segment

Find the nearest point on a finite line segment, including endpoint cases.

Geometry workspace● Runs in your browser
01 Your input
02 Your result
View or copy raw result data
FormulaParameter = max(0,min(1,((x-ax)*(bx-ax)+(y-ay)*(by-ay))/((bx-ax)**2+(by-ay)**2))); Nearest X = ax+(max(0,min(1,((x-ax)*(bx-ax)+(y-ay)*(by-ay))/((bx-ax)**2+(by-ay)**2))))*(bx-ax); Nearest Y = ay+(max(0,min(1,((x-ax)*(bx-ax)+(y-ay)*(by-ay))/((bx-ax)**2+(by-ay)**2))))*(by-ay); Distance = hypot(x-ax-(max(0,min(1,((x-ax)*(bx-ax)+(y-ay)*(by-ay))/((bx-ax)**2+(by-ay)**2))))*(bx-ax),y-ay-(max(0,min(1,((x-ax)*(bx-ax)+(y-ay)*(by-ay))/((bx-ax)**2+(by-ay)**2))))*(by-ay))
Sources and model references

Worked example

Inputs

AX
0 number
AY
0 number
BX
4 number
BY
0 number
X
6 number
Y
3 number

Expected results

Parameter
1 number
Nearest X
4 number
Nearest Y
0 number
Distance
3.605551275 length

Load the example in the workspace to reproduce these results. Display rounding does not change the underlying calculation.

How to use this tool

  1. Enter the labeled inputs using consistent units.
  2. Check the stated assumptions and calculate.
  3. Review each output and compare another input scenario.

Try this example

{
  "ax": 0,
  "ay": 0,
  "bx": 4,
  "by": 0,
  "x": 6,
  "y": 3
}

Accuracy and scope

Projection is clamped to the finite segment; parameter 0 or 1 selects an endpoint. Finite floating-point arithmetic; results can lose precision near singular or extreme inputs.

Is my data uploaded?

This tool runs locally in your browser. Input and output are not sent to a processing server. Copy or download your result before leaving the page.