此問題已多次詢問,我看到很多線索,但我的查詢非常具體。如何查看兩個矩形是否重疊。在我的代碼中發現錯誤的測試用例是:檢查兩個矩形是否重疊
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