Skip to content

Commit adc3197

Browse files
committed
style(geometry): fix code style
1 parent dd086bd commit adc3197

2 files changed

Lines changed: 51 additions & 119 deletions

File tree

src/main/java/com/thealgorithms/geometry/BentleyOttmann.java

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
*/
1616
public final class BentleyOttmann {
1717

18-
private BentleyOttmann() {}
18+
private BentleyOttmann() {
19+
}
1920

2021
private static final double EPS = 1e-9;
2122
private static double currentSweepX;
@@ -65,9 +66,7 @@ public String toString() {
6566
/**
6667
* Event types for the sweep line algorithm.
6768
*/
68-
private enum EventType {
69-
START, END, INTERSECTION
70-
}
69+
private enum EventType { START, END, INTERSECTION }
7170

7271
/**
7372
* Represents an event in the event queue.
@@ -186,8 +185,7 @@ public static Set<Point2D.Double> findIntersections(List<Segment> segments) {
186185
return intersections;
187186
}
188187

189-
private static Event getOrCreateEvent(Map<Point2D.Double, Event> eventMap,
190-
Point2D.Double point, EventType type) {
188+
private static Event getOrCreateEvent(Map<Point2D.Double, Event> eventMap, Point2D.Double point, EventType type) {
191189
// Find existing event at this point
192190
for (Map.Entry<Point2D.Double, Event> entry : eventMap.entrySet()) {
193191
if (pointsEqual(entry.getKey(), point)) {
@@ -200,17 +198,13 @@ private static Event getOrCreateEvent(Map<Point2D.Double, Event> eventMap,
200198
return event;
201199
}
202200

203-
private static void handleEvent(Event event, TreeSet<Segment> status,
204-
PriorityQueue<Event> eventQueue,
205-
Map<Point2D.Double, Event> eventMap,
206-
Set<Point2D.Double> intersections) {
201+
private static void handleEvent(Event event, TreeSet<Segment> status, PriorityQueue<Event> eventQueue, Map<Point2D.Double, Event> eventMap, Set<Point2D.Double> intersections) {
207202
Point2D.Double p = event.point;
208203
Set<Segment> segmentsAtPoint = new HashSet<>(event.segments);
209204

210205
// Check segments in status structure (much smaller than allSegments)
211206
for (Segment s : status) {
212-
if (pointsEqual(s.p1, p) || pointsEqual(s.p2, p) ||
213-
(onSegment(s, p) && !pointsEqual(s.p1, p) && !pointsEqual(s.p2, p))) {
207+
if (pointsEqual(s.p1, p) || pointsEqual(s.p2, p) || (onSegment(s, p) && !pointsEqual(s.p1, p) && !pointsEqual(s.p2, p))) {
214208
segmentsAtPoint.add(s);
215209
}
216210
}
@@ -302,14 +296,10 @@ private static Segment getRightmost(Set<Segment> segments, TreeSet<Segment> stat
302296
return rightmost;
303297
}
304298

305-
private static void findNewEvent(Segment s1, Segment s2, Point2D.Double currentPoint,
306-
PriorityQueue<Event> eventQueue,
307-
Map<Point2D.Double, Event> eventMap) {
299+
private static void findNewEvent(Segment s1, Segment s2, Point2D.Double currentPoint, PriorityQueue<Event> eventQueue, Map<Point2D.Double, Event> eventMap) {
308300
Point2D.Double intersection = getIntersection(s1, s2);
309301

310-
if (intersection != null &&
311-
intersection.x > currentPoint.x - EPS &&
312-
!pointsEqual(intersection, currentPoint)) {
302+
if (intersection != null && intersection.x > currentPoint.x - EPS && !pointsEqual(intersection, currentPoint)) {
313303

314304
// Check if event already exists
315305
boolean exists = false;
@@ -395,15 +385,10 @@ private static double crossProduct(Point2D.Double a, Point2D.Double b, Point2D.D
395385
}
396386

397387
private static boolean onSegment(Segment s, Point2D.Double p) {
398-
return p.x >= Math.min(s.p1.x, s.p2.x) - EPS &&
399-
p.x <= Math.max(s.p1.x, s.p2.x) + EPS &&
400-
p.y >= Math.min(s.p1.y, s.p2.y) - EPS &&
401-
p.y <= Math.max(s.p1.y, s.p2.y) + EPS &&
402-
Math.abs(crossProduct(s.p1, s.p2, p)) < EPS;
388+
return p.x >= Math.min(s.p1.x, s.p2.x) - EPS && p.x <= Math.max(s.p1.x, s.p2.x) + EPS && p.y >= Math.min(s.p1.y, s.p2.y) - EPS && p.y <= Math.max(s.p1.y, s.p2.y) + EPS && Math.abs(crossProduct(s.p1, s.p2, p)) < EPS;
403389
}
404390

405391
private static boolean pointsEqual(Point2D.Double p1, Point2D.Double p2) {
406392
return Math.abs(p1.x - p2.x) < EPS && Math.abs(p1.y - p2.y) < EPS;
407393
}
408-
409-
}
394+
}

src/test/java/com/thealgorithms/geometry/BentleyOttmannTest.java

Lines changed: 41 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package com.thealgorithms.geometry;
22

3-
import org.junit.jupiter.api.Test;
4-
import java.awt.geom.Point2D;
5-
import java.util.*;
6-
73
import 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

Comments
 (0)