Determine if two rectangles overlap each other?

前端 未结 23 836
礼貌的吻别
礼貌的吻别 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:48

    Lets say the two rectangles are rectangle A and rectangle B. Let their centers be A1 and B1 (coordinates of A1 and B1 can be easily found out), let the heights be Ha and Hb, width be Wa and Wb, let dx be the width(x) distance between A1 and B1 and dy be the height(y) distance between A1 and B1.

    Now we can say we can say A and B overlap: when

    if(!(dx > Wa+Wb)||!(dy > Ha+Hb)) returns true
    

提交回复
热议问题