java 两个矩形是否相互重叠?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17394089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:52:26  来源:igfitidea点击:

Are two rectangles overlapping each other?

javageometry

提问by George

I studied this question, and followed the answer to implement my own version in Java. I think it is close... but still incorrect. Could you please give me some suggestion on the error?

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

The full source code can be found 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));

According to the textbook I am studying, this is supposed to be: r2 overlap r1. But my program output r2 does not overlap r1.

根据我正在学习的教科书,这应该是: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

回答by kamjagin

I think it should be

我认为应该是

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

(easier to read without all the negations)

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

You can see this as the opposite of the four cases that each on their own guarantee that an overlap cannot happen:

您可以将其视为四种情况的对立面,每种情况都保证不会发生重叠:

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

回答by abc123

Here is my solution, I tested it as well.

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

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");
            }

        }

    }
}

回答by Renato Lochetti

The first line has an error in (r1x2 >= r2x2):

第一行有一个错误(r1x2 >= r2x2)

The correct should be:

正确的应该是:

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

回答by Bharat

two rectagles will be overlapping .... when the respective diagonals are having the same length

两个矩形将重叠......当各自的对角线具有相同的长度时

for example:

例如:

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

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

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

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

so this should be true:

所以这应该是真的:

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

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

and also

并且

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

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