在二维平面上计算出两个由直线构成的矩形重叠后形成的总面积。
每个矩形由其左下顶点和右上顶点坐标表示,如图所示。
示例:
输入: -3, 0, 3, 4, 0, -1, 9, 2 输出: 45
说明: 假设矩形面积不会超出 int 的范围。
class Solution {
public:
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H)
{
int x1 = max(A, E);
int y1 = max(B, F);
int x2 = min(C, G);
int y2 = min(D, H);
int s1 = (C - A) * (D - B);
int s2 = (G - E) * (H - F);
int s3 = 0;
if(x1 <= x2 && y1 <= y2)
s3 = (x2 - x1) * (y2 - y1);
return s1 + s2 - s3;
}
};