Find current checkpoint on route

50 Views Asked by At

I have a directed graph in a (O, x, y) cartesian coordinate system representing a physical path that the user wants to follow.

The user, whose position is represented by the filled-in circle, walks along the directed graph. In reality, the user is rarely located exactly on top of a segment. Rather, the user's location is slightly left or right (or above or below) of the current (the closest) segment.

At any given point in time, how would I find the segment (0, 1, 2, 3, 4, or 5) that the user is the closest to?

user positionning problem setup

Instinctively, I would divide the coordinate system into zones at the segment intersections, but that seems costly in terms of computational complexity. zones calculations Thanks for your help,

1

There are 1 best solutions below

2
On BEST ANSWER

What do you mean by "costly in terms of computational complexity"?

These zones are Voronoi diagrams of line segments and if you search a bit there are many more references. The math involved is "conceptually complicated" (at least for me) but the computation involved is not "computationally complex" - I'm pretty sure it's polynomial in input size. There also seems to be (but I'm not 100% sure) existing software packages which compute these.

BTW the zones are not necessarily polygons, i.e. they may not have straight edges, at least at the beginning and end of the path. E.g. imagine your segment $5$ extending a long way, and your segment $0$ being perpendicular to it with the beginning point closer to but not touching segment $5$. Then the "equi-distant contour" or "divider" will be at least partly a parabola.

OTOH if you don't need to classify the plane into zones, but you're only interested in computing the nearest segment for a few positions, then you can just brute force it. If the user is at position $P$, his/her distance to a segment $\overline{QR}$ can be easily computed:

  • First project $P$ onto the infinite line $QR$ (extended in both directions).

  • If $P$ projects onto the segment then the perpendicular distance is the answer.

  • Otherwise the distance is $\min(dist(P,Q), dist(P,R))$.

All these are constant-time operations, so if you don't mind calculating these for every segment, you can find the closest one. I.e. if you have $m$ segments and $n$ user positions you care about, everything can be answered in time $O(mn)$.