Determine if two rectangles overlap each other?

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

    struct rect
    {
        int x;
        int y;
        int width;
        int height;
    };
    
    bool valueInRange(int value, int min, int max)
    { return (value >= min) && (value <= max); }
    
    bool rectOverlap(rect A, rect B)
    {
        bool xOverlap = valueInRange(A.x, B.x, B.x + B.width) ||
                        valueInRange(B.x, A.x, A.x + A.width);
    
        bool yOverlap = valueInRange(A.y, B.y, B.y + B.height) ||
                        valueInRange(B.y, A.y, A.y + A.height);
    
        return xOverlap && yOverlap;
    }

提交回复
热议问题