代码之家  ›  专栏  ›  技术社区  ›  Rob Burke

确定两个矩形是否重叠?

  •  295
  • Rob Burke  · 技术社区  · 16 年前

    我正在尝试编写一个C++程序,该程序使用用户的下列输入来构造矩形(在2和5之间):高度、宽度、X-POS、Y-PO。所有这些矩形都将平行于X和Y轴,也就是说,它们的所有边将具有0或无穷大的斜率。

    我试着实现 this 但我运气不太好。

    我当前的实现如下:

    // Gets all the vertices for Rectangle 1 and stores them in an array -> arrRect1
    // point 1 x: arrRect1[0], point 1 y: arrRect1[1] and so on...
    // Gets all the vertices for Rectangle 2 and stores them in an array -> arrRect2
    
    // rotated edge of point a, rect 1
    int rot_x, rot_y;
    rot_x = -arrRect1[3];
    rot_y = arrRect1[2];
    // point on rotated edge
    int pnt_x, pnt_y;
    pnt_x = arrRect1[2]; 
    pnt_y = arrRect1[3];
    // test point, a from rect 2
    int tst_x, tst_y;
    tst_x = arrRect2[0];
    tst_y = arrRect2[1];
    
    int value;
    value = (rot_x * (tst_x - pnt_x)) + (rot_y * (tst_y - pnt_y));
    cout << "Value: " << value;  
    

    然而,我不太确定(a)我是否已经实现了我正确链接到的算法,或者我是否确实做到了如何解释这一点?

    有什么建议吗?

    22 回复  |  直到 6 年前
        1
  •  634
  •   Charles Bretana    8 年前
    if (RectA.Left < RectB.Right && RectA.Right > RectB.Left &&
         RectA.Top > RectB.Bottom && RectA.Bottom < RectB.Top ) 
    

    或者,使用笛卡尔坐标

    (x1为左坐标,x2为右坐标,从左向右增加,y1为上坐标,y2为下坐标,从下向上增加)……

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

    注意:所有具有编辑权限的SO用户。请不要再摆弄这个了。

    假设你有直肠A和直肠B。 证据是矛盾的。四个条件中的任何一个保证 不存在重叠 :

    • 康德1如果A的左边是B的右边, -那么A完全在B的右边
    • COND2.如果A的右边是B的左边, -那么A完全在B的左边
    • 康德如果A的上边缘低于B的下边缘, -那么A完全低于B
    • 康德4如果A的底边高于B的顶边, -那么A完全高于B

    所以不重叠的条件是

    Cond1 Or Cond2 Or Cond3 Or Cond4

    因此,重叠的一个充分条件是相反的。

    Not (Cond1 Or Cond2 Or Cond3 Or Cond4)

    德摩根定律说
    Not (A or B or C or D) 是一样的 Not A And Not B And Not C And Not D
    所以用德摩根,我们有

    Not Cond1 And Not Cond2 And Not Cond3 And Not Cond4

    这相当于:

    • A的左边缘到B的右边缘的左边缘,[ RectA.Left < RectB.Right ,以及
    • A的右边缘到B的左边缘的右边缘,[ RectA.Right > RectB.Left ,以及
    • A在B的底部上方,[ RectA.Top > RectB.Bottom ,以及
    • A在B的顶部下面[ RectA.Bottom < RectB.Top ]

    注释1 :很明显,同样的原理可以扩展到任何数量的维度。
    注释2 :只计算一个像素的重叠也应该相当明显,更改 < 和/或 > 在那个边界上 <= 或A >= .
    注释3 :当使用笛卡尔坐标(x,y)时,此答案基于标准代数笛卡尔坐标(x从左到右增加,y从下到上增加)。显然,如果计算机系统可能机械化屏幕坐标不同(例如,从上到下增加y,或从右到左增加x),则需要相应地调整语法。/

        2
  •  106
  •   James Eichele Bernard Igiri    11 年前
    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;
    }
        3
  •  24
  •   David Norman    14 年前
    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;
    };
    
    bool
    overlap(const Rect &r1, const Rect &r2)
    {
        // The rectangles don't overlap if
        // one rectangle's minimum in some dimension 
        // is greater than the other's maximum in
        // that dimension.
    
        bool noOverlap = r1.x1 > r2.x2 ||
                         r2.x1 > r1.x2 ||
                         r1.y1 > r2.y2 ||
                         r2.y1 > r1.y2;
    
        return !noOverlap;
    }
    
        4
  •  20
  •   Björn Kechel    14 年前

    更容易检查一个矩形是否完全位于另一个矩形之外,因此如果它是

    在左边…

    (r1.x + r1.width < r2.x)
    

    或者在右边……

    (r1.x > r2.x + r2.width)
    

    或者在上面…

    (r1.y + r1.height < r2.y)
    

    或者在底部…

    (r1.y > r2.y + r2.height)
    

    在第二个矩形中,它不可能与之碰撞。因此,为了有一个函数返回一个布尔值,说明矩形是否发生碰撞,我们只需通过逻辑OR组合条件,并对结果求反:

    function checkOverlap(r1, r2) : Boolean
    { 
        return !(r1.x + r1.width < r2.x || r1.y + r1.height < r2.y || r1.x > r2.x + r2.width || r1.y > r2.y + r2.height);
    }
    

    要想在只触摸时获得肯定的结果,我们可以将“<”和“>”更改为“<=”和“>=”。

        5
  •  6
  •   hkBattousai    10 年前

    假设您已经像这样定义了矩形的位置和大小:

    enter image description here

    我的C++实现是这样的:

    class Vector2D
    {
        public:
            Vector2D(int x, int y) : x(x), y(y) {}
            ~Vector2D(){}
            int x, y;
    };
    
    bool DoRectanglesOverlap(   const Vector2D & Pos1,
                                const Vector2D & Size1,
                                const Vector2D & Pos2,
                                const Vector2D & Size2)
    {
        if ((Pos1.x < Pos2.x + Size2.x) &&
            (Pos1.y < Pos2.y + Size2.y) &&
            (Pos2.x < Pos1.x + Size1.x) &&
            (Pos2.y < Pos1.y + Size1.y))
        {
            return true;
        }
        return false;
    }
    

    如上图所示的函数调用示例:

    DoRectanglesOverlap(Vector2D(3, 7),
                        Vector2D(8, 5),
                        Vector2D(6, 4),
                        Vector2D(9, 4));
    

    内部比较 if 块如下所示:

    if ((Pos1.x < Pos2.x + Size2.x) &&
        (Pos1.y < Pos2.y + Size2.y) &&
        (Pos2.x < Pos1.x + Size1.x) &&
        (Pos2.y < Pos1.y + Size1.y))
                     ↓  
    if ((   3   <    6   +   9    ) &&
        (   7   <    4   +   4    ) &&
        (   6   <    3   +   8    ) &&
        (   4   <    7   +   5    ))
    
        6
  •  5
  •   bstpierre Edgar Aviles    13 年前

    问自己一个相反的问题:如何确定两个矩形是否完全不相交?显然,完全位于矩形b左侧的矩形a不相交。如果A完全在右边。同样地,如果A完全在B之上或完全在B之下,在任何其他情况下,A和B相交。

    以下内容可能有缺陷,但我对算法很有信心:

    struct Rectangle { int x; int y; int width; int height; };
    
    bool is_left_of(Rectangle const & a, Rectangle const & b) {
       if (a.x + a.width <= b.x) return true;
       return false;
    }
    bool is_right_of(Rectangle const & a, Rectangle const & b) {
       return is_left_of(b, a);
    }
    
    bool not_intersect( Rectangle const & a, Rectangle const & b) {
       if (is_left_of(a, b)) return true;
       if (is_right_of(a, b)) return true;
       // Do the same for top/bottom...
     }
    
    bool intersect(Rectangle const & a, Rectangle const & b) {
      return !not_intersect(a, b);
    }
    
        7
  •  3
  •   Lyle    13 年前

    下面是如何在Java API中完成的:

    public boolean intersects(Rectangle r) {
        int tw = this.width;
        int th = this.height;
        int rw = r.width;
        int rh = r.height;
        if (rw <= 0 || rh <= 0 || tw <= 0 || th <= 0) {
            return false;
        }
        int tx = this.x;
        int ty = this.y;
        int rx = r.x;
        int ry = r.y;
        rw += rx;
        rh += ry;
        tw += tx;
        th += ty;
        //      overflow || intersect
        return ((rw < rx || rw > tx) &&
                (rh < ry || rh > ty) &&
                (tw < tx || tw > rx) &&
                (th < ty || th > ry));
    }
    
        8
  •  2
  •   Adam Tegen    16 年前
    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;
    }
    
        9
  •  1
  •   Will    16 年前

    在这个问题中,当矩形处于任意旋转角度时,您将链接到数学。但是,如果我理解问题中关于角度的一点,我会解释所有的矩形都是相互垂直的。

    一般知道重叠公式的面积是:

    使用示例:

       1   2   3   4   5   6
    
    1  +---+---+
       |       |   
    2  +   A   +---+---+
       |       | B     |
    3  +       +   +---+---+
       |       |   |   |   |
    4  +---+---+---+---+   +
                   |       |
    5              +   C   +
                   |       |
    6              +---+---+
    

    1)将所有X坐标(左、右)收集到一个列表中,然后对其进行排序并删除重复项

    1 3 4 5 6

    2)将所有Y坐标(顶部和底部)收集到一个列表中,然后对其进行排序并删除重复项

    1 2 3 4 6

    3)通过唯一X坐标之间的间隙数*唯一Y坐标之间的间隙数创建二维阵列。

    4 * 4

    4)将所有矩形绘制到此网格中,增加每个单元格的计数:

       1   3   4   5   6
    
    1  +---+
       | 1 | 0   0   0
    2  +---+---+---+
       | 1 | 1 | 1 | 0
    3  +---+---+---+---+
       | 1 | 1 | 2 | 1 |
    4  +---+---+---+---+
         0   0 | 1 | 1 |
    6          +---+---+
    

    5)绘制矩形时,很容易截取重叠部分。

        10
  •  1
  •   Mike Dunlavey    16 年前

    不要认为坐标表示像素的位置。把它们想象成像素之间。这样,2x2矩形的面积应该是4,而不是9。

    bool bOverlap = !((A.Left >= B.Right || B.Left >= A.Right)
                   && (A.Bottom >= B.Top || B.Bottom >= A.Top));
    
        11
  •  1
  •   Zar E Ahmer    11 年前

    最简单的方法是

    /**
     * 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);
    }
    

    首先,请记住,在计算机中,坐标系是颠倒的。x轴和数学中的一样,但y轴向下增加,向上减少。 如果从中心绘制矩形。 如果x1坐标大于x2加上其宽度的一半。那就意味着走一半,他们会互相碰触。以同样的方式向下+一半的高度。它会碰撞……

        12
  •  1
  •   simhumileco Janarthanan Ramu    6 年前

    假设这两个矩形是矩形a和矩形b,中心是a1和b1(很容易找到a1和b1的坐标),高度是ha和hb,宽度是wa和wb,dx是a1和b1之间的宽度(x)距离,dy是a1和b1之间的高度(y)距离。

    现在我们可以说A和B重叠:什么时候

    if(!(dx > Wa+Wb)||!(dy > Ha+Hb)) returns true
    
        13
  •  0
  •   baretta    16 年前

    我已经实现了一个C版本,它很容易转换成C++。

    public bool Intersects ( Rectangle rect )
    {
      float ulx = Math.Max ( x, rect.x );
      float uly = Math.Max ( y, rect.y );
      float lrx = Math.Min ( x + width, rect.x + rect.width );
      float lry = Math.Min ( y + height, rect.y + rect.height );
    
      return ulx <= lrx && uly <= lry;
    }
    
        14
  •  0
  •   himanshu    12 年前

    我有一个非常简单的解决方案

    让x1、y1 x2、y2、l1、b1、l2分别为坐标和长度和宽度。

    考虑条件((x2

    现在,这些矩形重叠的唯一方式是,如果指向x1、y1的点位于另一个矩形内,或者类似地,指向x2、y2的点位于另一个矩形内。这正是上述条件所暗示的。

        15
  •  0
  •   Anwit    11 年前

    A和B是两个矩形。c是它们的覆盖矩形。

    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) 
    

    它会处理所有可能的情况。

        16
  •  0
  •   anchan42    9 年前

    这是从练习3.28从书介绍到Java编程的综合版。代码测试两个矩形是否是缩进的,一个在另一个内部,一个在另一个外部。如果这些条件都不满足,那么这两个条件就会重叠。

    **3.28(几何:两个矩形)编写一个程序,提示用户输入 两个矩形的中心X、Y坐标、宽度和高度,并确定 第二个矩形是在第一个矩形内还是与第一个矩形重叠,如图所示 在图_3.9中。测试您的程序以覆盖所有情况。 以下是运行示例:

    输入R1的中心X、Y坐标、宽度和高度:2.5 4 2.5 43 输入R2的中心X、Y坐标、宽度和高度:1.5 5 5 0.5 3 R2在R1内部

    输入R1的中心X、Y坐标、宽度和高度:1 2 3 5.5 输入R2的中心X、Y坐标、宽度和高度:3 4 4.5 5 5 R2重叠R1

    输入R1的中心X、Y坐标、宽度和高度:1 2 3 3 输入R2的中心X、Y坐标、宽度和高度:40 45 3 2 R2不与R1重叠

    import java.util.Scanner;
    
    public class ProgrammingEx3_28 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
    
        System.out
                .print("Enter r1's center x-, y-coordinates, width, and height:");
        double x1 = input.nextDouble();
        double y1 = input.nextDouble();
        double w1 = input.nextDouble();
        double h1 = input.nextDouble();
        w1 = w1 / 2;
        h1 = h1 / 2;
        System.out
                .print("Enter r2's center x-, y-coordinates, width, and height:");
        double x2 = input.nextDouble();
        double y2 = input.nextDouble();
        double w2 = input.nextDouble();
        double h2 = input.nextDouble();
        w2 = w2 / 2;
        h2 = h2 / 2;
    
        // Calculating range of r1 and r2
        double x1max = x1 + w1;
        double y1max = y1 + h1;
        double x1min = x1 - w1;
        double y1min = y1 - h1;
        double x2max = x2 + w2;
        double y2max = y2 + h2;
        double x2min = x2 - w2;
        double y2min = y2 - h2;
    
        if (x1max == x2max && x1min == x2min && y1max == y2max
                && y1min == y2min) {
            // Check if the two are identicle
            System.out.print("r1 and r2 are indentical");
    
        } else if (x1max <= x2max && x1min >= x2min && y1max <= y2max
                && y1min >= y2min) {
            // Check if r1 is in r2
            System.out.print("r1 is inside r2");
        } else if (x2max <= x1max && x2min >= x1min && y2max <= y1max
                && y2min >= y1min) {
            // Check if r2 is in r1
            System.out.print("r2 is inside r1");
        } else if (x1max < x2min || x1min > x2max || y1max < y2min
                || y2min > y1max) {
            // Check if the two overlap
            System.out.print("r2 does not overlaps r1");
        } else {
            System.out.print("r2 overlaps r1");
        }
    
    }
    }
    
        17
  •  0
  •   khteh    8 年前
    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;
    }
    
        18
  •  0
  •   mchiasson    7 年前

    对于那些使用中心点和一半大小作为矩形数据,而不是典型的x、y、w、h或x0、y0、x1、x1的数据的用户,可以这样做:

    #include <cmath> // for fabsf(float)
    
    struct Rectangle
    {
        float centerX, centerY, halfWidth, halfHeight;
    };
    
    bool isRectangleOverlapping(const Rectangle &a, const Rectangle &b)
    {
        return (fabsf(a.centerX - b.centerX) <= (a.halfWidth + b.halfWidth)) &&
               (fabsf(a.centerY - b.centerY) <= (a.halfHeight + b.halfHeight)); 
    }
    
        19
  •  0
  •   simhumileco Janarthanan Ramu    6 年前

    从另一个网站上看这件事。

    事实证明 如果我们从另一方面看问题(算法),这就相当简单了。 .

    这意味着,与其回答问题:“矩形是否重叠?”我们将回答这个问题:“矩形是吗? 重叠?”.

    最后,两个问题解决了相同的问题,但是 第二个问题的答案更容易实现 因为 矩形不会仅在一个位于另一个之下或一个位于另一个的左侧时重叠。 (这些情况中的一种已经足够发生了,但这两种情况都可能同时发生——在这里,很好地理解逻辑条件“或”是很重要的)。这减少了许多在第一个问题上需要考虑的情况。

    整个事情也是 通过使用适当的变量名简化 :

    #include<bits/stdc++.h> 
    
    struct Rectangle
    { 
        // Coordinates of the top left corner of the rectangle and width and height
        float x, y, width, height; 
    }; 
    
    bool areRectanglesOverlap(Rectangle rect1, Rectangle rect2) 
    {
      // Declaration and initialization of local variables
    
      // if x and y are the top left corner of the rectangle
      float left1, top1, right1, bottom1, left2, top2, right2, bottom2;
      left1 = rect1.x;
      top1 = rect1.y;
      right1 = rect1.x + rect1.width;
      bottom1 = rect1.y - rect1.height;
      left2 = rect2.x;
      top2 = rect2.y;
      right2 = rect2.x + rect2.width;
      bottom2 = rect2.y - rect2.height;
    
      // The main part of the algorithm
    
      // The first rectangle is under the second or vice versa
      if (top1 < bottom2 || top2 < bottom1)
      {
        return false;
      }
      // The first rectangle is to the left of the second or vice versa
      if (right1 < left2 || right2 < left1)
      {
        return false;
      }
      // Rectangles overlap
      return true;
    }
    

    偶数 如果我们对一个矩形有不同的表示,那么通过只修改定义变量变化的部分,很容易使上面的函数适应它。 函数的进一步部分保持不变(当然,这里并不真正需要注释,但是我添加了注释,以便每个人都能快速理解这个简单的算法)。

    相等的 但可能有点不太可读 上述功能的形式 可能如下所示:

    bool areRectanglesOverlap(Rectangle rect1, Rectangle rect2) 
    {
      float left1, top1, right1, bottom1, left2, top2, right2, bottom2;
      left1 = rect1.x;
      top1 = rect1.y;
      right1 = rect1.x + rect1.width;
      bottom1 = rect1.y - rect1.height;
      left2 = rect2.x;
      top2 = rect2.y;
      right2 = rect2.x + rect2.width;
      bottom2 = rect2.y - rect2.height;
    
      return !(top1 < bottom2 || top2 < bottom1 || right1 < left2 || right2 < left1);
    }
    
        20
  •  -1
  •   Mr Jerry    12 年前

    “如果您对两个面对每个矩形的顶点执行减法X或Y坐标,如果结果是相同的符号,则两个矩形不会重叠那些轴”(对不起,我不确定我的翻译是否正确)。

    enter image description here

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

        21
  •  -1
  •   simhumileco Janarthanan Ramu    6 年前

    这个答案应该是最重要的答案:

    如果矩形重叠,则重叠区域将大于零。现在让我们找到重叠区域:

    如果它们重叠,则重叠矩形的左边缘将是 max(r1.x1, r2.x1) 右边是 min(r1.x2, r2.x2) . 所以重叠的长度是 min(r1.x2, r2.x2) - max(r1.x1, r2.x1)

    因此该区域将是:

    area = (max(r1.x1, r2.x1) - min(r1.x2, r2.x2)) * (max(r1.y1, r2.y1) - min(r1.y2, r2.y2))
    

    如果 area = 0 然后它们就不会重叠。

    很简单,不是吗?

        22
  •  -1
  •   simhumileco Janarthanan Ramu    6 年前

    Java代码,以确定矩形是否彼此接触或重叠。

    for ( int i = 0; i < n; i++ ) {
        for ( int j = 0; j < n; j++ ) {
            if ( i != j ) {
                Rectangle rectangle1 = rectangles.get(i);
                Rectangle rectangle2 = rectangles.get(j);
    
                int l1 = rectangle1.l; //left
                int r1 = rectangle1.r; //right
                int b1 = rectangle1.b; //bottom
                int t1 = rectangle1.t; //top
    
                int l2 = rectangle2.l;
                int r2 = rectangle2.r;
                int b2 = rectangle2.b;
                int t2 = rectangle2.t;
    
                boolean topOnBottom = t2 == b1;
                boolean bottomOnTop = b2 == t1;
                boolean topOrBottomContact = topOnBottom || bottomOnTop;
    
                boolean rightOnLeft = r2 == l1;
                boolean leftOnRight = l2 == r1;
                boolean rightOrLeftContact = leftOnRight || rightOnLeft;
    
                boolean leftPoll = l2 <= l1 && r2 >= l1;
                boolean rightPoll = l2 <= r1 && r2 >= r1;
                boolean leftRightInside = l2 >= l1 && r2 <= r1;
                boolean leftRightPossiblePlaces = leftPoll || rightPoll || leftRightInside;
    
                boolean bottomPoll = t2 >= b1 && b2 <= b1;
                boolean topPoll = b2 <= b1 && t2 >= b1;
                boolean topBottomInside = b2 >= b1 && t2 <= t1;
                boolean topBottomPossiblePlaces = bottomPoll || topPoll || topBottomInside;
    
    
                boolean topInBetween = t2 > b1 && t2 < t1;
                boolean bottomInBetween = b2 > b1 && b2 < t1;
                boolean topBottomInBetween = topInBetween || bottomInBetween;
    
                boolean leftInBetween = l2 > l1 && l2 < r1;
                boolean rightInBetween = r2 > l1 && r2 < r1;
                boolean leftRightInBetween = leftInBetween || rightInBetween;
    
                if ( (topOrBottomContact && leftRightPossiblePlaces) || (rightOrLeftContact && topBottomPossiblePlaces) ) {
                    path[i][j] = true;
                }
            }
        }
    }