George
George

Reputation: 4674

Are two rectangles overlapping each other?

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

Upvotes: 1

Views: 10923

Answers (4)

kamjagin
kamjagin

Reputation: 3654

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);

Upvotes: 3

abc123
abc123

Reputation: 587

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");
            }

        }

    }
}

Upvotes: 2

Bharat
Bharat

Reputation: 135

two rectagles will be overlapping .... when the respective diagonals are having the same length

for example:

R1 : (x1,y1) , (x2,y2) , (x3,y3) , (x4,y4)

R2 : (a1,b1) , (a2,b2) , (a3,b3) , (a4,b4)

so this should be true:

distance((x1,y1) , (x3,y3)) = distance((a1,b1) , (a3,b3))

and also

distance((x2,y2) , (x4,y4)) = distance((a2,b2) , (a4,b4))

Upvotes: 0

Renato Lochetti
Renato Lochetti

Reputation: 4568

The first line has an error in (r1x2 >= r2x2) :

The correct should be:

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

Upvotes: 0

Related Questions