leetCode 之 Rectangle Area
题目原意: 求解两个矩形覆盖的总面积
注意用总面积减去两者的共同覆盖面积即可,简单
代码如下(0ms):
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H)
{
int areaTotal;
int area_two = 0;
int x1,x2;
int y1, y2;
x1 = A >= E ? A : E;
x2 = C >= G ? G : C;
y1 = B >= F ? B : F;
y2 = D >= H ? H : D;
if ( (x1 <= x2) && (y1 <= y2) ) //!< 根据两个矩形的交点,求共同覆盖的面积
area_two = (x2 - x1) * (y2 - y1);
areaTotal = (C - A) * (D - B) + (G - E) * (H - F) - area_two; //!< 矩形的总面积减去共同覆盖的面积
return areaTotal;
}