Project needs, need to judge points in the interior of the polygon, is the entire algorithm necessary steps, check some information, Chinese very few, the English material has been introduced very clearly, here is just a summary.
The complete description of the problem is to judge a point on the inside, outside, or boundary of the polygon.
There are two ways to solve this problem: Ray casting algorithm, surround number method.
Ray casting algorithm :
A simple method of judging is to send a ray from this point to any fixed direction, and to find the number of points of intersection of the Ray at the polygon edge. If the number of intersections is an even number, the point is outside the polygon, and if the number of intersections is odd, then the point is outside the polygon. This method cannot judge the situation on the polygon.
There are two issues to be aware of:
The first is that when the point to be judged is too close to the boundary, the approximate error is caused by the inaccuracy of the floating-point calculation. This problem can be solved by setting a minimum distance between a point and a dot. However, this situation is not considered when the speed requirement of the algorithm is greater than the accuracy requirement.
Another problem is that in some applications, you need to determine the intersection of the Rays in turn, using a graph to describe the problem. One of the things that must be considered in this case is that when the ray passes directly through one vertex of the polygon, it will intersect with two segments at two endpoints. There is no problem when the vertex is the topmost vertex, but if it is the rightmost vertex, you need to record an intersection point.
Workaround for this problem: if the intersection of the Rays is already at the vertex of the polygon, it can be counted only if the second intersection is located on the other side of the ray. is equivalent to judging whether the vertices are on either side or on the same sides of the ray.
A ray casting algorithm is implemented:
BOOLPixelinsidepolygon (floatXfloatYint* Polygonpoints,intcount) { //Raycasting method shooting the ray along the x axis, using float BOOLInside =false; floatxintersection; for(inti =0; I < count; i + =2) { floatP1X =Polygonpoints[i]; floatp1y = Polygonpoints[i +1]; floatP2X = polygonpoints[(i +2) %Count]; floatP2Y = polygonpoints[(i +3) %Count]; if(Y > Std::min (p1y, p2y) && y <= Std::max (p1y, P2Y) && p1y! =p2y) { if(x <=Std::max (p1x, P2X)) {xintersection= (y-p1y) * (p2x-p1x)/(P2Y-P1Y) +p1x; if(p1x = = P2X | | x <=xintersection) { //Each time intersect, toggle insideInside =!inside; } } } } returninside;}
Surround number algorithm :
Another algorithm is to calculate the number of wrapping of a given vertex relative to a polygon. If the wrapping number is nonzero, it is inside the polygon. The method for calculating the number of wraps is the sum of the corners (subtended angle) of each edge in the polygon. However, this approach introduces inverse trigonometric function, which makes the algorithm inefficient. Because all angles add up to 0 or, actually inverse trigonometric function is not calculated.
There is an improved surround number algorithm that gives a left-to-right or right-to-left calculation of the number of wraps, which can get the correct result without calculating the angle, and the speed and ray-casting algorithms are fairly tangent to the case of complex polygons.
Judging points inside the polygon