"To determine whether a point is in a polygon", at first thought is a very difficult problem, but Google after a while it is quite simple, the algorithm used is called "ray-casting algorithm", Chinese should be called "Ray projection algorithm", which is the description of Wikipedia: [Wikipedia ]
In short, you can judge: from this point to draw a "ray", and the polygon of any number of edges intersect, the cumulative number of intersecting edges, if it is odd, then the point is within the polygon, or the point is outside the polygon.
, a point leads to a ray, intersects with polygon 3 edges, odd, so a point in a polygon, and a ray from point B, intersect with 2 sides of the polygon, even, so the B point is outside the polygon.
I am going to use this algorithm to determine whether the location of the map is within a range, I first use the mouse to draw a polygon area on the map, and then use this method to determine whether a coordinate is within the range of the polygon, I still take the pentagram to do the test, on the top of the map of a pentagram:
Well? How come the pentagram didn't get pierced? What's going on? After research, I found that the map of the mouse tool polygon fill with a different set of rules, called "None Zero Mode", to determine whether a point in the polygon rules will become: from this point to draw a "ray", and the polygon of any number of edges intersect, the count is initialized to 0, If the intersection is cut from left to right by the edges of the polygon, Count +1, if the intersection is cut from right to left by the edges of the polygon, count-1, last check count, if 0, point is outside the polygon, if not 0, point is within the polygon. Return to the pentagram example, this time to pay attention to the direction of polygon lines depicted:
A ray is drawn from point C, and the edges of the two polygons intersecting this ray are cut from left to right and the total count is 2, so the C point is inside the polygon. A more image of the way to describe the point is: from the C-point, has been heading in one direction, encountered two single-lane, all from their left to the right side of the direction, Count +1, Count +1, total number so is 2.
Algorithm implementation is incredibly simple, a few lines of code can be, really a few lines of code, I use C #, we can easily change to another.
Public Static classRaycastingalgorithm { Public Static BOOLIswithin (Point pt, ilist<point> Polygon,BOOLNonezeromode) { intPtnum =Polygon. Count (); if(Ptnum <3) { return false; } intj = Ptnum-1; BOOLOddnodes =false; intZerostate =0; for(intK =0; K < Ptnum; k++) {Point PtK=Polygon[k]; Point PTJ=Polygon[j]; if((Ptk.y > Pt. Y)! = (Ptj.y > Pt. Y) && (Pt. X < (ptj.x-ptk.x) * (Pt. Y-PTK.Y)/(PTJ.Y-PTK.Y) +ptk.x)) {Oddnodes= !Oddnodes; if(Ptk.y >ptj.y) {zerostate++; } Else{zerostate--; }} J=K; } returnnonezeromode?zerostate!=0: oddnodes; } }
I wrote a demo with WPF,
To play with the students who are too lazy to beat. (VS2015)
Determine if a point is in a polygon