Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating distance to a path

I have a set of points that form a path. I would like to determine the minimum distance from any given point to this path. The path might look something like this:

points = [
    [50, 58],
    [53, 67],
    [59, 82],
    [64, 75],
    [75, 73]
];

where the first value is the x coordinate and the second the y coordinate. The path is open ended (it will not form a closed loop) and is made of straight segments between the points.

So, given a point, eg. [90, 84], how to calculate the shortest distance from that point to the path?

I'm not necessarily looking for a complete solution, but any pointers and ideas will be appreciated.

like image 782
Tatu Ulmanen Avatar asked Apr 15 '11 13:04

Tatu Ulmanen


1 Answers

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.

like image 155
Jeffrey L Whitledge Avatar answered Oct 10 '22 08:10

Jeffrey L Whitledge