package pack2;
import java.util.Arrays;
import java.util.Scanner;
public class BoundRectangle {
public static void main(String[] args) {
boundRectangle();
}
// 1.0 2.5 3 4 5 6 7 8 9 10
public static void boundRectangle() {
try(Scanner input = new Scanner(System.in);) {
final int COLUMN = 2;
System.out.print("Enter the number of points: ");
double[][] points = new double[input.nextInt()][COLUMN];
System.out.print("Enter " + points.length + " points: ");
inputData(input, points);
MyRectangle2D boundRectangle = getRectangle(points);
System.out.println("The bounding rectangle's center (" +
boundRectangle.getX() +
", " + boundRectangle.getY() + "), width " +
boundRectangle.getWidth() +
", height " + boundRectangle.getHeight());
}
}
/**输入数据*/
public static void inputData(Scanner input, double[][] points) {
for (int i = 0; i < points.length; i++)
for (int j = 0; j < points[i].length; j++)
points[i][j] = input.nextDouble();
}
/**返回边界矩形*/
public static MyRectangle2D getRectangle(double[][] points) {
double[] x = new double[points.length]; //x坐标集
double[] y = new double[points.length]; //y坐标集
for (int i = 0; i < points.length; i++) { //坐标集赋值
x[i] = points[i][0];
y[i] = points[i][1];
}
Arrays.sort(x); //坐标集排序
Arrays.sort(y);
double xMax = x[x.length - 1]; //x坐标集的最大值
double yMax = y[y.length - 1]; //y坐标集的最大值
return new MyRectangle2D((xMax + x[0]) / 2, (yMax + y[0]) / 2, xMax - x[0],
yMax - y[0]);
}
}