|
| 1 | +package com.thealgorithms.geometry; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 6 | + |
| 7 | +import java.awt.geom.Point2D; |
| 8 | +import java.util.Optional; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +class LineIntersectionTest { |
| 12 | + |
| 13 | + @Test |
| 14 | + void testCrossingSegments() { |
| 15 | + Point p1 = new Point(0, 0); |
| 16 | + Point p2 = new Point(4, 4); |
| 17 | + Point q1 = new Point(0, 4); |
| 18 | + Point q2 = new Point(4, 0); |
| 19 | + |
| 20 | + assertTrue(LineIntersection.intersects(p1, p2, q1, q2)); |
| 21 | + Optional<Point2D.Double> intersection = LineIntersection.intersectionPoint(p1, p2, q1, q2); |
| 22 | + assertTrue(intersection.isPresent()); |
| 23 | + assertEquals(2.0, intersection.orElseThrow().getX(), 1e-9); |
| 24 | + assertEquals(2.0, intersection.orElseThrow().getY(), 1e-9); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + void testParallelSegments() { |
| 29 | + Point p1 = new Point(0, 0); |
| 30 | + Point p2 = new Point(3, 3); |
| 31 | + Point q1 = new Point(0, 1); |
| 32 | + Point q2 = new Point(3, 4); |
| 33 | + |
| 34 | + assertFalse(LineIntersection.intersects(p1, p2, q1, q2)); |
| 35 | + assertTrue(LineIntersection.intersectionPoint(p1, p2, q1, q2).isEmpty()); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + void testTouchingAtEndpoint() { |
| 40 | + Point p1 = new Point(0, 0); |
| 41 | + Point p2 = new Point(2, 2); |
| 42 | + Point q1 = new Point(2, 2); |
| 43 | + Point q2 = new Point(4, 0); |
| 44 | + |
| 45 | + assertTrue(LineIntersection.intersects(p1, p2, q1, q2)); |
| 46 | + Optional<Point2D.Double> intersection = LineIntersection.intersectionPoint(p1, p2, q1, q2); |
| 47 | + assertTrue(intersection.isPresent()); |
| 48 | + assertEquals(2.0, intersection.orElseThrow().getX(), 1e-9); |
| 49 | + assertEquals(2.0, intersection.orElseThrow().getY(), 1e-9); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void testCollinearOverlapHasNoUniquePoint() { |
| 54 | + Point p1 = new Point(0, 0); |
| 55 | + Point p2 = new Point(4, 4); |
| 56 | + Point q1 = new Point(2, 2); |
| 57 | + Point q2 = new Point(6, 6); |
| 58 | + |
| 59 | + assertTrue(LineIntersection.intersects(p1, p2, q1, q2)); |
| 60 | + assertTrue(LineIntersection.intersectionPoint(p1, p2, q1, q2).isEmpty()); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void testCollinearDisjointSegments() { |
| 65 | + Point p1 = new Point(0, 0); |
| 66 | + Point p2 = new Point(2, 2); |
| 67 | + Point q1 = new Point(3, 3); |
| 68 | + Point q2 = new Point(5, 5); |
| 69 | + |
| 70 | + assertFalse(LineIntersection.intersects(p1, p2, q1, q2)); |
| 71 | + assertTrue(LineIntersection.intersectionPoint(p1, p2, q1, q2).isEmpty()); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void testCollinearSegmentsTouchingAtEndpointHaveUniquePoint() { |
| 76 | + Point p1 = new Point(0, 0); |
| 77 | + Point p2 = new Point(2, 2); |
| 78 | + Point q1 = new Point(2, 2); |
| 79 | + Point q2 = new Point(4, 4); |
| 80 | + |
| 81 | + assertTrue(LineIntersection.intersects(p1, p2, q1, q2)); |
| 82 | + Optional<Point2D.Double> intersection = LineIntersection.intersectionPoint(p1, p2, q1, q2); |
| 83 | + assertTrue(intersection.isPresent()); |
| 84 | + assertEquals(2.0, intersection.orElseThrow().getX(), 1e-9); |
| 85 | + assertEquals(2.0, intersection.orElseThrow().getY(), 1e-9); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void testVerticalAndHorizontalCrossingSegments() { |
| 90 | + Point p1 = new Point(2, 0); |
| 91 | + Point p2 = new Point(2, 5); |
| 92 | + Point q1 = new Point(0, 3); |
| 93 | + Point q2 = new Point(4, 3); |
| 94 | + |
| 95 | + assertTrue(LineIntersection.intersects(p1, p2, q1, q2)); |
| 96 | + Optional<Point2D.Double> intersection = LineIntersection.intersectionPoint(p1, p2, q1, q2); |
| 97 | + assertTrue(intersection.isPresent()); |
| 98 | + assertEquals(2.0, intersection.orElseThrow().getX(), 1e-9); |
| 99 | + assertEquals(3.0, intersection.orElseThrow().getY(), 1e-9); |
| 100 | + } |
| 101 | +} |
0 commit comments