Relationship between points and polygon (improved shooting method)

Source: Internet
Author: User

InGISSome geometric features are often used in software development.Algorithm, Such as the construction of a triangular network, the division of polygon, the relationship between points, lines, and surfaces. The relationship between points and polygon is a very important basic task.

In the determination of the relationship between points and polygon, the commonly used method is the ray method and the angle and method. The shooting method can be used to judge the polygon with Island, and the angle and method are powerless.

The basic idea of the shoot method is to introduce rays to a certain direction from the point to be judged, and calculate the number of intersections with the polygon. If the number is an even number or0Point out of the polygon. If it is an odd number, it is inside the polygon. ThisOnlyIs the most basic identification, there are some complex situations that need special processing:

(Ray goes through the vertex): When the ray goes through the vertex, an exception will occur when the judgment is made. It is now stipulated that the two endpoints of the line segment are called the top endpoints as opposed to the other endpoints, below is the lower endpoint. If it passes through the lower endpoint, it is considered that the side is not intersecting with the ray.

(Point on the edge): In this case, the parity of the number of intersections cannot be used to determine whether the point is on the edge.

Improvement of the shoot method: the traditional shoot method directly calculates the number of intersections between points and polygon at the beginning. In this way, it will take a lot of time to judge the topological relationship. The improved algorithm is to use the smallest external rectangle of the polygon to quickly discharge non-MBRAnd then use the parity of the number of intersections to judge:

The following functions determine the relationship between rays and edges and the number of intersections:

 
// Relationship between rays and line segments: 1 is returned for intersection, 0 is returned for non-intersection, and-1int isintersectant (Double X, Double Y, double X1, double Y1, double X2, double Y2) {// calculate the minimum and maximum coordinate values of a line segment, double Minx, Maxx, miny, Maxy; Minx = x1; Maxx = x2; If (Minx> Maxx) {Minx = x2; Maxx = x1;} miny = Y1; Maxy = Y2; If (miny> Maxy) {miny = Y2; Maxy = Y1 ;} // quick if (Y <miny | Y> Maxy | x <Minx) {return 0 ;}// if it is a horizontal line segment, -1 is returned on the online segment; otherwise, 0if (FABS (Maxy-miny) <EPS) {return (x> = Minx & & Amp; x <= Maxx )? (-1): 0;} // returns the x-axis of the intersection of the X-ray and the edge. Double X0 = X1 + (double) (Y-Y1) * (x2-X1) /(Y2-Y1); // If (x0> X) {return 0;} does not intersection when the intersection is on the right of the ray ;} // The intersection and the ray start point are the same if (FABS (x-x0) <EPS) {return-1;} // pass through the lower endpoint also does not count if (FABS (Y-miny) <EPS) {return 0;} return 1 ;}

The above is the calculation of rays and an edge. For a polygon, you only need to judge the rays and their edges one by one.

Int mypolygon: pointinpolygon (const mypoint & popoint) {// If the vertex is not in the smallest external rectangle of the polygon, it must not be in the polygon myenvelope env; getenvelope (& env ); if (! Env. ispointinrect (popoint. getx (), popoint. gety () {return 0;} // calculates the number of intersections between the rays to the left and each edge. Int ncount = 0; Double X = popoint. getx (); Double Y = popoint. gety (); int nflag = 0; mylinestring * exteriorring = getexteriorring (); exteriorring-> closerings (); For (INT I = 0; I <exteriorring-> getnumpoint () -1; I ++) {nflag = isintersectant (X, Y, exteriorring-> getx (I), exteriorring-> Gety (I ), exteriorring-> getx (I + 1), exteriorring-> Gety (I + 1); If (ncount <0) {return 2; // point on the edge} ncount + = nflag;} If (ncount % 2 = 1) // point in the polygon {return 1;} elsereturn 0 ;}

ProgramThe tests are correct!

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.