题目链接:https://leetcode.com/problems/rectangle-area/
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.
class Solution {
public:
int computeArea(int A,int B,int C,int D,int E,int F,int G,int H)
{
int area1;
int area2;
int lap;
if(C>A && D>B)
area1=(C-A)*(D-B);
else
area1=0;
if(G>E && H>F)
area2=(G-E)*(H-F);
else
area2=0;
int innerLeft=A>E?A:E;
int innerRight=G>C?C:G;
int innerUp=D>H?H:D;
int innerBottom=B>F?B:F;
int lengthLap=(innerRight>innerLeft)?(innerRight-innerLeft):0;
int widthLap=(innerUp>innerBottom)?(innerUp-innerBottom):0;
lap=lengthLap*widthLap;
return area1+area2-lap;
}
};