下面是一个矩形类,编写一个方法isOverLap(rec1,rec2)(要简单有效),判断这两个矩形是否有重叠部分,有就返回true,否则返回false。
public class Rectangle{
private int left;
private int right;
private int top;
private int bottom; Rectangle(int left,int right,int top,int bottom){
this.left=left;
this.right=right;
this.top=top;
this.bottom=bottom;
}
}
public class Rectangle{
private int left;
private int right;
private int top;
private int bottom; Rectangle(int left,int right,int top,int bottom){
this.left=left;
this.right=right;
this.top=top;
this.bottom=bottom;
}
}
直接用 java.awt.Rectangle
有个函数专门用来干这个事情 public boolean intersects(Rectangle r)这种简单的计算早有人写好了
class IsOver
{
public boolean overOk(Rectangle rect1,Rectangle rect2)
{
if(rect1.left>rect2.right || rect1.top<rect2.top
|| rect1.right<rect2.right || rect1.bottom>rect2.top)
return true;
return false;
}
}
class IsOver
{
public boolean overOk(Rectangle rect1,Rectangle rect2)
{
if(rect1.left>rect2.right || rect1.top <rect2.bottom
|| rect1.right <rect2.right || rect1.bottom>rect2.top)
return true;
return false;
}
}