From 79e9818bee9d402d6815f7e97643702a7e6e7dd2 Mon Sep 17 00:00:00 2001 From: Harry9656 Date: Thu, 23 Nov 2023 06:21:19 +0100 Subject: [PATCH] JAVA-26530: Update the RectangleOverlap adding a method without borders check. --- .../rectanglesoverlap/Rectangle.java | 22 ++++++++++++++----- .../rectanglesoverlap/RectangleUnitTest.java | 17 +++++++++++++- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/core-java-modules/core-java-lang-math-2/src/main/java/com/baeldung/algorithms/rectanglesoverlap/Rectangle.java b/core-java-modules/core-java-lang-math-2/src/main/java/com/baeldung/algorithms/rectanglesoverlap/Rectangle.java index 38f5edec61..ee43af3eff 100644 --- a/core-java-modules/core-java-lang-math-2/src/main/java/com/baeldung/algorithms/rectanglesoverlap/Rectangle.java +++ b/core-java-modules/core-java-lang-math-2/src/main/java/com/baeldung/algorithms/rectanglesoverlap/Rectangle.java @@ -26,13 +26,25 @@ public class Rectangle { this.topRight = topRight; } - public boolean isOverlapping(Rectangle other) { - // one rectangle is to the top of the other - if (this.topRight.getY() < other.bottomLeft.getY() || this.bottomLeft.getY() > other.topRight.getY()) { + public boolean isOverlapping(Rectangle comparedRectangle) { + // one rectangle is to the top of the comparedRectangle + if (this.topRight.getY() < comparedRectangle.bottomLeft.getY() || this.bottomLeft.getY() > comparedRectangle.topRight.getY()) { return false; } - // one rectangle is to the left of the other - if (this.topRight.getX() < other.bottomLeft.getX() || this.bottomLeft.getX() > other.topRight.getX()) { + // one rectangle is to the left of the comparedRectangle + if (this.topRight.getX() < comparedRectangle.bottomLeft.getX() || this.bottomLeft.getX() > comparedRectangle.topRight.getX()) { + return false; + } + return true; + } + + public boolean isOverlappingWithoutBorders(Rectangle comparedRectangle) { + // one rectangle is to the top of the comparedRectangle + if (this.topRight.getY() <= comparedRectangle.bottomLeft.getY() || this.bottomLeft.getY() >= comparedRectangle.topRight.getY()) { + return false; + } + // one rectangle is to the left of the comparedRectangle + if (this.topRight.getX() <= comparedRectangle.bottomLeft.getX() || this.bottomLeft.getX() >= comparedRectangle.topRight.getX()) { return false; } return true; diff --git a/core-java-modules/core-java-lang-math-2/src/test/java/com/baeldung/algorithms/rectanglesoverlap/RectangleUnitTest.java b/core-java-modules/core-java-lang-math-2/src/test/java/com/baeldung/algorithms/rectanglesoverlap/RectangleUnitTest.java index e4bb614b48..18186b43b1 100644 --- a/core-java-modules/core-java-lang-math-2/src/test/java/com/baeldung/algorithms/rectanglesoverlap/RectangleUnitTest.java +++ b/core-java-modules/core-java-lang-math-2/src/test/java/com/baeldung/algorithms/rectanglesoverlap/RectangleUnitTest.java @@ -1,7 +1,8 @@ package com.baeldung.algorithms.rectanglesoverlap; -import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + import org.junit.Test; public class RectangleUnitTest { @@ -36,4 +37,18 @@ public class RectangleUnitTest { assertFalse(rectangle1.isOverlapping(rectangle2)); } + @Test + public void givenAdjacentRectangles_whensOverlappingCalled_shouldReturnTrue() { +Rectangle rectangle1 = new Rectangle(new Point(0, 0), new Point(5, 14)); +Rectangle rectangle2 = new Rectangle(new Point(5, 0), new Point(17, 14)); +assertTrue(rectangle1.isOverlapping(rectangle2)); + } + + @Test + public void givenAdjacentRectangles_whensOverlappingWithoutBordersCalled_shouldReturnFalse() { + Rectangle rectangle1 = new Rectangle(new Point(0, 0), new Point(5, 14)); + Rectangle rectangle2 = new Rectangle(new Point(5, 0), new Point(17, 14)); + assertFalse(rectangle1.isOverlappingWithoutBorders(rectangle2)); + } + }