Determine if the point is in the polygon

Source: Internet
Author: User
One, determine if the point is in the polygon (theory)

To determine whether point P is a very basic but very important algorithm in the computation geometry in the polygon. With point P as the endpoint, to the left as the Ray L, because the polygon is bounded, so the left side of the Ray L must be outside the polygon, considering moving from left to right along L from Infinity, to the first intersection of the polygon, to the interior of the polygon, to the second intersection, to the polygon, ... So it's easy to see that when the number of intersections of L and polygons is odd, p is in the polygon and the even word p is outside the polygon.

But there are special circumstances to consider. As shown in figure (a) (b) (c) (d) below. In figure (a), l and the vertices of a polygon intersect, at which point the intersection can only compute one; in diagram (b), the intersection of L and polygon vertices should not be computed; in graphs (c) and (d), the edges of L and polygons overlap, and this edge should be ignored. If a side of the L and a polygon is coincident, this edge should be ignored.

For the sake of unification, when we compute the intersection of the X ray L and the polygon, 1. The horizontal edges of the polygon are not considered; 2. For the vertex and L intersection of polygons, if the vertex is a larger vertex on the side of which it belongs, the count is ignored; 3. For the case where P is on the edge of a polygon, it can be directly judged that p belongs to the multilateral line. The pseudo code for this algorithm is as follows:

count←0;
Taking p as the endpoint, as the Ray L of the right and left;
For each edge of a polygon s
Do if p on the side s
then return true;
If S is not a horizontal
An endpoint of then if S is on l
If the endpoint is a larger-ordinate endpoint in s at both ends
Then count←count+1
else if S and l intersect
Then count←count+1;
If Count mod 2 = 1
then return true;
else return false;


The method of doing Ray L is to set the ordinate of P ' and P ', and the horizontal axis is positive infinity (a large positive number), then p and P ' determine the X-ray L.

The time complexity of the algorithm for determining whether the point is in a polygon is O (n).

Another algorithm is to use the symbolic area of the triangle and polygon area compared to the algorithm, because of the use of floating point arithmetic so will bring a certain error, do not recommend everyone to use.

Second, Determine if the point is in a polygon (action)

In order to speed up the discriminant speed, first compute the bounding rectangle of the polygon, judge whether the point falls in the outer package rectangle, only the point that satisfies the condition in the outer package rectangle, then enters the next "Ray method" calculation.

The following is the code implemented by C #

<summary>
Function: Determine if the point is within the polygon
Method: To solve the intersection point between the horizontal line and the polygon edges
Conclusion: Unilateral intersection is odd, set up!
Parameter: Returns 1 is definitely inside the polygon;-1 is not in the polygon; 0 is on the edge of the polygon;
Point P specified
Point[] The coordinates of each vertex of the Ptpolygon polygon (the first end point can be inconsistent)
int ncount Polygon Point number
</summary>
<param name= "P" ></param>
<param name= "Ptpolygon" ></param>
<returns></returns>
public static int Ptinpolygon (point P, point[] ptpolygon)
{
int ncount = Ptpolygon.length;
BOOL Isbeside = false;//Whether the record is on the edge of the polygon

#region Rectangular outer Area
Double Maxx;
Double Maxy;
Double minx;
Double miny;
if (ncount > 0)
{
Maxx = Ptpolygon[0]. X
Minx = ptpolygon[0]. X
Maxy = ptpolygon[0]. Y
Miny = ptpolygon[0]. Y

                for (int j = 1; j < ncount; J + +)
                {
                     if (Ptpolygon[j]. X >= Maxx)
                         maxx = Ptpolygon[j]. X
                     else if (ptpolygon[j). X <= Minx)
                         minx = ptpolygon[j]. X

                     if (ptpolygon[j). Y >= Maxy)
                         Maxy = Ptpolygon[j]. Y
                     else if (ptpolygon[j). Y <= miny)
                         miny = Ptpolygon[j]. Y
               }

if ((P.x > Maxx) | | (p.x < minx) | | (P.y > Maxy) | | (P.y < Miny))
return-1;
}


#endregion

#region-Ray Method
int ncross = 0;

for (int i = 0; i < ncount; i++)
{
Point p1 = Ptpolygon[i];
Point P2 = ptpolygon[(i + 1)% ncount];

To solve the intersection point between Y=p.y and P1P2

if (P1. Y = = P2. Y)//p1p2 parallel with Y=P0.Y
{
if (p.y = = P1. Y && p.x >= min (p1. X, p2. X) && p.x <= Max (p1. X, p2. X))
{
Isbeside = true;
Continue
}
}

if (P.y < min (p1). Y, p2. Y) | | P.y > Max (p1. Y, p2. Y))//crossing point on p1p2 extension line
Continue


To find the X coordinate of the intersection--------------------------------------------------------------
Double x = (double) (P.Y-P1. Y) * (double) (P2. X-p1. X)/(double) (P2. Y-p1. Y) + p1. X

                if (x > P.x)
                     ncross++; Counts only the one-sided intersection
                else if ( x = = p.x)
                     isbeside = true;
           }

if (isbeside)
Return 0;//Polygon Edge
else if (ncross% 2 = 1)//The unilateral intersection is even, the point is outside the polygon---
Return 1;//polygon Inside

return-1;//Polygon Outside
#endregion
}

private static int min (int x, int y)
{
if (x > Y)
return y;
Else
return x;
}
private static int Max (int x, int y)
{
if (x > Y)
return x;
Else
return y;
}

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.