Java check if two rectangles overlap at any point

后端 未结 9 2181
醉梦人生
醉梦人生 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 05:45

    This will find if the rectangle is overlapping another rectangle:

    public boolean overlaps (Rectangle r) {
        return x < r.x + r.width && x + width > r.x && y < r.y + r.height && y + height > r.y;
    }
    
    0 讨论(0)
  • 2020-11-29 05:47

    If the first one implements RectangularShape and the second one is a Rectangle2D, you can simply use RectangularShape.intersects:

    selectionRectangle.intersects(otherRectangle)
    

    Tests if the interior of the Shape intersects the interior of a specified Rectangle2D

    From the Oracle Java docs

    0 讨论(0)
  • 2020-11-29 05:48

    I have a generic implemententation for polygons in the gps coordinate system, which may be a bit overkill for rectangles (which are simple polygons); but it will work. It should be fairly straightforward to adapt the approach to your usecase if for whatever reason you don't want to use AWT.

    https://github.com/jillesvangurp/geogeometry/blob/master/src/main/java/com/jillesvangurp/geo/GeoGeometry.java#L753 (overlap method)

    What I do there is simply check if the polygons have any points that are contained by the other polygon.

    For polygon containment of points, I have a simple algorithm that walks the edges of the polygon to check if the point is inside or outside O(n). For rectangles it should be cheap to run.

    The nice thing about this approach it will work for any rectangles and also rotated rectangles or more complex shapes.

    0 讨论(0)
  • 2020-11-29 05:57

    Edit As mentioned in the accepted answer, the AWT Rectangle object provides this functionality with the intersects method. If you dont want to use AWT or for some other reason, below is a variant solution.

    If you want to reinvent the wheel, then here is some stuff. Using your example image, this will test of the black rectangle overlaps with the blue rectangle. Also, this assumes that touching is not overlapping.

    Each rectangle will be represented by two coordinate pairs: topLeft and bottomRight.

    This assumes that 0, 0 is in the top left corner.

    Function xOverlapCheck(black, blue)
    {
        // black left side overlaps.
        if ((black.topLeft.x <= blue.bottomRight.x) &&
            (black.topLeft.x >= blue.topLeft.x))
        {
            return true;
        }
    
        // black right side overlaps.
        if ((black.bottomRight.x <= blue.bottomRight.x) &&
            (black.bottomRight.x >= blue.topLeft.x))
        {
            return true;
        }
    
        // black fully contains blue.
        if ((black.bottomRight.x >= blue.bottomRight.x) &&
            (black.topLeft.x <= blue.topLeft.x))
        {
            return true;
        }
    }
    
    
    Function yOverlapCheck(black, blue)
    {
        // black top side overlaps.
        if ((black.topLeft.y >= blue.topLeft.y) &&
            (black.topLeft.y <= blue.bottomRight.y))
        {
            return true;
        }
    
        // black bottom side overlaps.
        if ((black.bottomRight.y >= blue.topLeft.y) &&
            (black.bottomRight.y <= blue.bottomRight.y))
        {
            return true;
        }
    
        // black fully contains blue.
        if ((black.bottomRight.y >= blue.bottomRight.y) &&
            (black.topLeft.y <= blue.topLeft.y))
        {
            return true;
        }
    }
    

    Black overlaps Blue when both functions return true.

    Edit: use <= and >= for overlap comparisons.

    0 讨论(0)
  • 2020-11-29 06:05

    I would make Rectangle objects and then use the Rectangle.intersects and Rectangle.contains methods to determine if they intersect or if one contains the other.

    Since you have one big rectangle, that is the selection rectangle, this is even easier than I thought. Run Rectangle.contains, and for all rectangles that aren't contained, run Rectangle.intersects, and you have what you are seeking.

    0 讨论(0)
  • 2020-11-29 06:07

    This class assumes the ordering left<=right, top<=bottom, x1<=x2, y1<=y2:

    public class Rect
    {
    int left, right, bottom, top;
    
    Rect(int left, int top, int right, int bottom)
    {
        this.left = left;
        this.right = right;
        this.top = top;
        this.bottom = bottom;
    }
    
    boolean overlap(int x1, int y1, int x2, int y2)
    {
        // if one rectangle is to the left or right, then there can be no overlap
        if(x2 < left || right < x1)
            return false;
    
        // the x values overlap, but the y values may still lie outside the rectangle
    
        // if one rectangle is above or below, then there can be no overlap
        if(y2 < top || bottom < y1)
            return false;
    
        // otherwise we must overlap !
        return true;        
    }
    }
    
    0 讨论(0)
提交回复
热议问题