11package com .thealgorithms .geometry ;
22
3- import org .junit .jupiter .api .Test ;
4- import java .awt .geom .Point2D ;
5- import java .util .*;
6-
73import static org .junit .jupiter .api .Assertions .*;
84
5+ import java .awt .geom .Point2D ;
6+ import java .util .ArrayList ;
7+ import java .util .List ;
8+ import java .util .Random ;
9+ import java .util .Set ;
10+ import org .junit .jupiter .api .Test ;
11+
912/**
1013 * Comprehensive unit tests for {@link BentleyOttmann}.
1114 *
@@ -20,10 +23,7 @@ public class BentleyOttmannTest {
2023
2124 @ Test
2225 void testSingleIntersection () {
23- List <Object > segments = List .of (
24- newSegment (1 , 1 , 5 , 5 ),
25- newSegment (1 , 5 , 5 , 1 )
26- );
26+ List <Object > segments = List .of (newSegment (1 , 1 , 5 , 5 ), newSegment (1 , 5 , 5 , 1 ));
2727
2828 Set <Point2D .Double > intersections = BentleyOttmann .findIntersections (cast (segments ));
2929 assertEquals (1 , intersections .size ());
@@ -32,10 +32,7 @@ void testSingleIntersection() {
3232
3333 @ Test
3434 void testVerticalIntersection () {
35- List <Object > segments = List .of (
36- newSegment (3 , 0 , 3 , 6 ),
37- newSegment (1 , 1 , 5 , 5 )
38- );
35+ List <Object > segments = List .of (newSegment (3 , 0 , 3 , 6 ), newSegment (1 , 1 , 5 , 5 ));
3936
4037 Set <Point2D .Double > intersections = BentleyOttmann .findIntersections (cast (segments ));
4138 assertEquals (1 , intersections .size ());
@@ -44,10 +41,7 @@ void testVerticalIntersection() {
4441
4542 @ Test
4643 void testNoIntersection () {
47- List <Object > segments = List .of (
48- newSegment (0 , 0 , 1 , 1 ),
49- newSegment (2 , 2 , 3 , 3 )
50- );
44+ List <Object > segments = List .of (newSegment (0 , 0 , 1 , 1 ), newSegment (2 , 2 , 3 , 3 ));
5145
5246 Set <Point2D .Double > intersections = BentleyOttmann .findIntersections (cast (segments ));
5347 assertTrue (intersections .isEmpty ());
@@ -69,10 +63,7 @@ void testCoincidentSegments() {
6963
7064 @ Test
7165 void testHorizontalIntersection () {
72- List <Object > segments = List .of (
73- newSegment (0 , 2 , 4 , 2 ),
74- newSegment (2 , 0 , 2 , 4 )
75- );
66+ List <Object > segments = List .of (newSegment (0 , 2 , 4 , 2 ), newSegment (2 , 0 , 2 , 4 ));
7667
7768 Set <Point2D .Double > intersections = BentleyOttmann .findIntersections (cast (segments ));
7869 assertTrue (containsPoint (intersections , 2.0 , 2.0 ));
@@ -87,9 +78,7 @@ void testEmptyList() {
8778
8879 @ Test
8980 void testSingleSegment () {
90- List <Object > segments = List .of (
91- newSegment (0 , 0 , 5 , 5 )
92- );
81+ List <Object > segments = List .of (newSegment (0 , 0 , 5 , 5 ));
9382 Set <Point2D .Double > intersections = BentleyOttmann .findIntersections (cast (segments ));
9483 assertTrue (intersections .isEmpty ());
9584 }
@@ -102,36 +91,21 @@ void testNullListThrowsException() {
10291 @ Test
10392 void testParallelSegments () {
10493 // Test 1: Parallel diagonal segments
105- List <Object > diagonalSegments = List .of (
106- newSegment (0 , 0 , 4 , 4 ),
107- newSegment (1 , 0 , 5 , 4 ),
108- newSegment (2 , 0 , 6 , 4 )
109- );
94+ List <Object > diagonalSegments = List .of (newSegment (0 , 0 , 4 , 4 ), newSegment (1 , 0 , 5 , 4 ), newSegment (2 , 0 , 6 , 4 ));
11095 assertTrue (BentleyOttmann .findIntersections (cast (diagonalSegments )).isEmpty ());
11196
11297 // Test 2: Parallel vertical segments
113- List <Object > verticalSegments = List .of (
114- newSegment (1 , 0 , 1 , 5 ),
115- newSegment (2 , 0 , 2 , 5 ),
116- newSegment (3 , 0 , 3 , 5 )
117- );
98+ List <Object > verticalSegments = List .of (newSegment (1 , 0 , 1 , 5 ), newSegment (2 , 0 , 2 , 5 ), newSegment (3 , 0 , 3 , 5 ));
11899 assertTrue (BentleyOttmann .findIntersections (cast (verticalSegments )).isEmpty ());
119100
120101 // Test 3: Parallel horizontal segments
121- List <Object > horizontalSegments = List .of (
122- newSegment (0 , 1 , 5 , 1 ),
123- newSegment (0 , 2 , 5 , 2 ),
124- newSegment (0 , 3 , 5 , 3 )
125- );
102+ List <Object > horizontalSegments = List .of (newSegment (0 , 1 , 5 , 1 ), newSegment (0 , 2 , 5 , 2 ), newSegment (0 , 3 , 5 , 3 ));
126103 assertTrue (BentleyOttmann .findIntersections (cast (horizontalSegments )).isEmpty ());
127104 }
128105
129106 @ Test
130107 void testTouchingEndpoints () {
131- List <Object > segments = List .of (
132- newSegment (0 , 0 , 2 , 2 ),
133- newSegment (2 , 2 , 4 , 0 )
134- );
108+ List <Object > segments = List .of (newSegment (0 , 0 , 2 , 2 ), newSegment (2 , 2 , 4 , 0 ));
135109
136110 Set <Point2D .Double > intersections = BentleyOttmann .findIntersections (cast (segments ));
137111 assertEquals (1 , intersections .size ());
@@ -140,28 +114,22 @@ void testTouchingEndpoints() {
140114
141115 @ Test
142116 void testOverlappingCollinearSegments () {
143- List <Object > segments = List .of (
144- newSegment (0 , 0 , 4 , 4 ),
145- newSegment (2 , 2 , 6 , 6 )
146- );
117+ List <Object > segments = List .of (newSegment (0 , 0 , 4 , 4 ), newSegment (2 , 2 , 6 , 6 ));
147118
148119 Set <Point2D .Double > intersections = BentleyOttmann .findIntersections (cast (segments ));
149120 // Overlapping collinear segments share the point (2,2) where second starts
150121 // and (4,4) where first ends - at least one should be detected
151122 assertFalse (intersections .isEmpty (), "Should find at least one overlap point" );
152- assertTrue (containsPoint (intersections , 2.0 , 2.0 ) ||
153- containsPoint (intersections , 4.0 , 4.0 ),
154- "Should contain either (2,2) or (4,4)" );
123+ assertTrue (containsPoint (intersections , 2.0 , 2.0 ) || containsPoint (intersections , 4.0 , 4.0 ), "Should contain either (2,2) or (4,4)" );
155124 }
156125
157126 @ Test
158127 void testMultipleSegmentsAtOnePoint () {
159128 // Star pattern: 4 segments meeting at (2, 2)
160- List <Object > segments = List .of (
161- newSegment (0 , 2 , 4 , 2 ), // horizontal
162- newSegment (2 , 0 , 2 , 4 ), // vertical
163- newSegment (0 , 0 , 4 , 4 ), // diagonal /
164- newSegment (0 , 4 , 4 , 0 ) // diagonal \
129+ List <Object > segments = List .of (newSegment (0 , 2 , 4 , 2 ), // horizontal
130+ newSegment (2 , 0 , 2 , 4 ), // vertical
131+ newSegment (0 , 0 , 4 , 4 ), // diagonal /
132+ newSegment (0 , 4 , 4 , 0 ) // diagonal \
165133 );
166134
167135 Set <Point2D .Double > intersections = BentleyOttmann .findIntersections (cast (segments ));
@@ -194,19 +162,17 @@ void testGridPattern() {
194162 // Verify all grid points are present
195163 for (int x = 0 ; x <= 2 ; x ++) {
196164 for (int y = 0 ; y <= 2 ; y ++) {
197- assertTrue (containsPoint (intersections , x , y ),
198- String .format ("Grid point (%d, %d) should be present" , x , y ));
199- }
165+ assertTrue (containsPoint (intersections , x , y ), String .format ("Grid point (%d, %d) should be present" , x , y )); }
200166 }
201167 }
202168
203169 @ Test
204170 void testTriangleIntersections () {
205171 // Three segments forming a triangle
206172 List <Object > segments = List .of (
207- newSegment (0 , 0 , 4 , 0 ), // base
208- newSegment (0 , 0 , 2 , 3 ), // left side
209- newSegment (4 , 0 , 2 , 3 ) // right side
173+ newSegment (0 , 0 , 4 , 0 ), // base
174+ newSegment (0 , 0 , 2 , 3 ), // left side
175+ newSegment (4 , 0 , 2 , 3 ) // right side
210176 );
211177
212178 Set <Point2D .Double > intersections = BentleyOttmann .findIntersections (cast (segments ));
@@ -220,12 +186,7 @@ void testTriangleIntersections() {
220186 @ Test
221187 void testCrossingDiagonals () {
222188 // X pattern with multiple crossings
223- List <Object > segments = List .of (
224- newSegment (0 , 0 , 10 , 10 ),
225- newSegment (0 , 10 , 10 , 0 ),
226- newSegment (5 , 0 , 5 , 10 ),
227- newSegment (0 , 5 , 10 , 5 )
228- );
189+ List <Object > segments = List .of (newSegment (0 , 0 , 10 , 10 ), newSegment (0 , 10 , 10 , 0 ), newSegment (5 , 0 , 5 , 10 ), newSegment (0 , 5 , 10 , 5 ));
229190
230191 Set <Point2D .Double > intersections = BentleyOttmann .findIntersections (cast (segments ));
231192 assertTrue (containsPoint (intersections , 5.0 , 5.0 ), "Center point should be present" );
@@ -236,10 +197,7 @@ void testCrossingDiagonals() {
236197
237198 @ Test
238199 void testVerySmallSegments () {
239- List <Object > segments = List .of (
240- newSegment (0.001 , 0.001 , 0.002 , 0.002 ),
241- newSegment (0.001 , 0.002 , 0.002 , 0.001 )
242- );
200+ List <Object > segments = List .of (newSegment (0.001 , 0.001 , 0.002 , 0.002 ), newSegment (0.001 , 0.002 , 0.002 , 0.001 ));
243201
244202 Set <Point2D .Double > intersections = BentleyOttmann .findIntersections (cast (segments ));
245203 assertEquals (1 , intersections .size ());
@@ -248,19 +206,11 @@ void testVerySmallSegments() {
248206
249207 @ Test
250208 void testSegmentsShareCommonPoint () {
251- List <Object > segmentsSameStart = List .of (
252- newSegment (0 , 0 , 4 , 4 ),
253- newSegment (0 , 0 , 4 , -4 ),
254- newSegment (0 , 0 , -4 , 4 )
255- );
209+ List <Object > segmentsSameStart = List .of (newSegment (0 , 0 , 4 , 4 ), newSegment (0 , 0 , 4 , -4 ), newSegment (0 , 0 , -4 , 4 ));
256210
257211 Set <Point2D .Double > intersectionsSameStart = BentleyOttmann .findIntersections (cast (segmentsSameStart ));
258212 assertTrue (containsPoint (intersectionsSameStart , 0.0 , 0.0 ));
259- List <Object > segmentsSameEnd = List .of (
260- newSegment (0 , 0 , 4 , 4 ),
261- newSegment (8 , 4 , 4 , 4 ),
262- newSegment (4 , 8 , 4 , 4 )
263- );
213+ List <Object > segmentsSameEnd = List .of (newSegment (0 , 0 , 4 , 4 ), newSegment (8 , 4 , 4 , 4 ), newSegment (4 , 8 , 4 , 4 ));
264214
265215 Set <Point2D .Double > intersectionsSameEnd = BentleyOttmann .findIntersections (cast (segmentsSameEnd ));
266216 assertTrue (containsPoint (intersectionsSameEnd , 4.0 , 4.0 ));
@@ -270,10 +220,10 @@ void testSegmentsShareCommonPoint() {
270220 void testSegmentsAtAngles () {
271221 // Segments at 45, 90, 135 degrees
272222 List <Object > segments = List .of (
273- newSegment (0 , 2 , 4 , 2 ), // horizontal
274- newSegment (2 , 0 , 2 , 4 ), // vertical
275- newSegment (0 , 0 , 4 , 4 ), // 45 degrees
276- newSegment (0 , 4 , 4 , 0 ) // 135 degrees
223+ newSegment (0 , 2 , 4 , 2 ), // horizontal
224+ newSegment (2 , 0 , 2 , 4 ), // vertical
225+ newSegment (0 , 0 , 4 , 4 ), // 45 degrees
226+ newSegment (0 , 4 , 4 , 0 ) // 135 degrees
277227 );
278228
279229 Set <Point2D .Double > intersections = BentleyOttmann .findIntersections (cast (segments ));
@@ -301,30 +251,27 @@ void testPerformanceWithManySegments() {
301251 long duration = endTime - startTime ;
302252
303253 // Should complete in reasonable time (< 1 second for 100 segments)
304- assertTrue (duration < 1000 ,
305- "Algorithm should complete in less than 1 second for 100 segments. Took: " + duration + "ms" );
254+ assertTrue (duration < 1000 , "Algorithm should complete in less than 1 second for 100 segments. Took: " + duration + "ms" );
306255
307256 // Just verify it returns a valid result
308257 assertNotNull (intersections );
309- System .out .println ("Performance test: 100 segments processed in " + duration +
310- "ms, found " + intersections .size () + " intersections" );
258+ System .out .println ("Performance test: 100 segments processed in " + duration + "ms, found " + intersections .size () + " intersections" );
311259 }
312260
313261 @ Test
314262 void testIssueExample () {
315263 // Example from the GitHub issue
316264 List <Object > segments = List .of (
317- newSegment (1 , 1 , 5 , 5 ), // Segment A
318- newSegment (1 , 5 , 5 , 1 ), // Segment B
319- newSegment (3 , 0 , 3 , 6 ) // Segment C
265+ newSegment (1 , 1 , 5 , 5 ), // Segment A
266+ newSegment (1 , 5 , 5 , 1 ), // Segment B
267+ newSegment (3 , 0 , 3 , 6 ) // Segment C
320268 );
321269
322270 Set <Point2D .Double > intersections = BentleyOttmann .findIntersections (cast (segments ));
323271
324272 // Expected output: [(3, 3)]
325273 assertEquals (1 , intersections .size (), "Should find exactly one intersection" );
326- assertTrue (containsPoint (intersections , 3.0 , 3.0 ),
327- "Intersection should be at (3, 3)" );
274+ assertTrue (containsPoint (intersections , 3.0 , 3.0 ), "Intersection should be at (3, 3)" );
328275 }
329276
330277 private static Object newSegment (double x1 , double y1 , double x2 , double y2 ) {
@@ -340,4 +287,4 @@ private static List<BentleyOttmann.Segment> cast(List<Object> objs) {
340287 private static boolean containsPoint (Set <Point2D .Double > points , double x , double y ) {
341288 return points .stream ().anyMatch (p -> Math .abs (p .x - x ) < EPS && Math .abs (p .y - y ) < EPS );
342289 }
343- }
290+ }
0 commit comments