Java check if two rectangles overlap at any point

后端 未结 9 2243
醉梦人生
醉梦人生 2020-11-29 05:12

I have multiple rectangles and one special rectangle: the selection rect. I want to check for each rectangle if the rectangle contains at least one point which is inside the

9条回答
  •  执念已碎
    2020-11-29 06:11

    Two rectangles do not overlap if one of the following conditions is true.
    1) One rectangle is above top edge of other rectangle.
    2) One rectangle is on left side of left edge of other rectangle.

    Note that a rectangle can be represented by two coordinates, top left and bottom right. So mainly we are given following four coordinates.
    l1: Top Left coordinate of first rectangle.
    r1: Bottom Right coordinate of first rectangle.
    l2: Top Left coordinate of second rectangle.
    r2: Bottom Right coordinate of second rectangle.

    class Point
    {
        int x, y;
    };
    
    // Returns true if two rectangles (l1, r1) and (l2, r2) overlap
    bool doOverlap(Point l1, Point r1, Point l2, Point r2)
    {
        // If one rectangle is on left side of other
        if (l1.x > r2.x || l2.x > r1.x)
            return false;
    
        // If one rectangle is above other
        if (l1.y < r2.y || l2.y < r1.y)
            return false;
    
        return true;
    }
    

提交回复
热议问题