Determine if the point is inside the polygon

Source: Internet
Author: User

Determine whether the point in the polygon has three steps: (from CSDN) The first step: to determine if this point is the end of the polygon;
The second step: Judge whether this point falls on the boundary of the polygon;
The third step: through this point transverse to make a parallel ray, to judge the number of intersections with the polygon, if the intersection point is the vertex, then the intersection points plus one, the result if it is odd, then the dot falls within the polygon, if it is even, then vice versa.     The specific algorithm involves vector fork product, specifically this part not detailed said, the Internet easy to find, the following affixed to the main algorithm function bar, reference very easy to use.         Or is it from the csdn. Const double INFINITY = 1E10;
Const double ESP = 1e-5;
const int max_n = 1000;


struct Point {
Double x, y;
};

struct LineSegment {
Point Pt1, Pt2;
}; Inline double max (double x, double y)
{
return (x > y?) X:Y);
}

Inline double min (double x, double y)
{
return (x < y?) X:Y);
}

Compute Fork Multiply |   p1p0| X| p2p0|
Double Multiply (Point P1, point P2, point p0)
{
Return ((p1.x-p0.x) * (P2.Y-P0.Y)-(p2.x-p0.x) * (P1.Y-P0.Y));
}

To determine if a segment contains point points
BOOL Isonline (point point, LineSegment Line)
{
Return (Fabs (Multiply (LINE.PT1, Line.pt2, point) < ESP) &&
((point.x-line.pt1.x) * (point.x-line.pt2.x) <= 0) &&
((POINT.Y-LINE.PT1.Y) * (POINT.Y-LINE.PT2.Y) <= 0));
}

To determine the intersection of segments
BOOL Intersect (linesegment L1, LineSegment L2)
{
Return ((Max (l1.pt1.x, l1.pt2.x) >= min (l2.pt1.x, l2.pt2.x)) &&
(Max (l2.pt1.x, l2.pt2.x) >= min (l1.pt1.x, l1.pt2.x)) &&
(Max (L1.PT1.Y, l1.pt2.y) >= min (l2.pt1.y, l2.pt2.y)) &&
(Max (L2.PT1.Y, l2.pt2.y) >= min (l1.pt1.y, l1.pt2.y)) &&
(Multiply (L2.PT1, L1.PT2, L1.PT1) * Multiply (L1.pt2, L2.pt2, L1.PT1) >= 0) &&
(Multiply (L1.PT1, L2.PT2, L2.PT1) * Multiply (L2.pt2, L1.pt2, L2.PT1) >= 0)
);
}


Judge the point within the polygon
BOOL Inpolygon (point polygon[], int n, point point)
{
if (n = = 1) {
Return (Fabs (Polygon[0].x-point.x) < ESP) && (fabs (POLYGON[0].Y-POINT.Y) < ESP )   );
else if (n = = 2) {
LineSegment side;
SIDE.PT1 = polygon[0];
Side.pt2 = polygon[1];
Return Isonline (point, side);
}

int count = 0;
LineSegment Line;
LINE.PT1 = point;
Line.pt2.y = Point.y;
line.pt2.x =-INFINITY;

for (int i = 0;   I < n; i++) {
To get a side of a polygon
LineSegment side;
SIDE.PT1 = Polygon[i];
Side.pt2 = polygon[(i + 1)% n];

if (Isonline (point, side)) {
return true;
}

If the side parallel x axis is not considered
if (Fabs (SIDE.PT1.Y-SIDE.PT2.Y) < ESP) {
Continue
}

if (Isonline (SIDE.PT1, line)) {
if (Side.pt1.y > Side.pt2.y) count++;
else if (Isonline (Side.pt2, line)) {
if (Side.pt2.y > Side.pt1.y) count++;
else if (Intersect (line, side)) {
count++;
}
}

Return (count% 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.