Pnpoly algorithm code example to determine whether a point is inside a polygon

Source: Internet
Author: User

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

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.