【问题描述】
平面上有个两个矩形 R 1 和 R 2 ,它们各边都与坐标轴平行。设 ( x 1 , y 1 ) 和 (x 2 , y 2 ) 依次是 R 1 的左下角和右上角坐标, ( x 3 , y 3 ) 和 ( x 4 , y 4 ) 依次是 R 2 的左下 角和右上角坐标,请你计算 R 1 和 R 2 的总面积是多少?
注意:如果 R 1 和 R 2 有重叠区域,重叠区域的面积只计算一次。
【输入格式】
输入只有一行,包含 8 个整数,依次是: x 1 , y 1 , x 2 , y 2 , x 3 , y 3 , x 4 和 y 4 。
【输出格式】
一个整数,代表答案。
【样例输入】
2 1 7 4 5 3 8 6
【样例输出】
22
package com.java.LanqQiaoBei;
import java.util.Scanner;
public class Area {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x1 = sc.nextInt();
int y1 = sc.nextInt();
int x2 = sc.nextInt();
int y2 = sc.nextInt();
int x3 = sc.nextInt();
int y3 = sc.nextInt();
int x4 = sc.nextInt();
int y4 = sc.nextInt();
long area1 = (long) (x2 - x1) * (y2 - y1); // 计算第一个矩形的面积
long area2 = (long) (x4 - x3) * (y4 - y3); // 计算第二个矩形的面积
long overlapArea=0;
long l = Math.min(x2, x4) - Math.max(x1, x3);
long w= Math.min(y2,y4)-Math.max(y1,y3);
if (l >=0&&w >=0){
overlapArea= l * w;
}
long Area = area1 + area2 - overlapArea; // 总面积
System.out.println(Area); // 输出总面积
}
}