Determine if two rectangles overlap each other?

前端 未结 23 1181
礼貌的吻别
礼貌的吻别 2020-11-22 04:09

I am trying to write a C++ program that takes the following inputs from the user to construct rectangles (between 2 and 5): height, width, x-pos, y-pos. All of these rectang

23条回答
  •  孤城傲影
    2020-11-22 04:55

    A and B be two rectangle. C be their covering rectangle.

    four points of A be (xAleft,yAtop),(xAleft,yAbottom),(xAright,yAtop),(xAright,yAbottom)
    four points of A be (xBleft,yBtop),(xBleft,yBbottom),(xBright,yBtop),(xBright,yBbottom)
    
    A.width = abs(xAleft-xAright);
    A.height = abs(yAleft-yAright);
    B.width = abs(xBleft-xBright);
    B.height = abs(yBleft-yBright);
    
    C.width = max(xAleft,xAright,xBleft,xBright)-min(xAleft,xAright,xBleft,xBright);
    C.height = max(yAtop,yAbottom,yBtop,yBbottom)-min(yAtop,yAbottom,yBtop,yBbottom);
    
    A and B does not overlap if
    (C.width >= A.width + B.width )
    OR
    (C.height >= A.height + B.height) 
    

    It takes care all possible cases.

提交回复
热议问题