简体   繁体   中英

How does the Even-Odd Algorithm count polygon edges?

I am wondering how the even-odd algorithm works for identifying a point in a complex polygon.

What I know right now is that it will do the horizontal search from most left to the point and count the number of edges touched.

However, what happens if the edges touched is in an intersection of 2 edges? how does it count?

Example of polygons:

多边形的例子

The way I like to do it, which works for integer as well as floating-point coordinates, is:

  • for non-horizontal segments, each segment includes its bottom-most point but not its top-most point.

  • do not include the horizontal segments at all in the even-odd count.

This ensures that the even-odd count is correct for every point inside the polygon and every point outside, but it's not entirely consistent about points exactly on the boundary. If it matters to you, you may want to add a rule that any point that is actually on a segment is included in the polygon.

there are more ways of dealing with this problem but the safest I know of is to

Change ray direction slightly

this is numerically safe but implementation is not as easy as it sounds. Either change entire ray and compute from the start or change somewhere before the hit in question.

改变射线方向

You need to ensure that you do not form close loops for example by zig zag pattern (so you alternate between turning CW and CCW within some cone from the original direction).

In case where ray is exactly parallel to and also touching an edge of your polygon either ignore such edge or count it twice or change the ray direction again.

Changing ray direction is always safe as it avoids singularities and numerical instabilities.

btw. This inside polygon algorithm you are using is well known by name Hit test

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM