【leetcode】223. 矩形面积

 

#define MAX(a,b) ((a) > (b) ? (a) :(b))
#define MIN(a,b) ((a) < (b) ? (a) :(b))

int computeArea(int A, int B, int C, int D, int E, int F, int G, int H){
    int overlap_area = 0;
    int area = 0;

    if(!(E >= C|| F >= D || G <= A | H <= B))
    {
        int x1 = MAX(A,E);
        int x2 = MIN(C,G);
        int y1 = MAX(F,B);
        int y2 = MIN(D,H);

        overlap_area = (x2 - x1) * (y2 - y1);
    }

    int area1 = (C - A) * (D - B);
    int area2 = (G - E) * (H -F );


    return area1 + (area2 - overlap_area);
}

 

posted @ 2020-12-28 20:44  温暖了寂寞  阅读(89)  评论(0编辑  收藏  举报