Check If Two Rectangles Overlap In Java – 在Java中检查两个矩形是否重叠

最后修改: 2018年 9月 11日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this quick tutorial, we’ll learn to solve an algorithmic problem of checking whether the two given rectangles overlap.

在这个快速教程中,我们将学习解决一个检查两个给定的矩形是否重叠的算法问题。

We’ll start by looking at the problem definition and then progressively build up a solution.

我们将从问题的定义开始,然后逐步建立起一个解决方案。

Finally, we’ll implement it in Java.

最后,我们将用Java实现它。

2. Problem Definition

2.问题定义

Let’s say we have two given rectangles – r1 and r2. We need to check if there’s at least one common point among r1 and r2. If yes, it simply means that these two rectangles overlap.

假设我们有两个给定的矩形–r1r2。我们需要检查r1r2之间是否至少有一个公共点。如果有,就意味着这两个矩形重叠了。

Let’s have a look at some examples:

让我们看一下一些例子。

OverlappingRectangles

If we notice the very last case, the rectangles r1 and r2 have no intersecting boundaries. Still, they’re overlapping rectangles as every point in r1 is also a point in r2.

如果我们注意到最后一种情况,矩形r1r2没有相交的边界。但是,它们仍然是重叠的矩形,因为r1中的每个点也是r2中的一个点。

3. Initial Setup

3.初始设置

To solve this problem, we should first start by defining a rectangle programmatically. A rectangle can be easily represented by its bottom-left and top-right coordinates:

为了解决这个问题,我们首先应该以编程方式定义一个矩形。一个矩形可以很容易地通过其左下角和右上角的坐标来表示:

public class Rectangle {
    private Point bottomLeft;
    private Point topRight;

    //constructor, getters and setters

    boolean isOverlapping(Rectangle other) {
        ...
    }
}

where Point is a class representing a point (x,y) in space:

其中Point是一个代表空间中一个点的类(x,y)

public class Point {
    private int x;
    private int y;

    //constructor, getters and setters
}

We’ll later define isOverlapping(Rectangle other) method in our Rectangle class to check if it overlaps with another given rectangle – other.

我们稍后将在我们的Rectangle类中定义isOverlapping(Rectangle other) 方法来检查它是否与另一个给定的矩形–other重叠。

4. Solution

4.解决办法

The two given rectangles won’t overlap if either of the below conditions is true:

如果下面的任何一个条件为真,两个给定的矩形就不会重叠。

  1. One of the two rectangles is above the top edge of the other rectangle
  2. One of the two rectangles is on the left side of the left edge of the other rectangle

NonOverlappingRectangles1

For all other cases, the two rectangles will overlap with each other. To convince ourselves, we can always draw out several examples.

对于所有其他情况,这两个矩形将相互重叠。为了说服自己,我们总是可以抽出几个例子。

5. Java Implementation

五、Java实现

Now that we understand the solution, let’s implement our isOverlapping() method:

现在我们理解了这个解决方案,让我们来实现我们的isOverlapping()方法。

public boolean isOverlapping(Rectangle other) {
    if (this.topRight.getY() < other.bottomLeft.getY() 
      || this.bottomLeft.getY() > other.topRight.getY()) {
        return false;
    }
    if (this.topRight.getX() < other.bottomLeft.getX() 
      || this.bottomLeft.getX() > other.topRight.getX()) {
        return false;
    }
    return true;
}

Our isOverlapping() method in Rectangle class returns false if one of the rectangles is either above or to the left side of the other, true otherwise.

我们在Rectangle类中的isOverlapping()方法,如果其中一个矩形在另一个的上方或左侧,则返回false,否则返回true

To find out if one rectangle is above the other, we compare their y-coordinates. Likewise, we compare the x-coordinates to check if one rectangle is to the left of the other.

为了找出一个矩形是否在另一个矩形之上,我们比较它们的y坐标。同样地,我们比较x坐标来检查一个矩形是否在另一个矩形的左边。

6. Conclusion

6.结论

In this short article, we learned how to solve an algorithmic problem of finding whether the two given rectangles overlap with each other. It serves as a collision detection strategy for two rectangular objects.

在这篇短文中,我们学习了如何解决一个算法问题,即寻找两个给定的矩形是否相互重叠的问题。它可以作为两个矩形物体的碰撞检测策略。

As usual, the entire source code is available over on Github.

像往常一样,整个源代码可在Github上获得