public class MyRectangle2D {
public double x=0;
public double y=0;
public double width=1;
public double height=1;
public double getX() {
return x;
}
public void setX(double x) {
this.x=x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y=y;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width=width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height=height;
}
public MyRectangle2D(double x,double y,double width,double height) {
this.x=x;
this.y=y;
this.width=width;
this.height=height;
}
public double getArea() {
return width*height;
}
public double getPerimeter() {
return 2*(width+height);
}
public boolean contains(double x,double y) {
boolean result =false;
if((Math.abs(x-this.x)<=width/2)&&(Math.abs(y-this.y)<=height/2))
result=true;
return result;
}
public boolean contains(MyRectangle2D r) {
return contains(r.x-r.width/2,r.y-r.height/2)&&
contains(r.x-r.width/2,r.y+r.height/2)&&
contains(r.x+r.width/2,r.y-r.height/2)&&
contains(r.x+r.width/2,r.y+r.height/2);
}
public boolean overlaps(MyRectangle2D r) {
if(contains(r)==true)
return false;
else
return contains(r.x-r.width/2,r.y-r.height/2)||
contains(r.x-r.width/2,r.y+r.height/2)||
contains(r.x+r.width/2,r.y-r.height/2)||
contains(r.x+r.width/2,r.y+r.height/2);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
MyRectangle2D r1 =new MyRectangle2D(2,2,5.5,4.9);
System.out.println("面积为"+r1.getArea()+"周长为"+r1.getPerimeter());
System.out.println("点(3,3)是否在矩形内"+r1.contains(3,3));
System.out.println("矩形(4,5,10,5,3.2)是否在该矩形内"+r1.contains(new MyRectangle2D(3,5,2.3,5.4)));
System.out.println("是否与矩形(3,5,2.3,5.4)重叠"+r1.overlaps(new MyRectangle2D(3,5,2.3,5.4)));
}
}