Java、边框

在这里插入图片描述

 


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]);
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值