【问题标题】:I need method that will check if one rectangle lies wholy inside another我需要一种方法来检查一个矩形是否完全位于另一个矩形内
【发布时间】:2020-11-21 11:01:18
【问题描述】:

所以我需要一种方法来检查一个矩形是否完全位于另一个矩形内

两个矩形都是使用这个测试创建的

@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。

【问题讨论】:

  • 是否确认 getTopLeft 和 getBottomRight 按预期工作?
  • 如果 this.getTopLeft().getY() 大于 rectangle.getBottomRIght().getY()isInside() 也不会是 false(假设 Y 坐标在“向下”方向)?
  • 您确定要比较坐标而不是测试它们是否相等?即if(topLeft.x < rectangle.topLeft.x) return false; else ...
  • 请用英语解释你的 if 语句是做什么的。或者画出 if 语句何时为真的图表。这应该可以帮助您发现问题。
  • 方法 getTopLeftgetBottomRight 是错误的,isInside 中的条件对任务根本没有意义..

标签: java rectangles


【解决方案1】:

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

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;
    }

还应确定比较是否必须严格(严格在内部或边界可能重叠。)在第一种情况下使用 &lt;&gt;,在第二种情况下使用 &gt;=&lt;=

【讨论】:

  • 仍然只返回真
猜你喜欢
  • 2015-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 2011-11-24
  • 2020-07-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多