Calculating distance to a path

It is possible to construct pathological cases in which the closest line segment to a point P connects two points which are themselves farther from P than any other points in the path. Therefore, unless I am missing something very subtle, you must calculate the distance to each line segment to get the shortest distance to the path.

Here is a simple example:

(5,1)-(4,2)-(1,3)-(20,3)-(15,2)-(14,1)

Given point (10,1), the closest distance to the path would be to the point (10,3), which is along the line segment (1,3)-(20,3), but those two points are farther from (10,1) than any other point in the path.

So I don't believe there are any shortcuts to the naïve algorithm of finding the distance to each line segment and taking the minimum.


The best thing would be to find the closest point from a line (making the path) measure the distance and move on along the path (and store the point for shortest distance).

demo