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