LifeTally
Closest point and distance to a segment
Where is the closest point on your finite segment, and how far is your point from it?
Enter segment endpoints A and B and a point P. Find the closest point Q, its position from A toward B and the straight-line distance P–Q.
Your segment and point
Use the same perpendicular Cartesian axes, equal scale and one coordinate unit for A, B and P. Enter decimal fractions, not a/b. Endpoints keep their entered order. Vertical, horizontal and collapsed segments are allowed.
How the estimate works
Let d = B − A and u = P − A. For distinct endpoints, w = d·d > 0 and t = min(1, max(0, (u·d)/w)). The closest point is Q = A + t d; distance is √((Px − Qx)² + (Py − Qy)²). Position 0 is A and 1 is B; an interior t lies strictly between them. The squared distance along A + t d is a quadratic with its unconstrained minimum at (u·d)/w, so restricting t to [0,1] gives the closest point on the finite segment. If A = B, the segment is one point: Q = A and we choose t = 0 as a convention. Distance always needs all six coordinates. For a noncollapsed segment all six are also needed for t, Q and location. For known coincident endpoints, t = 0, Q and the collapsed status remain known even with invalid P; its errors stay visible and distance remains unknown. Private exact decimal fractions of accepted numerical inputs are used before final rounding; rounded t or Q is never reused.
d = B − A; u = P − A; w = d · d
w > 0: t = min(1, max(0, (u · d) ÷ w))
Q = A + t d; |P − Q| = √((Px − Qx)² + (Py − Qy)²)
A = B: Q = A; t = 0Only one declared point and one closed finite straight segment in a two-dimensional Euclidean plane. Both endpoints belong to the segment. Coincident endpoints and a point on the segment are valid; a point on it has distance zero. This does not calculate distance to an infinite line, ray, polyline, curve, 3D object, geographic location or road route. No graph, unit conversion, measurement validation, uncertainty estimate or physical fit/safety guarantee. Exact comparisons use accepted numerical values without a near-zero tolerance; they do not prove properties of measured objects.
CGAL: closed plane segment and coincident endpoints ↗OpenStax: dot product and vector projection ↗
Check the filled example
Our chosen example: A = (−2, 1), B = (4, 1), P = (1, 5). Here d = (6, 0), u = (3, 4), u·d = 18 and d·d = 36. Thus t = 0.5, Q = (1, 1) lies inside the segment and distance P–Q = 4 coordinate units. These are mathematical example coordinates, not measured or recommended dimensions.
Accepted input values
- Endpoint A: x coordinate
- -2
- Endpoint A: y coordinate
- 1
- Endpoint B: x coordinate
- 4
- Endpoint B: y coordinate
- 1
- Point P: x coordinate
- 1
- Point P: y coordinate
- 5
Plan the next step
Use the standard calculator for separate arithmetic. Check the endpoint order, intended axes and consistent units before interpreting the geometry.
Open the standard calculator →