views:

374

answers:

2

I've read http://stackoverflow.com/questions/217578/point-in-polygon-aka-hit-test but I'm not sure if the solution would apply to a polygon that is divided down the middle by an interior segment. Think of a squared-off figure 8 or simply two squares stacked on one another. A point inside either square would surely be "inside" the polygon but the crossing count would differ depending on which direction you went (and whether you crossed that interior segment).

sample polygon

I suppose one way of dealing with this is to treat the polygon as two separate polygons... (in which case I'll need an algorithm to divide a complex polygon into a set of simpler ones?)

Or is there a refinement to the ray-casting algorithm, or another point-in-polygon algorithm, to deal with the case I described?

A: 

I'm not sure if this is the optimal solution; but the ray-casting algorithm works for any convex polygon. Any polygon can be decomposed into triangles, which are convex. (The double box is not a convex polygon, since if you connect two of the vertices with a line segment, in some cases you will cross over the center edge.) So, to clarify: first decompose the polygon into triangles, then use ray-casting to determine whether the point is inside a triangle.

[Edit: ray-casting does work for concave polygons. Sorry, I was mistaken.]

RMorrisey
The op describes a polygon that is even more than concave, it's degenerated ;>
Kornel Kisielewicz
actually, the algorithm is supposed to work for not just convex polygons but also concave and complex polygons as well. my understanding is that a complex polygon is one where the polygon intersects itself. (instead of 2 stacked squares imagine 2 stacked triangles standing point to point).i will also look into polygon triangulation as a method for decomposing the complex polygon into a set of convex ones.
emh
You're right, sorry, my mistake
RMorrisey
+1  A: 

The algorithm described will work fine, cause if you take a look closer at it, you see that it's just the number of crossings that counts. If we start in either "sub-polygon" of the "8" we will cross in worst case the edges 3 times, normally once. And it's true that it's inside. Otherwise it's outside.

However, one may assume that there's one special case. If the ray goes EXACTLY through a crossing point. But note that in that case, you'll ALSO get 2 intersections :).

Kornel Kisielewicz
how would you cross 3 times? if my point is in the top square and i draw my ray pointing up i cross once (and so the point is inside) but if i draw my ray down i cross twice: once through the center segment and once through the bottom segment.
emh
I think you'd need to post a picture to clear things up.
Kornel Kisielewicz
image added above - see how the ray would cross either once or twice depending on which direction you went?
emh
Aaah, so that's the case... in this case you have edges that are clearly "interior". Mark them as such, and don't include in interiority tests.
Kornel Kisielewicz
forgive me if this is a stupid question but how do i algorithmically test to see if an edge is interior? (i thought about testing a midpoint of the line to see if it is inside the polygon but then i run into a catch-22, particularly if there is more than one interior edge).thus far my google searches have turned up nothing.
emh
this looks like it might help:http://alienryderflex.com/polygon_perimeter/
emh