java—我需要一个方法来检查一个矩形是否完全位于另一个矩形中

cxfofazt  于 2021-07-11  发布在  Java
关注(0)|答案(1)|浏览(514)

所以我需要一个方法来检查一个矩形是否完全位于另一个矩形中
两个矩形都是使用此测试创建的

@Test
    public void testIsRectangleInsideRectangle() {
        Rectangle rect = new Rectangle(20, 30, 20, 20);
        assertAll(
                () -> assertTrue(rect.isInside(new Rectangle(20, 30, 10, 10))),
                () -> assertFalse(rect.isInside(new Rectangle(-35, 30, 10, 20))),
                () -> assertFalse(rect.isInside(new Rectangle(120, 130, 20, 20))),
                () -> assertFalse(rect.isInside(new Rectangle(20, 130, 20, 20))),
                () -> assertFalse(rect.isInside(new Rectangle(20, -30, 20, 20)))
        );
    }

在矩形生成中使用了一个名为point的传统类,它工作得非常好,我添加它只是为了清晰起见

package net.thumbtack.school.figures.v1;

public class Point {

    private int x, y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public Point() {
        this(0, 0);
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public void moveTo(int newX, int newY) {
        x = newX;
        y = newY;
    }

    public void moveRel(int dx, int dy) {
        x += dx;
        y += dy;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + x;
        result = prime * result + y;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Point other = (Point) obj;
        if (x != other.x)
            return false;
        if (y != other.y)
            return false;
        return true;
    }
}

这是一个主类,它有一个通过定义点来创建矩形的方法。

public class Rectangle {
    public int width;
    public int height;
    public Point center;
    public int xCenter;
    public int yCenter;
    private Point point;
    private int Area;
    private int Perimeter;

    public Point getTopLeft() {
        Point point = getCenter();
        point.moveRel(-width / 2, -height / 2);
        return point;
    }

    public Point getBottomRight() {
        Point point = getCenter();
        point.moveRel(width / 2, height / 2);
        return point;
    }

    public int getWidth() {

        return width;
    }

    public int getHeight() {

        return height;
    }

    public Point getCenter() {
        Point center = new Point(xCenter, yCenter);
        return center;
    }

    public Rectangle(int xCenter, int yCenter, int width, int height) {//2
        this.xCenter = xCenter;
        this.yCenter = yCenter;
        this.width = width;
        this.height = height;
    }
}

这就是我正在研究的方法,应该检查一个矩形是否在另一个矩形内

public boolean isInside(Rectangle rectangle) {
        if (this.getTopLeft().getY() == rectangle.getBottomRight().getY()
                || this.getBottomRight().getY() == rectangle.getTopLeft().getY()) {
            return false;
        }
        if (this.getTopLeft().getX() == rectangle.getBottomRight().getX()
                || this.getBottomRight().getX() == rectangle.getTopLeft().getX()) {
            return false;
        } else
            return true;
    }

但由于某种原因,当我运行测试时,它总是返回true。

44u64gxh

44u64gxh1#

基本上需要测试一个矩形的两个对角是否位于另一个矩形内。

public boolean isInside(Rectangle rectangle) { //checks is this rectangle is within the passed rectangle
        if (
               (this.getTopLeft().getX() > rectangle.getTopLeft().getX()) &&
               (this.getTopLeft().getX() < rectangle.getBottomRight().getX()) &&
               (this.getTopLeft().getY() < rectangle.getTopLeft().getY()) &&
               (this.getTopLeft().getY() > rectangle.getBottomRight().getY()) &&

               (this.getBottomRight().getX() < rectangle.getBottomRight().getX()) &&
               (this.getBottomRight().getX() > rectangle.getTopLeft().getX()) &&
               (this.getBottomRight().getY() > rectangle.getBottomRight().getY()) &&
               (this.getBottomRight().getY() < rectangle.getTopLeft().getY()) 
        ) return true;
        else return false;
    }

还应决定是否必须严格比较(严格在边界内或边界可能重叠) < 以及 > 在第一种情况下 >= 以及 <= 在第二个

相关问题