3.28

3.28 判断第二个矩形是否在第一个矩形内

数学思想:第二个矩形的四个顶点是否在在第一个矩形的范围之内



public class Demo12 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		
		System.out.println("请输入(x1,y1):");
		System.out.println("请输入宽度和高度:");
		double x1 = input.nextDouble();
		double y1 = input.nextDouble();
		double width1 = input.nextDouble();
		double height1 = input.nextDouble();
		
		System.out.println("请输入(x2,y2)");
		double x2 = input.nextDouble();
		double y2 = input.nextDouble();
		double width2 = input.nextDouble();
		double height2 = input.nextDouble();
		
		double leftBorder = x1 - 0.5 * width1;
		double rightBorder = x1 + 0.5 * width1;
		double topBorder = y1 + 0.5 * height1;
		double bottomBorder = y1 - 0.5 * height1;
		
		double point1X = x2 - 0.5 * width2;
		double point1Y = y2 + 0.5 * height2;
		
		double point2X = x2 + 0.5 * width2;
		double point2Y = y2 + 0.5 * height2;
		
		double point3X = x2 - 0.5 * width2;
		double point3Y = y2 - 0.5 * height2;
		
		double point4X = x2 + 0.5 * width2;
		double point4Y = y2 - 0.5 * height2;
		
		boolean point1 = (leftBorder <= point1X && point1X <= rightBorder) && (bottomBorder <= point1Y && point1Y <= topBorder);
		boolean point2 = (leftBorder <= point2X && point2X <= rightBorder) && (bottomBorder <= point2Y && point2Y <= topBorder);
		boolean point3 = (leftBorder <= point3X && point3X <= rightBorder) && (bottomBorder <= point3Y && point3Y <= topBorder);
		boolean point4 = (leftBorder <= point4X && point4X <= rightBorder) && (bottomBorder <= point4Y && point4Y <= topBorder);
		
		if (point1 && point2 && point3 && point4) {
			System.out.println("圆2在圆1中");
		}else {
			System.out.println("圆2不在圆1中");
		}
		input.close();
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值