Java黑皮书课后题第10章:*10.15(几何:边框)边框是指包围一个二维平面上点集的最小矩形,编写一个方法,为二维平面上一系列点返回一个边框

*10.15编写一个方法,为二维平面上一系列点返回一个边框

  • 题目
    • 程序
  • 代码
    • Test15.java
    • Test13_MyRectangle2D.java
    • 运行结果

题目

在这里插入图片描述
点击这里跳转编程练习题10.13

程序

Test15.java:测试程序
Test13_MyRectangle2D.java:编程练习题10.13构造程序

代码

Test15.java

import java.util.Scanner;public class Test15 {public static void main(String[] args) {// 获取5个点的坐标double[][] points = new double[5][2];System.out.print("Enter five points: ");Scanner input = new Scanner(System.in);for (int m = 0 ; m < 5 ; m++){for (int n = 0 ; n < 2 ; n++){points[m][n] = input.nextDouble();}}// 结束结果Test13_MyRectangle2D mt = getRectangle(points);System.out.print("The bounding rectangle's center (" + mt.getX() + ", " + mt.getY() +"), width " +mt.getWidth() + ", height " + mt.getHeight());}public static Test13_MyRectangle2D getRectangle(double[][] points){double xMax = points[0][0], yMax = points[0][1], xMin = points[0][0], yMin = points[0][1];for (int a = 0 ; a < points.length ; a++){if (points[a][0] > xMax)xMax = points[a][0];if (points[a][0] < xMin)xMin = points[a][0];if (points[a][1] > yMax)yMax = points[a][1];if (points[a][1] < yMin)yMin = points[a][1];}return new Test13_MyRectangle2D((xMax + xMin) / 2, (yMax + yMin) / 2, xMax - xMin, yMax - yMin);}
}

Test13_MyRectangle2D.java

public class Test13_MyRectangle2D {private double x, y;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;}private double width, height;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 Test13_MyRectangle2D(){x = 0;y = 0;width = 1;height = 1;}public Test13_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 bool1 = false, bool2 = false;if (x > this.x - width / 2 && x < this.x + width / 2)bool1 = true;if (y > this.y - height / 2 && y < this.y + height / 2)bool2 = true;return bool1 && bool2;}public boolean contains(Test13_MyRectangle2D r){return ((Math.abs(x - r.x)) < width / 2) && (Math.abs(y - r.y) < height / 2);}public boolean overlaps(Test13_MyRectangle2D r){return ((Math.abs(x - r.x)) < (width / 2 + r.width / 2)) && (Math.abs(y - r.y) < (height / 2 + r.height / 2));}
}

运行结果

Enter five points:1.0 2.5 3 4 5 6 7 8 9 10
The bounding rectangle's center (5.0, 6.25), width 8.0, height 7.5


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部