After looking at the discuss area, I'm going to be crazy


  • 7
    X

    that's the solution after i try thousands of times. you can laugh at me now......

    class Solution {
    public:
    struct myPoint
    {
        int x;
        int y;
        bool flag;
    };
    struct myRect
    {
        myPoint bl;
        myPoint tr;
        myPoint tl;
        myPoint br;
    };
    myRect rect1;
    myRect rect2;
    vector<myPoint> pts;
    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H)
    {
        int myinterarea=InterArea(A,B,C,D,E,F,G,H);
        return myArea(rect1)+myArea(rect2)-myinterarea;
    }
    int InterArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        rect1.bl.x=A;
        rect1.bl.y=B;
        rect1.tr.x=C;
        rect1.tr.y=D;
        rect1.tl.x=A;
        rect1.tl.y=D;
        rect1.br.x=C;
        rect1.br.y=B;
        rect2.bl.x=E;
        rect2.bl.y=F;
        rect2.tr.x=G;
        rect2.tr.y=H;
        rect2.tl.x=E;
        rect2.tl.y=H;
        rect2.br.x=G;
        rect2.br.y=F;
        
        int ptinrect1=0,ptinrect2=0;
        if(myPtInRect(rect2.tl,rect1))
            ptinrect1++;
        if(myPtInRect(rect2.tr,rect1))
            ptinrect1++;
        if(myPtInRect(rect2.bl,rect1))
            ptinrect1++;
        if(myPtInRect(rect2.br,rect1))
            ptinrect1++;
        
        if(myPtInRect(rect1.tl,rect2))
            ptinrect2++;
        if(myPtInRect(rect1.tr,rect2))
            ptinrect2++;
        if(myPtInRect(rect1.bl,rect2))
            ptinrect2++;
        if(myPtInRect(rect1.br,rect2))
            ptinrect2++;
            
        if(ptinrect1==4&&ptinrect2==4)
            return myArea(rect1);
        if(ptinrect1==4&&ptinrect2==0)
            return myArea(rect2);
        if(ptinrect1==0&&ptinrect2==4)
            return myArea(rect1);
        if(ptinrect1==2&&ptinrect2==2)
        {
            int xmin=min<int>(pts[0].x,min<int>(pts[1].x,min<int>(pts[2].x,pts[3].x)));
            int xmax=max<int>(pts[0].x,max<int>(pts[1].x,max<int>(pts[2].x,pts[3].x)));
            int ymin=min<int>(pts[0].y,min<int>(pts[1].y,min<int>(pts[2].y,pts[3].y)));
            int ymax=max<int>(pts[0].y,max<int>(pts[1].y,max<int>(pts[2].y,pts[3].y)));
            return (xmax-xmin)*(ymax-ymin);
        }
        if(ptinrect1==1&&ptinrect2==1)
        {
            int xmin=min<int>(pts[0].x,pts[1].x);
            int xmax=max<int>(pts[0].x,pts[1].x);
            int ymin=min<int>(pts[0].y,pts[1].y);
            int ymax=max<int>(pts[0].y,pts[1].y);
            return (xmax-xmin)*(ymax-ymin);
        }
        if((ptinrect1==2&&ptinrect2==0)||(ptinrect1==0&&ptinrect2==2))
        {
            if(ptinrect1==0&&ptinrect2==2)
            {
                myRect temprect=rect1;
                rect1=rect2;
                rect2=temprect;
            }
            if(pts[0].x==pts[1].x)
            {
                if(pts[0].x==rect2.tl.x)
                    return (rect1.br.x-rect2.tl.x)*(rect2.tl.y-rect2.bl.y);
                else if(pts[0].x==rect2.tr.x)
                    return (rect2.br.x-rect1.tl.x)*(rect2.tr.y-rect2.br.y);
            }
            else if(pts[0].y==pts[1].y)
            {
                if(pts[0].y==rect2.tl.y)
                    return (rect2.tr.x-rect2.tl.x)*(rect2.tl.y-rect1.br.y);
                else if(pts[0].y==rect2.br.y)
                    return (rect2.br.x-rect2.bl.x)*(rect1.tl.y-rect2.br.y);
            }
        }
        if(ptinrect1==0&&ptinrect2==0)
        {
            if((rect1.tl.x>rect2.br.x)||(rect2.tl.x>rect1.br.x)||(rect1.tl.y<rect2.br.y)||(rect2.tl.y<rect1.br.y))
                return 0;
            else
            {
                myPoint newtl,newbr;
                newtl.x=max<int>(rect1.tl.x,rect2.tl.x);
                newtl.y=min<int>(rect1.tl.y,rect2.tl.y);
                newbr.x=min<int>(rect1.br.x,rect2.br.x);
                newbr.y=max<int>(rect1.br.y,rect2.br.y);
                return (newbr.x-newtl.x)*(newtl.y-newbr.y);
            }
        }
        if((ptinrect1==1&&ptinrect2==2)||(ptinrect1==2&&ptinrect2==1))
        {
            if((rect1.tl.x>=rect2.br.x)||(rect2.tl.x>=rect1.br.x)||(rect1.tl.y<=rect2.br.y)||(rect2.tl.y<=rect1.br.y))
                return 0;
            else
            {
                myPoint newtl,newbr;
                newtl.x=min<int>(pts[0].x,min<int>(pts[1].x,pts[2].x));
                newtl.y=max<int>(pts[0].y,max<int>(pts[1].y,pts[2].y));
                newbr.x=max<int>(pts[0].x,max<int>(pts[1].x,pts[2].x));
                newbr.y=min<int>(pts[0].y,min<int>(pts[1].y,pts[2].y));
                return (newbr.x-newtl.x)*(newtl.y-newbr.y);
            }
        }
        if((ptinrect1==1&&ptinrect2==4)||(ptinrect1==4&&ptinrect2==1))
            return min<int>(myArea(rect1),myArea(rect2));
        if((ptinrect1==2&&ptinrect2==4)||(ptinrect1==4&&ptinrect2==2))
            return min<int>(myArea(rect1),myArea(rect2));
    }
    
    int myArea(myRect rect)
    {
        return (rect.tr.x-rect.bl.x)*(rect.tr.y-rect.bl.y);
    }
    
    bool myPtInRect(myPoint pt,myRect rect)
    {
        if(pt.x>=rect.bl.x&&pt.x<=rect.tr.x&&pt.y>=rect.bl.y&&pt.y<=rect.tr.y)
        {
            pts.push_back(pt);
            return true;
        }
        return false;
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.