帮助!现在正在寻找几个小时,每个矩形对象组成 x坐标,y co-rdinate,矩形的高度和宽度
任何有关我出错的地方的帮助都会让我感到宽慰!提前谢谢你。
public static boolean intersection(Rectangle r1, Rectangle r2) {
int xmin = Math.max(r1.x, r2.x); //find the maximum minimum x co-ordinate value between rectangle 1 and 2 entered
int xmax1 = r1.x + r1.width; //finds max co-ordinate for rectangle 1
int xmax2 = r2.x + r2.width; //finds max co-ordinate for rectangle 2
int xmax = Math.min(xmax1, xmax2); //out of both max rect 1 and 2, take minimum as this is where would intersect
if (xmax >= xmin) { //if true then its in the same x co-ordinates
int ymin = Math.max(r1.y, r2.y); //do same for y co-ordinate
int ymax1 = r1.y + r1.height;
int ymax2 = r2.y + r2.height;
int ymax = Math.min(ymax1, ymax2);
if ( ymax <= ymax2 ) //if in-between this, then two rectangles intersects
return true;
}
return false; //else doesn't intersect
}
答案 0 :(得分:1)
看起来代码中唯一的问题是:
if ( ymax <= ymax2 )
...应该与其他if
:
if ( ymax >= ymin )