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; }
阅读全文
0 0
- java:<几何问题-设计一个MyRectangle2D类>
- 一个趣味几何问题:Triangle_Puzzle
- 一个有趣的几何问题
- 一个几何问题和猜测
- java--设计一个Person类
- 多边形重心问题_nyoj_3(计算几何).java
- 几何问题
- 我的答案:一个趣味几何问题Triangle_Puzzle
- 史上最难的初等几何问题?分享一个参考答案
- Java设计一个表示学生的类
- java类的设计(如何设计一个类)
- 设计一个类时应该问自己四个问题
- java最基础类的设计之判断一个类是不是一个设计良好的类
- Java:一个完整OO设计
- java中设计一个进度条
- 一个java简单问题
- 一个JAVA小问题
- Java 一个问题
- 使用maven创建web项目
- 404 Not Found错误页面的解决方法和注意事项
- JS倒计时
- 观察者模式
- sas日志输出到多个文件
- java:<几何问题-设计一个MyRectangle2D类>
- 基于服务的云计算模式
- 应该选择AWS还是Azure云平台
- HttpServletResponse响应数据--setContentType
- 欢迎使用CSDN-markdown编辑器
- jquery 获取图片的宽高
- GitHub 提交提示错误
- 文章标题
- Maven中使用tomcat:run出现错误org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException