题目:
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.
Tags: Math
分析:
notes:两个矩阵不同的位置关系会对相交区域造成影响
代码:
//判断相交面积和四个点的关系
#include <math>
class Solution {
public:
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
//两个角点分别为(max(A,E),max(B,F)),(min(C,G),min(D,H))
int overlap;
if(min(C,G)>max(A,E)&&min(D,H)>max(B,F))
overlap=(min(C,G)-max(A,E))*(min(D,H)-max(B,F));
else
overlap=0;
int total=(C-A)*(D-B)+(G-E)*(H-F)-overlap;
return total;
}
};