Distance of a Point to a Segment

Requires a Wolfram Notebook System
Interact on desktop, mobile and cloud with the free Wolfram Player or other Wolfram Language products.
The distance of a point to a segment is the length of the shortest line joining the point to a point on the segment. This Demonstration depicts this shortest line as dotted. The measurements are normalized so that the point starts at a distance of 1 to the segment.
Contributed by: Jaime Rangel-Mondragon (March 2011)
Open content licensed under CC BY-NC-SA
Snapshots
Details
To compute the distance from point p to segment ab (all as complex numbers) compute first z=(p-a)/(b-a). If 0 ≤ Re[z] ≤ 1 then the distance is equal to Abs[Im[z](b-a)]. If not, it is equal to the smallest of the distances from p to a or to b.
Permanent Citation
"Distance of a Point to a Segment"
http://demonstrations.wolfram.com/DistanceOfAPointToASegment/
Wolfram Demonstrations Project
Published: March 7 2011