此问题已多次询问,我看到很多线索,但我的查询非常具体。如何查看两个矩形是否重叠。在我的代码中发现错误的测试用例是:检查两个矩形是否重叠
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是在条件:如果(topLeftA.y < bottomRightB.y || topLeftB.y < bottomRightA.y)
请帮忙。我已经在这里花了很多时间。
你是什么意思的更大“的错误是在条件...”?你期望的结果是什么,你得到了什么?请参见[如何创建最小,完整和可验证示例](http://stackoverflow.com/help/mcve)。 –
根据条件:两个矩形不会重叠,但如果我用纸笔绘制两个矩形,则它重叠 – ojas