is doing a project, to determine whether the point is within the polygon, the implementation of the language can be C # or Java, two are required, in fact, both languages have their own class library method can be judged, Baidu can be found, polygon needs to consider concave polygons, There is a better understanding of the implementation method is through the single-sided Ray and Polygon edge intersection of the odd even to judge, if the intersection point is an odd number can be judged in the polygon, but this is only a general idea, the specific implementation also to consider the special case of intersection with the endpoint, we take the horizontal unilateral ray as an example, the following steps:
1. Traverse all edges (at least one end of the horizontal axis is less than the horizontal axis of the point), if the point on an edge, the direct exit, you can return 0 to represent;
2. In the most general case, the intersection point is not the polygon end point, accumulate the intersection number;
3. If there is an intersection point for the endpoint, in order to make the algorithm effective, we need to define what is a valid intersection, the ordinate is greater than or equal to the side of the ordinate, the ordinate is less than the points of the ordinate is one side, only the intersection of the end of the edge satisfies the ray on both sides of the condition is valid intersection
4. If the number of intersections is odd, within the polygon, otherwise outside the polygon (including 0);
The Java implementation is as follows
/** * Determines whether a point is within a polygon and can be applied to a complex concave polygon * @return If return 0 on an edge, 1 internally, returns 1 */public int Isinpolygon (double x, double y, double[] Xarra Y, double[] yarray) {int sidescount = xarray.length, if (Sidescount < 3) return-1;//calculates the number of intersections using the horizontal left ray int intersectioncount = 0;for (int i = 0; i < Sidescount; i++) {int J = (i + 1)%sidescount;//i,j represents two endpoints of an edge if (Xarray[i] > x && xarray[j] > x)//Exclude the right side continue;if ((Y-yarray[i]) * (Y-yarray[j]) > 0)//exclude disjoint edges continue;if ((Y-yarray[i]) * (x-xarray[j]) = = ( Y-YARRAY[J]) */(X-xarray[i])//To determine whether to point on the side return 0;if (Yarray[i] < y | | yarray[j] < y)//Determine whether to point at the sides of the side intersectioncount++ ;} if (intersectioncount% 2 = = 1) return 1;else return-1;}
Time is a little late also did not try, if there are problems follow up and perfect
Reference: http://blog.csdn.net/hjh2005/article/details/9246967
Determine if a point is inside a polygon