【发布时间】:2017-05-13 06:14:14
【问题描述】:
这个问题已经问过很多次了,我看过很多帖子,但我的查询非常具体。如何查看两个矩形是否重叠。在我的代码中发现错误的测试用例是:
l2 = new RectanglePoint(0, 7);
r2 = new RectanglePoint(6, 10);
l1 = new RectanglePoint(0, 7);
r1 = new RectanglePoint(6, 0);
函数调用:isOverlap(new Rectangle(l1, r1), new Rectangle(l2, r2));
我的代码:
class RectanglePoint {
int x;
int y;
public RectanglePoint(int x, int y) {
this.x = x;
this.y = y;
}
}
class Rectangle {
RectanglePoint topLeft;
RectanglePoint bottomRight;
public Rectangle(RectanglePoint topLeft, RectanglePoint bottomRight) {
this.topLeft = topLeft;
this.bottomRight = bottomRight;
}
}
public class RectangleOverlap {
public boolean isOverlap(Rectangle rect1, Rectangle rect2) {
return isOverlapHelper1(rect1.topLeft, rect1.bottomRight, rect2.topLeft,
rect2.bottomRight);
}
private boolean isOverlapHelper1(RectanglePoint topLeftA,
RectanglePoint bottomRightA, RectanglePoint topLeftB,
RectanglePoint bottomRightB) {
if (topLeftA.y < bottomRightB.y || topLeftB.y < bottomRightA.y) {
return false;
}
if (topLeftA.x > bottomRightB.x || topLeftB.x > bottomRightA.x) {
return false;
}
return true;
}
bug 的条件是:if (topLeftA.y
请帮忙。我已经在这方面花费了很多时间。
【问题讨论】:
-
您的意思是“错误处于...”是什么意思?你期待什么结果,你得到了什么?见How to create a Minimal, Complete, and Verifiable example。
-
根据条件:两个矩形永远不会重叠,但是如果我用铅笔画两个矩形,那么它就会重叠
标签: java math data-structures rectangles