两个矩形是否相互重叠?

时间:2013-06-30 19:31:20

标签: java geometry

我研究了这个question,并按照答案在Java中实现我自己的版本。我认为它很接近......但仍然不正确。你能否就错误给我一些建议?

可以找到完整的源代码here

// Determine if it is inside
boolean isInside = ((r1x1 >= r2x1) && (r1x2 >= r2x2) 
        && (r1y1 >= r2y1) && (r1y2 <= r2y2));

// Determine if it is overlap
boolean isOverLap = (!(r1x1 >= r2x2) && !(r1x2 <= r2x2)
        && !(r1y2 >= r2y1) && !(r1y1 <= r2y2));

// Determine if it is NOT overlap
boolean isNotOverLap = ((r1x1 >= r2x2) || (r1x2 <= r2x2)
        || (r1y2 >= r2y1) || (r1y1 <= r2y2));

根据我正在研究的教科书,这应该是:r2 overlap r1。但我的程序输出r2 does not overlap r1

Enter the r1's center x, y coordinates, width and height
1 2 3 5.5
Enter the r2's center x, y coordinates, width and height
3 4 4.5 5
Rectangle 1: (-0.50, 4.75), (2.50, -0.75)
Rectangle 2: (0.75, 6.50), (5.25, 1.50)
r2 does not overlap r1

4 个答案:

答案 0 :(得分:3)

我认为应该是

boolean isOverLap = (r1x1 < r2x2) && (r1x2 > r2x1) &&  (r1y1 < r2y2) && (r1y2 > r2y1);

(没有所有的否定,更容易阅读)

你可以看到这与四种情况相反,每种情况都可以保证不会发生重叠:

boolean isNonOverLap = (r1x1 >= r2x2) || (r1x2 <= r2x1) || (r1y1 >= r2y2) || (r1y2 <= r2y1);

答案 1 :(得分:2)

这是我的解决方案,我也测试了它。

package small_Progs;

class Point {
    int x, y;

    Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

class Rectangle {
    Point lt, lb, rt, rb;

    Rectangle(Point lt, Point lb, Point rt, Point rb) {
        this.lt = lt;
        this.lb = lb;
        this.rt = rt;
        this.rb = rb;
    }
}

public class OverlappingRectagles {

    public static void main(String arg[]) {
        Point lt1 = new Point(3, 8);
        Point lb1 = new Point(3, 5);
        Point rt1 = new Point(6, 8);
        Point rb1 = new Point(6, 5);

        Point lt2 = new Point(5, 6);
        Point lb2 = new Point(5, 3);
        Point rt2 = new Point(9, 6);
        Point rb2 = new Point(9, 3);

        Point lt3 = new Point(3, 7);
        Point lb3 = new Point(3, 6);
        Point rt3 = new Point(5, 7);
        Point rb3 = new Point(5, 6);

        Point lt4 = new Point(1, 2);
        Point lb4 = new Point(1, 1);
        Point rt4 = new Point(2, 2);
        Point rb4 = new Point(2, 1);

        Rectangle r1 = new Rectangle(lt1, lb1, rt1, rb1);
        Rectangle r2 = new Rectangle(lt2, lb2, rt2, rb2);
        Rectangle r3 = new Rectangle(lt3, lb3, rt3, rb3);
        Rectangle r4 = new Rectangle(lt4, lb4, rt4, rb4);

        OverlappingRectagles obj = new OverlappingRectagles();
        obj.isOverLapping(r1, r2);
        obj.isOverLapping(r1, r3);
        obj.isOverLapping(r1, r4);

    }

    private void isOverLapping(Rectangle rect1, Rectangle rect2) {
        Point l1 = rect1.lt;
        Point l2 = rect2.lt;

        Point r1 = rect1.rb;
        Point r2 = rect2.rb;

        if (l1.y < l2.y || l2.y < r1.y) {
            System.out.println("Not Overlapping");
        } else if (l1.x > r2.x || l2.x > r1.x) {
            System.out.println("Not Overlapping");
        } else {
            if ((l1.y > r2.y && l2.y > r1.y) || (l2.y > r1.y && r2.y > r2.y)) {
                System.out.println("Overlapping");
            } else {
                System.out.println("Not Overlapping");
            }

        }

    }
}

答案 2 :(得分:0)

第一行在(r1x2 >= r2x2)

中有错误

正确应该是:

boolean isInside = ((r1x1 >= r2x1) && (r1x2 <= r2x2) && (r1y1 >= r2y1) && (r1y2 <= r2y2));

答案 3 :(得分:0)

当相应的对角线具有相同的长度时,两个矩形将重叠

例如:

R1:(x1,y1),(x2,y2),(x3,y3),(x4,y4)

R2:(a1,b1),(a2,b2),(a3,b3),(a4,b4)

所以这应该是真的:

距离((x1,y1),(x3,y3))=距离((a1,b1),(a3,b3))

以及

距离((x2,y2),(x4,y4))=距离((a2,b2),(a4,b4))

相关问题