java:<几何问题-设计一个MyRectangle2D类>

来源:互联网 发布:jquery书籍推荐 知乎 编辑:程序博客网 时间:2024/06/02 01:09

在只知道矩形中心,矩形长宽的情况下,

1.涉及如何判定一个点是否在一个矩形里。

2.判定一个矩形在另一个矩形里,也是只知道中心坐标和长宽。

3.判定一个矩形和另一个矩形相交,条件也如上。

设计一个java-MyRectangle2D类。





附两份代码,都是可以的,有些许细节用法不同。

public class Exercise10_12 {    public static void main(String[] args) {        MyRectangle2D r1 =new MyRectangle2D(2,2,5.5,4.9);        System.out.println("(3,3)是否在r1中:" +r1.contains(3, 3));        System.out.println("(4,5,10.5,3.2)是否在r1中:" +r1.contains(new MyRectangle2D(4, 5, 10.5, 3.2)));        System.out.println("(3, 5, 2.3, 5.4)是否与r1重叠:" +r1.overlaps(new MyRectangle2D(3,5,2.3,5.4)));            }    }class MyRectangle2D{    private double x;    private double y;    private double width;    private double height;    public double GETx(){        return x;    }    public double GETy(){        return y;    }    public double GETw(){        return width;    }    public double GETh(){        return height;    }    public void SETw(double new_width){        width=new_width;    }    public void SETh(double new_height){        height = new_height;    }    public MyRectangle2D(){        x = 0;        y = 0;        width = 1;        height = 1;    }    public MyRectangle2D(double new_x,double new_y,double width,double height) {        x = new_x;        y = new_y;        SETw(width);        SETh(height);    }    public double getAreac(){        return width*height;    }    public double Get_Perimeter(){        return (width+height)*2;    }    public boolean contains(double x,double y){        if(x <(this.x+width/2)&&(this.x-width/2)< x&&y<(this.y+height/2)&&y>(this.y-height/2)){            return true;        }         else        {            return false;        }    }    public boolean contains(MyRectangle2D r){        if(r.GETx()<(this.x+(width-r.GETw())/2)&&(this.x-(width-r.GETw())/2)< r.GETw()&&r.GETy()<(this.y+(height-r.GETh())/2)&&r.GETy()>(this.y+(height-r.GETh())/2)){            return true;        }         else        {            return false;        }    }    public boolean overlaps(MyRectangle2D r){        if(r.GETx()<(this.x+(width+r.GETw())/2)&&(this.x-(width+r.GETw())/2)< r.GETw()&&r.GETy()<(this.y+(height+r.GETh())/2)&&r.GETy()>(this.y+(height+r.GETh())/2)){            return true;        }         else        {            return false;        }    }}


public class Exercise10_12 {  public static void main(String[] args) {    MyRectangle2D r1 = new MyRectangle2D(2, 2, 5.5, 4.9);    System.out.println("Area is " + r1.getArea());    System.out.println("Perimeter is " + r1.getPerimeter());    System.out.println(r1.contains(3, 3));    System.out.println(r1.contains(new MyRectangle2D(4, 5, 10.5, 3.2)));    System.out.println(r1.overlaps(new MyRectangle2D(3, 5, 2.3, 6.7)));  }}class MyRectangle2D {  private double x, y; // Center of the rectangle  private double width, height;    public MyRectangle2D() {    x = y = 0;    width = height = 1;  }    public MyRectangle2D(double x, double y, double width, double height) {    this.x = x;    this.y = y;    this.width = width;    this.height = height;  }    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 double getPerimeter() {    return 2 * (width + height);  }    public double getArea() {    return width * height;  }    public boolean contains(double x, double y) {    return Math.abs(x - this.x) <= width / 2 &&      Math.abs(y - this.y) <= height / 2;  }    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) {        return Math.abs(this.x - r.x) <= (this.width + r.width) / 2 &&      Math.abs(this.y - r.y) <= (this.height + r.height) / 2;   }  



原创粉丝点击