# 1 题目描述

Find the total area covered by two rectilinear rectangles in a 2D plane.

Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.

Assume that the total area is never beyond the maximum possible value of int.

# 2 解题思路

1.总面积 = 矩形A  + 矩形B - A与B相交的部分。

# 3 源代码

package com.larry.easy;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class RectangleArea {
public boolean isIntersected(int A, int B, int C, int D, int E, int F, int G, int H){
if(A >= G) return false;
if(F >= D) return false;
if(E >= C) return false;
if(B >= H) return false;
return true;
}

public int getMidTwo(int a, int b, int c, int d){
int retn = 0;
List<Integer> ls = new ArrayList<Integer>();
Collections.sort(ls);
retn = ls.get(2) - ls.get(1);
return retn;
}

public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int totalBef = (C-A)*(D-B) + (G-E)*(H-F);//分别计算两个矩形

boolean isInted = isIntersected(A, B, C, D, E, F, G, H);
if(isInted) {//如果相交，则减去相交的部分
int length = getMidTwo(A, C, E, G);
int width = getMidTwo(B, D, F, H);
totalBef = totalBef - length*width;
}

}
public static void main(String[] args) {
/*int a = -3;
int b = 0;
int c = 3;
int d = 4;
int e = 0;
int f = -1;
int g = 9;
int h = 2;*/
/*int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = -1;
int f = -1;
int g = 1;
int h = 1;*/
int a = -2;
int b = -2;
int c = 2;
int d = 2;
int e = 3;
int f = 3;
int g = 4;
int h = 4;

int A = a;
int B = b;
int C = c;
int D = d;
int E = e;
int F = f;
int G = g;
int H = h;
RectangleArea ra = new RectangleArea();
System.out.println(ra.computeArea(A, B, C, D, E, F, G, H));
}
}


#### [LintCode]Rectangle Area(Java)

2017-08-05 15:19:05

#### leetcode 223: Rectangle Area

2015-06-10 03:25:56

#### 【LeetCode-面试算法经典-Java实现】【223-Rectangle Area（矩形区域）】

2015-08-29 06:39:48

#### 850. Rectangle Area II

2018-06-10 13:35:48

#### 223. Rectangle Area [easy] (Python)

2016-06-15 11:24:21

#### leetcode -- Rectangle Area -- 看看计算公式

2015-12-03 12:46:36

#### hdu 4101 Ali and Baba(bfs,dfs)

2015-08-10 16:16:15

#### Leetcode 850 矩形面积 II

2018-06-10 11:02:43

#### （1）定义一个Rectangle类，它包括两个数据成员长len和宽width，以及求面积的成员函数Area，另外定义Set函数对私有数据成员进行设置。在主函数定义一个对象，输出其面积。（可自行增加需要

2018-05-06 09:17:39

#### Q74：面积光源（Area Light）

2017-03-02 14:20:10