1

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

完整的源代码可以在这里找到:

// 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

4 回答 4

3

我认为应该是

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

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

您可以将此视为与四种情况相反的情况,每种情况各自保证不会发生重叠:

boolean isNonOverLap = (r1x1 >= r2x2) || (r1x2 <= r2x1) || (r1y1 >= r2y2) || (r1y2 <= r2y1);
于 2013-06-30T19:51:37.060 回答
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");
            }

        }

    }
}
于 2016-02-21T18:41:22.670 回答
0

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

正确的应该是:

boolean isInside = ((r1x1 >= r2x1) && (r1x2 <= r2x2) && (r1y1 >= r2y1) && (r1y2 <= r2y2));
于 2013-06-30T19:38:22.323 回答
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))

于 2014-02-14T06:18:53.990 回答