Are two rectangles overlapping each other?

10,890

Solution 1

I think it should be

boolean isOverLap = (r1x1 < r2x2) && (r1x2 > r2x1) &&  (r1y1 < r2y2) && (r1y2 > r2y1);

(easier to read without all the negations)

You can see this as the opposite of the four cases that each on their own guarantee that an overlap cannot happen:

boolean isNonOverLap = (r1x1 >= r2x2) || (r1x2 <= r2x1) || (r1y1 >= r2y2) || (r1y2 <= r2y1);

Solution 2

Here is my solution, I tested it as well.

package small_Progs;

class Point {
    int x, y;

    Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

class Rectangle {
    Point lt, lb, rt, rb;

    Rectangle(Point lt, Point lb, Point rt, Point rb) {
        this.lt = lt;
        this.lb = lb;
        this.rt = rt;
        this.rb = rb;
    }
}

public class OverlappingRectagles {

    public static void main(String arg[]) {
        Point lt1 = new Point(3, 8);
        Point lb1 = new Point(3, 5);
        Point rt1 = new Point(6, 8);
        Point rb1 = new Point(6, 5);

        Point lt2 = new Point(5, 6);
        Point lb2 = new Point(5, 3);
        Point rt2 = new Point(9, 6);
        Point rb2 = new Point(9, 3);

        Point lt3 = new Point(3, 7);
        Point lb3 = new Point(3, 6);
        Point rt3 = new Point(5, 7);
        Point rb3 = new Point(5, 6);

        Point lt4 = new Point(1, 2);
        Point lb4 = new Point(1, 1);
        Point rt4 = new Point(2, 2);
        Point rb4 = new Point(2, 1);

        Rectangle r1 = new Rectangle(lt1, lb1, rt1, rb1);
        Rectangle r2 = new Rectangle(lt2, lb2, rt2, rb2);
        Rectangle r3 = new Rectangle(lt3, lb3, rt3, rb3);
        Rectangle r4 = new Rectangle(lt4, lb4, rt4, rb4);

        OverlappingRectagles obj = new OverlappingRectagles();
        obj.isOverLapping(r1, r2);
        obj.isOverLapping(r1, r3);
        obj.isOverLapping(r1, r4);

    }

    private void isOverLapping(Rectangle rect1, Rectangle rect2) {
        Point l1 = rect1.lt;
        Point l2 = rect2.lt;

        Point r1 = rect1.rb;
        Point r2 = rect2.rb;

        if (l1.y < l2.y || l2.y < r1.y) {
            System.out.println("Not Overlapping");
        } else if (l1.x > r2.x || l2.x > r1.x) {
            System.out.println("Not Overlapping");
        } else {
            if ((l1.y > r2.y && l2.y > r1.y) || (l2.y > r1.y && r2.y > r2.y)) {
                System.out.println("Overlapping");
            } else {
                System.out.println("Not Overlapping");
            }

        }

    }
}
Share:
10,890
George
Author by

George

Updated on June 04, 2022

Comments

  • George
    George about 2 years

    I studied this question, and followed the answer to implement my own version in Java. I think it is close... but still incorrect. Could you please give me some suggestion on the error?

    The full source code can be found here:

    // Determine if it is inside
    boolean isInside = ((r1x1 >= r2x1) && (r1x2 >= r2x2) 
            && (r1y1 >= r2y1) && (r1y2 <= r2y2));
    
    // Determine if it is overlap
    boolean isOverLap = (!(r1x1 >= r2x2) && !(r1x2 <= r2x2)
            && !(r1y2 >= r2y1) && !(r1y1 <= r2y2));
    
    // Determine if it is NOT overlap
    boolean isNotOverLap = ((r1x1 >= r2x2) || (r1x2 <= r2x2)
            || (r1y2 >= r2y1) || (r1y1 <= r2y2));
    

    According to the textbook I am studying, this is supposed to be: r2 overlap r1. But my program output r2 does not overlap r1.

    Enter the r1's center x, y coordinates, width and height
    1 2 3 5.5
    Enter the r2's center x, y coordinates, width and height
    3 4 4.5 5
    Rectangle 1: (-0.50, 4.75), (2.50, -0.75)
    Rectangle 2: (0.75, 6.50), (5.25, 1.50)
    r2 does not overlap r1
    
    • arshajii
      arshajii almost 11 years
      What's wrong with boolean isNotOverLap = !isOverLap? :P
    • Marko Topolnik
      Marko Topolnik almost 11 years
      Is this a riddle? What error?
    • Hovercraft Full Of Eels
      Hovercraft Full Of Eels almost 11 years
      Title edited: "Is you is, or is you ain't, my constituency?"
  • George
    George almost 11 years
    Thank for pointing it out, but I was asking for isOverLap and isNotOverLap...