Java 2个矩形,重叠、相交、并集。JUnit失败:testNotOverlaps(RectangleGraderTest)
有点沮丧试图找出这个问题,试图修复这个错误,并且在我自己尝试了很多之后来找你们。我需要创建一个Java程序,允许在两个矩形上运行一些函数(类似于映射)。我使用JUnit4(另一个要求)测试的两个矩形对于重叠来说是“true”,但它们不应该重叠。信息是: “失败:testNotOverlaps(矩形渐变测试) 消息:x:0,y:0,w:20,h:10不应与x:-50,y:-50,w:5,h:5重叠,但它确实重叠。” 屏幕截图、我的代码和标题都是不言自明的。我一直有这个错误,我需要它消失。我很乐意回答任何进一步的问题:) Rectangle.java:Java 2个矩形,重叠、相交、并集。JUnit失败:testNotOverlaps(RectangleGraderTest),java,junit,junit4,Java,Junit,Junit4,有点沮丧试图找出这个问题,试图修复这个错误,并且在我自己尝试了很多之后来找你们。我需要创建一个Java程序,允许在两个矩形上运行一些函数(类似于映射)。我使用JUnit4(另一个要求)测试的两个矩形对于重叠来说是“true”,但它们不应该重叠。信息是: “失败:testNotOverlaps(矩形渐变测试) 消息:x:0,y:0,w:20,h:10不应与x:-50,y:-50,w:5,h:5重叠,但它确实重叠。” 屏幕截图、我的代码和标题都是不言自明的。我一直有这个错误,我需要它消失。我很乐意回
import java.util.NoSuchElementException;
/**
*此类表示一个矩形。A具有x和y坐标以及宽度和高度。
*/
公共类矩形{
私人INTX;
私营企业;
私有整数宽度;
私人内部高度;
/**
*构造一个矩形对象,并使用给定的x、y、高度和宽度对其进行初始化。
*
*@param x此矩形的x坐标
*@param y此矩形的y坐标
*@param height此矩形的高度
*@param width此矩形的宽度
*如果宽度或/和高度为非正,@将引发IllegalStateException
*/
公共矩形(整数x,整数y,整数宽度,整数高度)抛出IllegalArgumentException{
这个.x=x;
这个。y=y;
如果(高度0){
高度=高度;
}
}
/**
*如果此矩形与其他矩形重叠,则返回true,否则返回false。
*
*@param other另一个矩形
*@如果此矩形与其他矩形重叠,则返回true,否则返回false。
*/
公共布尔重叠(矩形其他){
如果(this.y>other.y+other.height&&other.xother.y&&this.xother.x&&this.y>other.y&&this.x+this.widthxTopRightInter | | yBottomLeftInter>yTopRightInter){
抛出新的NoTouchElementException(“矩形不重叠”);
}
矩形中断=新矩形(xBottomLeftInter、yBottomLeftInter、,
xTopRightInter-xBottomLeftInter、yTopRightInter-yBottomLeftInter);
返回中断;
}
/**
*返回一个矩形对象,该对象表示此矩形和另一个矩形的并集。
*
*@param other另一个矩形
*@return Rectangle对象,表示两个矩形的重叠
*/
公共矩形联合(矩形其他){
int xBottomLeft=Math.min(this.x,other.x);
int yBottomLeft=Math.min(this.y,other.y);
int xTopLeft=xBottomLeft;
int-yTopLeft=Math.max(this.y+this.height,other.y+other.height);
int xTopRight=Math.max(this.x+this.width,other.x+other.width);
int yTopRight=yTopLeft;
int xBottomRight=xTopRight;
int yBottomRight=yBottomLeft;
矩形unionRect=新矩形(xBottomLeft、yBottomLeft、xBottomRight-xBottomLeft、,
yTopLeft-yBottomLeft);
返回unionRect;
}
/**
*获取此矩形的toString方法。
*
*@return此矩形的toString方法
*/
公共字符串toString(){
返回(“x:+x+”,y:+y+”,w:+width+,h:+height);
}
}
检查重叠时,此情况this.y>other.y+other.height&&other.x
检查“其他最大y值是否低于此最小y值”,这意味着没有重叠,因此两个矩形重叠的部分不可能为真
以下是一组简单的条件,用于检查一个矩形的垂直边和水平边是否与另一个矩形的边交叉:
public boolean overlap(Rectangle other) {
//for overlap both vertical and horizontal edges need to cross
return
//check if horizontal edge cross
other.width + other.x > x && //other max x is bigger than min x and
other.x < width + x && //other min x is smaller than max x
//check if vertical edge cross
other.height+ other.y > y &&
other.y <height + y ;
}
使用
2.见评论
if (height <= 0) {
throw new IllegalArgumentException("Height can't be non-positive.");
}
if (width <= 0) {
throw new IllegalArgumentException("Width can't be non-positive.");
}
if (width > 0) { //width is always > 0 here so `if` is not needed
this.width = width;
}
if (height > 0) { //height is always > 0 here so `if` is not needed
this.height = height;
}
if(此处高度为0),因此不需要'if'
这个。宽度=宽度;
}
if(height>0){//此处的height始终大于0,因此不需要'if'
高度=高度;
}
检查重叠时,此情况
this.y>other.y+other.height&&other.x
检查“其他最大y值是否低于此最小y值”,这意味着没有重叠,因此两个矩形重叠的部分不可能为真。
以下是一组简单的条件,用于检查一个矩形的垂直边和水平边是否与另一个矩形的边交叉:
public boolean overlap(Rectangle other) {
//for overlap both vertical and horizontal edges need to cross
return
//check if horizontal edge cross
other.width + other.x > x && //other max x is bigger than min x and
other.x < width + x && //other min x is smaller than max x
//check if vertical edge cross
other.height+ other.y > y &&
other.y <height + y ;
}
使用
2.见评论
if (height <= 0) {
throw new IllegalArgumentException("Height can't be non-positive.");
}
if (width <= 0) {
throw new IllegalArgumentException("Width can't be non-positive.");
}
if (width > 0) { //width is always > 0 here so `if` is not needed
this.width = width;
}
if (height > 0) { //height is always > 0 here so `if` is not needed
this.height = height;
}
if(此处高度为0),因此不需要'if'
这个。宽度=宽度;