English 中文(简体)
point in a self-intersecting / complex polygon
原标题:

I ve read How can I determine whether a 2D Point is within a Polygon? 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?

问题回答

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 :).

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.]

The referenced intersection algorithm works for any closed polygon, even if concave or self-intersecting. In order for your two-box polygon to be closed (starting and ending at the same point), the middle segment must be traversed twice. That means that your example ray going through the bottom crosses three edges, so it is inside using the odd-even rule.





相关问题
Bounding ellipse

I have been given an assignement for a graphics module, one part of which is to calculate the minimum bounding ellipse of a set of arbitrary shapes. The ellipse doesn t have to be axis aligned. This ...

Line segment in a triangle

How can we check if a line segment falls partially or fully inside a triangle? Cheers.

Line Segments from a point

I have a point p, and 2 line segments in a 2D plane. Point p is a location of view from where camera is looking towards the line segments. I want to check if line segment 1 is partially or fully ...

Creating a surface of triangles from a set of 2D points

I have a set of points and I need to convert the set to (non-overlapping) triangles (or a big polygon if equivalent)... The application: I have a list of locations (latitude,longitude) from a country,...

Radial plotting algorithm

I have to write an algorithm in AS3.0 that plots the location of points radially. I d like to input a radius and an angle at which the point should be placed. Obviously I remember from geometry ...

Delete holes in a Polygon

I have a polygon determined by an Array of Points. This polygon is crossing itself making some holes in the polygon itself. My questions is: How can I omit this holes and just get the exterior ...

Finding cycle of 3 nodes ( or triangles) in a graph

I am working with complex networks. I want to find group of nodes which forms a cycle of 3 nodes (or triangles) in a given graph. As my graph contains about million edges, using a simple iterative ...

热门标签