**3.28(几何:两个矩形)编写一个程序,提示用户输入两个矩形中心的x坐标和y坐标以及矩形的宽度和高度,然后判断第二个矩形是在第一个矩形内,还是和第一个矩形重叠
题目
题目概述
**3.28(几何:两个矩形)编写一个程序,提示用户输入两个矩形中心的x坐标和y坐标以及矩形的宽度和高度,然后判断第二个矩形是在第一个矩形内,还是和第一个矩形重叠
运行示例
Enter r1’s center x-. y- coordinates, width, and height: 2.5 4 2.5 43
Enter r2’s center x-, y- coordinates, width, and height: 1.5 5 0.5 3
r2 is inside r1
Enter r1’s center x-. y- coordinates, width, and height: 1 2 3 5.5
Enter r2’s center x-, y- coordinates, width, and height: 3 4 4.5 5
r2 overlaps r1
Enter r1’s center x-. y- coordinates, width, and height: 1 2 3 3
Enter r2’s center x-, y- coordinates, width, and height: 40 45 3 2
r2 does not overlap r1
破题
三种情况:
- 一个矩形(中心(x2,y2))在另一个(中心(x1,y1))之内:两个中心距离在x、y上的分量假设为x0、y0,中心(x1,y1)的高为h1、宽为w1,中心(x2,y2)的高为h2、宽为w2,则此时(x0 + w2 / 2) < (w1 / 2),且(y0 + h2 / 2) < (h1 / 2)
- 重叠(变量用1中的):((w1 - w2) / 2) < x0 < ((w1 + w2) / 2) 且 ((h1 - h2) / 2) < y0 < ((h1 + h2) / 2)
- 相离:x0 > ((w1 + w2) / 2) 或 y0 > ((h1 + h2) / 2)
代码
import java.util.Scanner;
public class Test3_28 {
public static void main(String[] args