开发者

Given a polygon and a point in 2D, how can one find the feature (vertex or edge) of the polygon closest to the point?

A naive approach is to find, for each edge in the polygon, the point on that edge closest to the given point, and then take the one that's closest. Is there a faster algorithm? My goal is to implement a 2D Super Mario Galaxy-style platformer.

Apparently this can be done with Voronoi regions, as in this video: http://开发者_运维问答www.youtube.com/watch?v=Ldh2YKobuWo

However, I can't find any Voronoi algorithms that deal with edges as well as points. Ideas?


Calculate the point-line distance for each of the edges, then pick the shortest one. There is no shortcut. This site has a good explanation and even implementations in various languages.

However, finding "the point on that edge closest to the given point" is a computationally unnecessary intermediate result.


If the polygon is convex, then the overhead of the voronoi calculation far exceeds that of the naive approach.

If this is run many times, and each time the point changes slightly, you only need to check 3 segments (think about it: as you move around, assuming many checks, then the closest edge will only change to an adjacent edge)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜