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