Determine if two rectangles overlap each other?

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

    Easiest way is

    /**
     * Check if two rectangles collide
     * x_1, y_1, width_1, and height_1 define the boundaries of the first rectangle
     * x_2, y_2, width_2, and height_2 define the boundaries of the second rectangle
     */
    boolean rectangle_collision(float x_1, float y_1, float width_1, float height_1, float x_2, float y_2, float width_2, float height_2)
    {
      return !(x_1 > x_2+width_2 || x_1+width_1 < x_2 || y_1 > y_2+height_2 || y_1+height_1 < y_2);
    }
    

    first of all put it in to your mind that in computers the coordinates system is upside down. x-axis is same as in mathematics but y-axis increases downwards and decrease on going upward.. if rectangle are drawn from center. if x1 coordinates is greater than x2 plus its its half of widht. then it means going half they will touch each other. and in the same manner going downward + half of its height. it will collide..

    0 讨论(0)
  • 2020-11-22 04:46
    struct Rect
    {
       Rect(int x1, int x2, int y1, int y2)
       : x1(x1), x2(x2), y1(y1), y2(y2)
       {
           assert(x1 < x2);
           assert(y1 < y2);
       }
    
       int x1, x2, y1, y2;
    };
    
    //some area of the r1 overlaps r2
    bool overlap(const Rect &r1, const Rect &r2)
    {
        return r1.x1 < r2.x2 && r2.x1 < r1.x2 &&
               r1.y1 < r2.y2 && r2.x1 < r1.y2;
    }
    
    //either the rectangles overlap or the edges touch
    bool touch(const Rect &r1, const Rect &r2)
    {
        return r1.x1 <= r2.x2 && r2.x1 <= r1.x2 &&
               r1.y1 <= r2.y2 && r2.x1 <= r1.y2;
    }
    
    0 讨论(0)
  • 2020-11-22 04:48
    if (RectA.Left < RectB.Right && RectA.Right > RectB.Left &&
         RectA.Top > RectB.Bottom && RectA.Bottom < RectB.Top ) 
    

    or, using Cartesian coordinates

    (With X1 being left coord, X2 being right coord, increasing from left to right and Y1 being Top coord, and Y2 being Bottom coord, increasing from bottom to top -- if this is not how your coordinate system [e.g. most computers have the Y direction reversed], swap the comparisons below) ...

    if (RectA.X1 < RectB.X2 && RectA.X2 > RectB.X1 &&
        RectA.Y1 > RectB.Y2 && RectA.Y2 < RectB.Y1) 
    

    Say you have Rect A, and Rect B. Proof is by contradiction. Any one of four conditions guarantees that no overlap can exist:

    • Cond1. If A's left edge is to the right of the B's right edge, - then A is Totally to right Of B
    • Cond2. If A's right edge is to the left of the B's left edge, - then A is Totally to left Of B
    • Cond3. If A's top edge is below B's bottom edge, - then A is Totally below B
    • Cond4. If A's bottom edge is above B's top edge, - then A is Totally above B

    So condition for Non-Overlap is

    NON-Overlap => Cond1 Or Cond2 Or Cond3 Or Cond4

    Therefore, a sufficient condition for Overlap is the opposite.

    Overlap => NOT (Cond1 Or Cond2 Or Cond3 Or Cond4)

    De Morgan's law says
    Not (A or B or C or D) is the same as Not A And Not B And Not C And Not D
    so using De Morgan, we have

    Not Cond1 And Not Cond2 And Not Cond3 And Not Cond4

    This is equivalent to:

    • A's Left Edge to left of B's right edge, [RectA.Left < RectB.Right], and
    • A's right edge to right of B's left edge, [RectA.Right > RectB.Left], and
    • A's top above B's bottom, [RectA.Top > RectB.Bottom], and
    • A's bottom below B's Top [RectA.Bottom < RectB.Top]

    Note 1: It is fairly obvious this same principle can be extended to any number of dimensions.
    Note 2: It should also be fairly obvious to count overlaps of just one pixel, change the < and/or the > on that boundary to a <= or a >=.
    Note 3: This answer, when utilizing Cartesian coordinates (X, Y) is based on standard algebraic Cartesian coordinates (x increases left to right, and Y increases bottom to top). Obviously, where a computer system might mechanize screen coordinates differently, (e.g., increasing Y from top to bottom, or X From right to left), the syntax will need to be adjusted accordingly/

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-22 04:50
    bool Square::IsOverlappig(Square &other)
    {
        bool result1 = other.x >= x && other.y >= y && other.x <= (x + width) && other.y <= (y + height); // other's top left falls within this area
        bool result2 = other.x >= x && other.y <= y && other.x <= (x + width) && (other.y + other.height) <= (y + height); // other's bottom left falls within this area
        bool result3 = other.x <= x && other.y >= y && (other.x + other.width) <= (x + width) && other.y <= (y + height); // other's top right falls within this area
        bool result4 = other.x <= x && other.y <= y && (other.x + other.width) >= x && (other.y + other.height) >= y; // other's bottom right falls within this area
        return result1 | result2 | result3 | result4;
    }
    
    0 讨论(0)
  • 2020-11-22 04:50

    "If you perform subtraction x or y coordinates corresponding to the vertices of the two facing each rectangle, if the results are the same sign, the two rectangle do not overlap axes that" (i am sorry, i am not sure my translation is correct)

    enter image description here

    Source: http://www.ieev.org/2009/05/kiem-tra-hai-hinh-chu-nhat-chong-nhau.html

    0 讨论(0)
提交回复
热议问题