diff --git a/core-java/src/main/java/com/baeldung/linesintersection/LinesIntersectionService.java b/core-java/src/main/java/com/baeldung/linesintersection/LinesIntersectionService.java index e4fed5a22e..a2cce53c8f 100644 --- a/core-java/src/main/java/com/baeldung/linesintersection/LinesIntersectionService.java +++ b/core-java/src/main/java/com/baeldung/linesintersection/LinesIntersectionService.java @@ -5,17 +5,17 @@ import java.util.Optional; public class LinesIntersectionService { - public Optional calculateIntersectionPoint(float m1, float b1, float m2, float b2) { + public Optional calculateIntersectionPoint(double m1, double b1, double m2, double b2) { if (m1 == m2) { return Optional.empty(); } - - float x = (b2 - b1) / (m1 - m2); - float y = m1 * x + b1; - Point point = new Point(Math.round(x), Math.round(y)); + double x = (b2 - b1) / (m1 - m2); + double y = m1 * x + b1; + Point point = new Point(); + point.setLocation(x, y); return Optional.of(point); } } diff --git a/core-java/src/test/java/com/baeldung/linesintersection/LinesIntersectionServiceUnitTest.java b/core-java/src/test/java/com/baeldung/linesintersection/LinesIntersectionServiceUnitTest.java index 90c93fe050..47f1fd45a1 100644 --- a/core-java/src/test/java/com/baeldung/linesintersection/LinesIntersectionServiceUnitTest.java +++ b/core-java/src/test/java/com/baeldung/linesintersection/LinesIntersectionServiceUnitTest.java @@ -14,24 +14,24 @@ public class LinesIntersectionServiceUnitTest { @Test public void givenNotParallelLines_whenCalculatePoint_thenPresent() { - float m1 = 0; - float b1 = 0; - float m2 = 1; - float b2 = -1; + double m1 = 0; + double b1 = 0; + double m2 = 1; + double b2 = -1; Optional point = service.calculateIntersectionPoint(m1, b1, m2, b2); assertTrue(point.isPresent()); - assertEquals(point.get().x, 1); - assertEquals(point.get().y, 0); + assertEquals(point.get().getX(), 1, 0.001); + assertEquals(point.get().getX(), 1, 0.001); } @Test public void givenParallelLines_whenCalculatePoint_thenEmpty() { - float m1 = 1; - float b1 = 0; - float m2 = 1; - float b2 = -1; + double m1 = 1; + double b1 = 0; + double m2 = 1; + double b2 = -1; Optional point = service.calculateIntersectionPoint(m1, b1, m2, b2);