早教吧 育儿知识 作业答案 考试题库 百科 知识分享

一个平面的两物体,两物体在屏幕当中的左上角坐标为x,y,x1,y1,两物体的宽高分别为w,h,w1,h1,写条件判断两物体是否发生了重叠注:两物体都是矩形

题目详情
一个平面的两物体,两物体在屏幕当中的左上角坐标为x,y ,x1,y1,两物体的宽高分别为 w ,h,w1,h1,写条件判断两物体是否发生了重叠
注:两物体都是矩形
▼优质解答
答案和解析
private boolean isOverLap(int x, int y, int x1, int y1, int w, int h, int w1, int h1) {
boolean xOverlap;// x方向是否重叠
boolean yOverlap;// y方向是否重叠
boolean isOverlap;// 两个物体是否重叠
if (x > x1) {
if (x1 + w1 < x) {
xOverlap = false;
} else {
xOverlap = true;
}
} else {
if (x + w < x1) {
xOverlap = false;
} else {
xOverlap = true;
}
}
if (y > y1) {
if (y1 + h1 < y) {
yOverlap = false;
} else {
yOverlap = true;
}
} else {
if (y + h < y1) {
yOverlap = false;
} else {
yOverlap = true;
}
}
if (!xOverlap && !yOverlap) {
isOverlap = false;// 物体没有重叠
return false;
} else {
isOverlap = true;// 物体重叠
return true;
}
}