Problem
Given a 2D point P(x, y) and a line segment with endpoints A(x1, y1) and B(x2, y2), compute the minimum Euclidean distance from point P to the segment AB.
Input
-
Real numbers (or integers) representing coordinates of
P
,
A
, and
B
.
Output
-
A single floating-point number: the minimum distance from
P
to segment
AB
.
Notes / Edge cases
-
If the perpendicular projection of
P
onto the infinite line
AB
falls
within
the segment, the answer is the perpendicular distance.
-
Otherwise, the answer is
min(dist(P, A), dist(P, B))
.
-
Handle degenerate segments where
A == B
(distance is
dist(P, A)
).
Constraints (assume)
-
Coordinates are within a reasonable range (e.g.,
[-1e9, 1e9]
).
-
Use double precision.