How to check intersection between 2 rotated rectangles?

前端 未结 11 1786
囚心锁ツ
囚心锁ツ 2020-11-27 04:18

Can someone explain how to check if one rotated rectangle intersect other rectangle?

11条回答
  •  感动是毒
    2020-11-27 04:30

    Here's the same algorithm in Java if anybody is interested.

    boolean isPolygonsIntersecting(Polygon a, Polygon b)
    {
        for (int x=0; x<2; x++)
        {
            Polygon polygon = (x==0) ? a : b;
    
            for (int i1=0; i1 maxA)
                        maxA = projected;
                }
    
                double minB = Double.POSITIVE_INFINITY;
                double maxB = Double.NEGATIVE_INFINITY;
    
                for (Point p : b.getPoints())
                {
                    double projected = normal.x * p.x + normal.y * p.y;
    
                    if (projected < minB)
                        minB = projected;
                    if (projected > maxB)
                        maxB = projected;
                }
    
                if (maxA < minB || maxB < minA)
                    return false;
            }
        }
    
        return true;
    }
    

提交回复
热议问题