C ++ checks whether the vertex is inside the polygon.

Source: Internet
Author: User

No more classic algorithms:

// Function: determines whether the vertex is inside the polygon.
// Method: calculates the intersection between the horizontal line and each side of the polygon.
// Conclusion: the single-side intersection is an odd number!

// Parameters:
// A point specified by point P
// Coordinates of each vertex of the lppoint ptpolygon polygon (the first and last vertex may be inconsistent)
// Int ncount the number of points on the Polygon

Bool ptinpolygon (point P, lppoint ptpolygon, int ncount)
{
Int ncross = 0;

For (INT I = 0; I <ncount; I ++)
{
Point P1 = ptpolygon [I];
Point P2 = ptpolygon [(I + 1) % ncount];

// Solve the intersection of Y = P. Y and P1P2

If (p1.y = p2.y) // P1P2 is parallel to Y = y
Continue;

If (P. Y <min (p1.y, p2.y) // The intersection is over the P1P2 extension line.
Continue;
If (P. Y> = max (p1.y, p2.y) // The intersection is over the P1P2 extension line.
Continue;

// Obtain the X coordinate of the intersection point --------------------------------------------------------------
Double x = (double) (p. y-p1.y) * (double) (p2.x-p1.x)/(double) (p2.y-p1.y) + p1.x;

If (x> p. x)
NCross ++; // only count single-side intersections
}

// The intersection of a single side is an even number, which is outside the polygon ---
Return (nCross % 2 = 1 );
}

 

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.