Computing closest edge of a point within a polygon that is constructed with lat/longs?

768 Views Asked by At

So here's what I'm trying to do:

Given a collection of lat/long coordinates that form a polygon like below, I want to be able to select a point inside the polygon and determine which side of the polygon it is closest to, and determine its distance to this closest edge. My problem is that I need to somehow get rid of the extra coordinates so I can do computation on them -- if, for instance, I only had four points that formed a four sided polygon (i.e. the west-most and east-most points on O'Farrell, and the west-most and east-most points on Ellis), I could do this computation. For instance, putting in the point for 'JINS' as input would return the edge that runs along O'Farrell St. We can assume that the polygon will not be smooth, and that each polygon represents a city square block, so they're relatively simple polygons.

Can anyone think of a solution to this problem (whether that be getting rid of extra points or finding some other way to do this computation)?

Thanks.

enter image description here

1

There are 1 best solutions below

2
On

I don't think you need to get rid of any points.

Suppose you have a set of $n$ edges, with the $n$th edge defined by its vertices $(x_{1n},y_{1n})$ and $(x_{2n},y_{2n})$.

For each edge, find the line that passes through it vertices.

Find the point on this line that is closest to your point. Check if the point you have just found lies on the edge (is it between the two vertices) or not. If not, then discard it as a candidate for closest point. If it is, compare against shortest distance so far.

Repeat for all edges.

It is possible that the closest point is at a vertex rather than along an edge. Check distances to all vertices to see if any of these are closer to your point inside the polygon.