Write a C language experiment to use an algorithm to determine whether a point is within the polygon. The code for C is as follows:
int pnpoly (int nvert, float *vertx, float *verty, float testx, float testy) { int I, j, C = 0; for (i = 0, j = nvert-1; i < nvert; j = I++ if ((verty[i]>testy)! = (verty[j]>testy)) && (Testx < (Vertx[j]-vertx[i]) * (Testy-verty[i])/ (ver TY[J] -verty[i]) + vertx[i])) c =! C; return C;}
Where the Nvert is the number of polygon vertices, vertx and verty are the polygon vertex horizontal, ordinate array, textx and testy are the coordinates of the point to be measured. This algorithm is composed of W. Randolph Franklin proposed that, according to the Jordan curve theorem, the polygon divides the plane into the inner and outer two regions, assuming that the point to be measured within the polygon, a ray from the point to be measured will inevitably have at least one intersection with the polygon. The ray will "punch out" the polygon the first time it intersects the polygon, the second intersection will "enter" the polygon, and so on, if the ray and polygon have an odd number of intersections, then the point is inside the polygon, and vice versa on the outside.
The Pnpoly algorithm draws a horizontal right ray from the point to be measured, and calculates the number of intersections with polygons. Explain this code: for (i = 0, j = nvert-1; i < nvert; j = i++) The meaning of the loop is to always let J = i–1, if i = 0 Then j = nvert–1, which sequentially examines each edge of the polygon. The next point is the conditional statement, (verty[i]>testy)! = (Verty[j]>testy) is well understood, that is, the two vertices on one edge are above and below the point to be measured, This statement allows you to know that the ray from the point to the right is likely to intersect with that edge (as long as the point is on the left side of the edge).
But the concrete judgment intersection will give analytic geometry. To write the equation for the line in which the edge is located:
To deform a bit:
Substituting the coordinates of the point to be measured, the inequality is obtained according to the graph relation:
i.e. Statement Testx < Vertx[j]-vertx[i]) * (Testy-verty[i])/(Verty[j]-verty[i]) + vertx[i].
Pnpoly algorithm code example to determine whether a point is inside a polygon