Determines whether the vertex is inside the polygon.

Source: Internet
Author: User

How can I determine whether a point is inside a polygon?

(1) area and discriminant method: determine the area of the triangle formed by the target point and each side of the polygon and whether it is equal to the polygon. If it is equal, it is inside the polygon.

(2) angle and discriminant method: Determine the angle between the target point and all sides and whether it is 360 degrees. If it is 360 degrees, it is inside the polygon.

(3) Shooting Method: Start from the target point and draw a Ray to see the number of intersections between the ray and all edges of the polygon. If there is an odd point of intersection, It is internal. If there is an even point of intersection, It is external.

Practice: Compare the Y coordinate of the test point with each point of the polygon to obtain a list of intersection points between the row and the polygon edge of the test point. In this example, eight edges and the row where the test point is located intersect, while six edges do not. If the number of vertices on both sides of the test point is an odd number, the test point is within the polygon; otherwise, it is outside the polygon. In this example, there are five intersections on the left of the test point and three intersections on the right. They are all odd, so they are inside the polygon.

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]) /(verty [j]-verty [I]) + vertx [I]) c =! C;} return c ;}

Internal implementation from a polygon:

      public bool IsInside(PointLatLng p)      {         int count = Points.Count;         if(count < 3)         {            return false;         }         bool result = false;         for(int i = 0, j = count - 1; i < count; i++)         {            var p1 = Points[i];            var p2 = Points[j];            if(p1.Lat < p.Lat && p2.Lat >= p.Lat || p2.Lat < p.Lat && p1.Lat >= p.Lat)            {               if(p1.Lng + (p.Lat - p1.Lat) / (p2.Lat - p1.Lat) * (p2.Lng - p1.Lng) < p.Lng)               {                  result = !result;               }            }            j = i;         }         return result;      }

In special cases: when the point to be detected is on a side of multiple deformation, the results determined by the shooting method are uncertain and need special processing (If the test point is on the border of the polygon, this algorithm will deliver unpredictable results ).

 

 

References:

Http://alienryderflex.com/polygon/

Http://en.wikipedia.org/wiki/Point_in_polygon

Http://www.codeproject.com/Tips/84226/Is-a-Point-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.