Determine if two rectangles overlap each other?

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

    struct point { int x, y; };
    
    struct rect { point tl, br; }; // top left and bottom right points
    
    // return true if rectangles overlap
    bool overlap(const rect &a, const rect &b)
    {
        return a.tl.x <= b.br.x && a.br.x >= b.tl.x && 
               a.tl.y >= b.br.y && a.br.y <= b.tl.y;
    }
    

提交回复
热议问题