Skip to content

Commit 12124f3

Browse files
shubhi22maibin
authored andcommitted
BAEL-2153 : Check If Rectangles Overlap In Java (eugenp#5200)
* BAEL-2153 : Check If Rectangles Overlap In Java * BAEL-2153 : Moving to Algorithms repo
1 parent 0469d6f commit 12124f3

3 files changed

Lines changed: 111 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.algorithms.rectanglesoverlap;
2+
3+
public class Point {
4+
5+
private int x;
6+
private int y;
7+
8+
public Point(int x, int y) {
9+
this.x = x;
10+
this.y = y;
11+
}
12+
13+
public int getX() {
14+
return x;
15+
}
16+
17+
public void setX(int x) {
18+
this.x = x;
19+
}
20+
21+
public int getY() {
22+
return y;
23+
}
24+
25+
public void setY(int y) {
26+
this.y = y;
27+
}
28+
29+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.algorithms.rectanglesoverlap;
2+
3+
public class Rectangle {
4+
5+
private Point bottomLeft;
6+
private Point topRight;
7+
8+
public Rectangle(Point bottomLeft, Point topRight) {
9+
this.bottomLeft = bottomLeft;
10+
this.topRight = topRight;
11+
}
12+
13+
public Point getBottomLeft() {
14+
return bottomLeft;
15+
}
16+
17+
public void setBottomLeft(Point bottomLeft) {
18+
this.bottomLeft = bottomLeft;
19+
}
20+
21+
public Point getTopRight() {
22+
return topRight;
23+
}
24+
25+
public void setTopRight(Point topRight) {
26+
this.topRight = topRight;
27+
}
28+
29+
public boolean isOverlapping(Rectangle other) {
30+
// one rectangle is to the top of the other
31+
if (this.topRight.getY() < other.bottomLeft.getY() || this.bottomLeft.getY() > other.topRight.getY()) {
32+
return false;
33+
}
34+
// one rectangle is to the left of the other
35+
if (this.topRight.getX() < other.bottomLeft.getX() || this.bottomLeft.getX() > other.topRight.getX()) {
36+
return false;
37+
}
38+
return true;
39+
}
40+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.algorithms.rectanglesoverlap;
2+
3+
import static org.junit.Assert.assertTrue;
4+
import static org.junit.Assert.assertFalse;
5+
import org.junit.Test;
6+
7+
import com.baeldung.algorithms.rectanglesoverlap.Point;
8+
import com.baeldung.algorithms.rectanglesoverlap.Rectangle;
9+
10+
public class RectangleUnitTest {
11+
12+
@Test
13+
public void givenTwoOverlappingRectangles_whenisOverlappingCalled_shouldReturnTrue() {
14+
Rectangle rectangle1 = new Rectangle(new Point(2, 1), new Point(4, 3));
15+
Rectangle rectangle2 = new Rectangle(new Point(1, 1), new Point(6, 4));
16+
assertTrue(rectangle1.isOverlapping(rectangle2));
17+
18+
rectangle1 = new Rectangle(new Point(-5, -2), new Point(2, 3));
19+
rectangle2 = new Rectangle(new Point(-2, -1), new Point(5, 2));
20+
assertTrue(rectangle1.isOverlapping(rectangle2));
21+
22+
rectangle1 = new Rectangle(new Point(-5, 1), new Point(2, 4));
23+
rectangle2 = new Rectangle(new Point(-2, -2), new Point(5, 5));
24+
assertTrue(rectangle1.isOverlapping(rectangle2));
25+
}
26+
27+
@Test
28+
public void givenTwoNonOverlappingRectangles_whenisOverlappingCalled_shouldReturnFalse() {
29+
Rectangle rectangle1 = new Rectangle(new Point(-5, 1), new Point(-3, 4));
30+
Rectangle rectangle2 = new Rectangle(new Point(-2, -2), new Point(5, 5));
31+
assertFalse(rectangle1.isOverlapping(rectangle2));
32+
33+
rectangle1 = new Rectangle(new Point(-5, 1), new Point(3, 4));
34+
rectangle2 = new Rectangle(new Point(-2, -2), new Point(5, -1));
35+
assertFalse(rectangle1.isOverlapping(rectangle2));
36+
37+
rectangle1 = new Rectangle(new Point(-2, 1), new Point(0, 3));
38+
rectangle2 = new Rectangle(new Point(3, 1), new Point(5, 4));
39+
assertFalse(rectangle1.isOverlapping(rectangle2));
40+
}
41+
42+
}

0 commit comments

Comments
 (0)