Intersection of polygons

后端 未结 4 1821
无人共我
无人共我 2020-12-06 07:43

There are two polygons given. how can one determine whether one polygon is inside, outside or intersecting the other polygon? Polygons can be Concave or convex.

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-06 07:52

    Here is a simple algorithm to know if a given point is inside or outside a given polygon:

    bool isInside(point a, polygon B)
    {
        double angle = 0;
        for(int i = 0; i < B.nbVertices(); i++)
        {
            angle += angle(B[i],a,B[i+1]);
        }
        return (abs(angle) > pi);
    }
    
    • If a line segment of A intersects a line segment of B then the two polygons intersect each other.
    • Else if all points of polygon A are inside polygon B, then A is inside B.
    • Else if all points of polygon B are inside polygon A, then B is inside A.
    • Else if all points of polygon A are outside polygon B, then A is outside B.
    • Else the two polygons intersect each other.

提交回复
热议问题