Java 我需要一种方法来检查一个矩形是否完全位于另一个矩形内
所以我需要一个方法来检查一个矩形是否完全位于另一个矩形内Java 我需要一种方法来检查一个矩形是否完全位于另一个矩形内,java,rectangles,Java,Rectangles,所以我需要一个方法来检查一个矩形是否完全位于另一个矩形内 public boolean isInside(Rectangle rectangle) { if (this.getTopLeft().getY() == rectangle.getBottomRight().getY() || this.getBottomRight().getY() == rectangle.getTopLeft().getY()) { retu
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;
}
这两个矩形都是使用此测试创建的
@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;
}
但由于某种原因,当我运行test时,它总是返回true。基本上需要测试一个矩形的两个相对角是否位于另一个矩形内
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;
}
public boolean isInside(矩形){//检查此矩形是否在传递的矩形内
如果(
(此.gettoplft().getX()>rectangle.gettoplft().getX())&&
(此.gettoplft().getX()rectangle.getBottomRight().getY())&&
(此.getBottomRight().getX()rectangle.getTopLeft().getX())&&
(此.getBottomRight().getY()>矩形.getBottomRight().getY())&&
(this.getBottomRight().getY()
还应确定比较是否必须严格(严格在范围内或边界可能重叠)。在第一种情况下使用
,并且=
和基本上需要测试一个矩形的两个相对角是否位于另一个矩形内
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;
}
public boolean isInside(矩形){//检查此矩形是否在传递的矩形内
如果(
(此.gettoplft().getX()>rectangle.gettoplft().getX())&&
(此.gettoplft().getX()rectangle.getBottomRight().getY())&&
(此.getBottomRight().getX()rectangle.getTopLeft().getX())&&
(此.getBottomRight().getY()>矩形.getBottomRight().getY())&&
(this.getBottomRight().getY()
我们还应该决定比较是否必须严格(严格在范围内或边界可能重叠)。在第一种情况下使用
,并且=
和是否确认getTopLeft和getBottomRight按预期工作?如果说this.getTopLeft()
也将是false.getY()
大于矩形。getBottomRIght().getY()
(假设Y坐标在“向下”方向上增加)?您肯定想比较坐标,而不是测试它们是否相等?i、 e.if(topLeft.x
请用英语解释您的if语句在做什么。或者绘制一张if语句为真时的图表。这应该可以帮助您发现问题。方法getTopLeft
和getBottomRight
是错误的,isInside
中的条件对任务毫无意义。是否确认getTopLeft和getBottomRight按预期工作?如果,假设this.gettoplft().getY()
大于rectangle.getBottomRIght().getY()
(假设Y坐标在“向下”方向上增加)?您肯定想比较坐标,而不是测试它们是否相等?i、 e.if(topLeft.x
请用英语解释您的if语句在做什么。或者绘制一张if语句为真时的图表。这应该可以帮助您发现问题。方法getTopLeft
和getBottomRight
是错误的,isInside
中的条件对任务毫无意义。仍然只返回true仍然只返回true