Determine whether any point is inside a given convex-concave polygon (an efficient algorithm, with C++ code)

Given a polygon and a point "p", find whether "p" is inside the polygon. Points located on the boundary are also considered interior points.

example:

Implementation steps:

1. Draw a horizontal line to the right of each point and extend it to infinity

2. Count the number of times the line intersects the polygon edge.

3. If the number of intersections is odd or the point is on an edge of the polygon, the point is inside the polygon. If none of the conditions is true, then the point is outside.

How to deal with point "g" in the above diagram?

        ​ ​ ​ Note that it should return true if the point lies on a straight line or is the same as one of the vertices of the given polygon. To handle this, after checking if the rays from "p" to the extremum intersect, we check if "p" is collinear with the vertex of the current edge of the polygon. If it is collinear, then we check if point "p" is located on the current side of the polygon, if so, return true, otherwise return false.

Implement code

#include <bits/stdc++.h>
using namespace std;
 
struct Point {
    int x, y;
};
 
struct line {
    Point p1, p2;
};
 
bool onLine(line l1, Point p)
{
    // Check whether p is on the line or not
    if (p.x <= max(l1.p1.x, l1.p2.x)
        && p.x >= min(l1.p1.x, l1.p2.x)
        && (p.y <= max(l1.p1.y, l1.p2.y)
            && p.y >= min(l1.p1.y, l1.p2.y)))
        return true;
 
    return false;
}
 
int direction(Point a, Point b, Point c)
{
    int val = (b.y - a.y) * (c.x - b.x)
              - (b.x - a.x) * (c.y - b.y);
 
    if (val == 0)
 
        // Collinear
        return 0;
 
    else if (val < 0)
 
        // Anti-clockwise direction
        return 2;
 
    // Clockwise direction
    return 1;
}
 
bool isIntersect(line l1, line l2)
{ 
    // Four direction for two lines and points of other line
    int dir1 = direction(l1.p1, l1.p2, l2.p1);
    int dir2 = direction(l1.p1, l1.p2, l2.p2);
    int dir3 = direction(l2.p1, l2.p2, l1.p1);
    int dir4 = direction(l2.p1, l2.p2, l1.p2);
 
    // When intersecting
    if (dir1 != dir2 && dir3 != dir4)
        return true;
 
    // When p2 of line2 are on the line1
    if (dir1 == 0 && onLine(l1, l2.p1))
        return true;
 
    // When p1 of line2 are on the line1
    if (dir2 == 0 && onLine(l1, l2.p2))
        return true;
 
    // When p2 of line1 are on the line2
    if (dir3 == 0 && onLine(l2, l1.p1))
        return true;
 
    // When p1 of line1 are on the line2
    if (dir4 == 0 && onLine(l2, l1.p2))
        return true;
 
    return false;
}
 
bool checkInside(Point poly[], int n, Point p)
{
 
    // When polygon has less than 3 edge, it is not polygon
    if (n < 3)
        return false;
 
    // Create a point at infinity, y is same as point p
    line exline = { p, { 9999, p.y } };
    int count = 0;
    int i = 0;
    do {
 
        // Forming a line from two consecutive points of
        // poly
        line side = { poly[i], poly[(i + 1) % n] };
        if (isIntersect(side, exline)) {
 
            // If side is intersects exline
            if (direction(side.p1, p, side.p2) == 0)
                return onLine(side, p);
            count++;
        }
        i = (i + 1) % n;
    } while (i != 0);
 
    // When count is odd
    return count & 1;
}
 
// Driver code
int main()
{
    Point polygon[]
        = { { 0, 0 }, { 10, 0 }, { 10, 10 }, { 0, 10 } };
    Point p = { 5, 3 };
    int n = 4;
 
    // Function call
    if (checkInside(polygon, n, p))
        cout << "Point is inside.";
    else
        cout << "Point is outside.";
   
    return 0;
}

output

Point is inside.

Time complexity: O(n), where n is the number of vertices in the given polygon.

Auxiliary space: O(1), because no additional space is occupied.

If you find anything incorrect or you would like to share more information about the above topic, please write a comment.

The content of this article comes fromHow to check if a given point lies inside or outside a polygon? - GeeksforGeeks

Guess you like

Origin blog.csdn.net/subin0403/article/details/134947324