An algorithm used to write a C-language experiment to determine whether a point is inside 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)) &&
(te STX < (Vertx[j]-vertx[i]) * (Testy-verty[i])/
(Verty[j]-verty[i]) + vertx[i]) (
c =!c)
return c;
}
The Nvert is the number of polygon vertices, the vertx and Verty are the vertices of polygons, the coordinates of the ordinate, textx and testy are the coordinate of the measured points. This algorithm is made up of W. Randolph Franklin, according to the Jordan curve theorem, the polygon divides the plane into two regions inside and outside, assuming that the point to be measured is inside the polygon, a ray from the point to be measured must have at least one intersection with the polygon. The Ray and the first intersection of the polygon will "dash out" the polygon, the second intersection will "enter" the polygon, and so on, if the ray and the polygon has an odd number of intersection points, then the point within the polygon, and vice versa in the external.
The Pnpoly algorithm draws a horizontal ray from the measured point 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 always to make j = i–1, if i = 0 Then j = nvert–1, so that each edge of the polygon is checked sequentially. The next point is that the conditional statement (verty[i]>testy)!= (verty[j]>testy) is well understood, that is, the two vertices on one side are above and below the measured point, It is possible to know from this statement that the Rays drawn from the right of the point to be measured may intersect with the edge of the line (as long as the measured point is to the left of the edge).
But the concrete judgment intersect will give analytic geometry. Construct the equation that writes the line of the edge of the Strip:
Deformation:
To the coordinates of the measured point, this inequality is obtained according to the graph relation:
That is, the statement Testx < Vertx[j]-vertx[i]) * (Testy-verty[i])/(Verty[j]-verty[i]) + vertx[i].