C language implementation of Pnpoly algorithm code example _c language

Source: Internet
Author: User

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].

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.