Skip to content

Commit 47b36e4

Browse files
committed
fix(geometry): Resolve SpotBugs and PMD static analysis warnings
1 parent 29e5224 commit 47b36e4

File tree

2 files changed

+48
-52
lines changed

2 files changed

+48
-52
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ public static class Segment {
6161
}
6262

6363
Point2D.Double leftPoint() {
64-
return p1.x < p2.x ? p1 : (p1.x > p2.x ? p2 : (p1.y < p2.y ? p1 : p2));
64+
return p1.x < p2.x ? p1 : p1.x > p2.x ? p2 : p1.y < p2.y ? p1 : p2;
6565
}
6666

6767
Point2D.Double rightPoint() {
68-
return p1.x > p2.x ? p1 : (p1.x < p2.x ? p2 : (p1.y > p2.y ? p1 : p2));
68+
return p1.x > p2.x ? p1 : p1.x < p2.x ? p2 : p1.y > p2.y ? p1 : p2;
6969
}
7070

7171
@Override

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

Lines changed: 46 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package com.thealgorithms.geometry;
22

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.assertNotNull;
6-
import static org.junit.jupiter.api.Assertions.assertThrows;
7-
import static org.junit.jupiter.api.Assertions.assertTrue;
3+
import org.junit.jupiter.api.Assertions;
84

95
import java.awt.geom.Point2D;
106
import java.util.ArrayList;
@@ -30,25 +26,25 @@ void testSingleIntersection() {
3026
List<BentleyOttmann.Segment> segments = List.of(newSegment(1, 1, 5, 5), newSegment(1, 5, 5, 1));
3127

3228
Set<Point2D.Double> intersections = BentleyOttmann.findIntersections(segments);
33-
assertEquals(1, intersections.size());
34-
assertTrue(containsPoint(intersections, 3.0, 3.0));
29+
Assertions.assertEquals(1, intersections.size());
30+
Assertions.assertTrue(containsPoint(intersections, 3.0, 3.0));
3531
}
3632

3733
@Test
3834
void testVerticalIntersection() {
3935
List<BentleyOttmann.Segment> segments = List.of(newSegment(3, 0, 3, 6), newSegment(1, 1, 5, 5));
4036

4137
Set<Point2D.Double> intersections = BentleyOttmann.findIntersections(segments);
42-
assertEquals(1, intersections.size());
43-
assertTrue(containsPoint(intersections, 3.0, 3.0));
38+
Assertions.assertEquals(1, intersections.size());
39+
Assertions.assertTrue(containsPoint(intersections, 3.0, 3.0));
4440
}
4541

4642
@Test
4743
void testNoIntersection() {
4844
List<BentleyOttmann.Segment> segments = List.of(newSegment(0, 0, 1, 1), newSegment(2, 2, 3, 3));
4945

5046
Set<Point2D.Double> intersections = BentleyOttmann.findIntersections(segments);
51-
assertTrue(intersections.isEmpty());
47+
Assertions.assertTrue(intersections.isEmpty());
5248
}
5349

5450
@Test
@@ -57,60 +53,60 @@ void testCoincidentSegments() {
5753

5854
Set<Point2D.Double> intersections = BentleyOttmann.findIntersections(segments);
5955

60-
assertEquals(2, intersections.size(), "Two identical segments should report 2 intersection points (both endpoints)");
61-
assertTrue(containsPoint(intersections, 1.0, 1.0));
62-
assertTrue(containsPoint(intersections, 5.0, 5.0));
56+
Assertions.assertEquals(2, intersections.size(), "Two identical segments should report 2 intersection points (both endpoints)");
57+
Assertions.assertTrue(containsPoint(intersections, 1.0, 1.0));
58+
Assertions.assertTrue(containsPoint(intersections, 5.0, 5.0));
6359
}
6460

6561
@Test
6662
void testHorizontalIntersection() {
6763
List<BentleyOttmann.Segment> segments = List.of(newSegment(0, 2, 4, 2), newSegment(2, 0, 2, 4));
6864

6965
Set<Point2D.Double> intersections = BentleyOttmann.findIntersections(segments);
70-
assertTrue(containsPoint(intersections, 2.0, 2.0));
66+
Assertions.assertTrue(containsPoint(intersections, 2.0, 2.0));
7167
}
7268

7369
@Test
7470
void testEmptyList() {
7571
List<BentleyOttmann.Segment> segments = List.of();
7672
Set<Point2D.Double> intersections = BentleyOttmann.findIntersections(segments);
77-
assertTrue(intersections.isEmpty());
73+
Assertions.assertTrue(intersections.isEmpty());
7874
}
7975

8076
@Test
8177
void testSingleSegment() {
8278
List<BentleyOttmann.Segment> segments = List.of(newSegment(0, 0, 5, 5));
8379
Set<Point2D.Double> intersections = BentleyOttmann.findIntersections(segments);
84-
assertTrue(intersections.isEmpty());
80+
Assertions.assertTrue(intersections.isEmpty());
8581
}
8682

8783
@Test
8884
void testNullListThrowsException() {
89-
assertThrows(IllegalArgumentException.class, () -> BentleyOttmann.findIntersections(null));
85+
Assertions.assertThrows(IllegalArgumentException.class, () -> BentleyOttmann.findIntersections(null));
9086
}
9187

9288
@Test
9389
void testParallelSegments() {
9490
// Test 1: Parallel diagonal segments
9591
List<BentleyOttmann.Segment> diagonalSegments = List.of(newSegment(0, 0, 4, 4), newSegment(1, 0, 5, 4), newSegment(2, 0, 6, 4));
96-
assertTrue(BentleyOttmann.findIntersections(diagonalSegments).isEmpty());
92+
Assertions.assertTrue(BentleyOttmann.findIntersections(diagonalSegments).isEmpty());
9793

9894
// Test 2: Parallel vertical segments
9995
List<BentleyOttmann.Segment> verticalSegments = List.of(newSegment(1, 0, 1, 5), newSegment(2, 0, 2, 5), newSegment(3, 0, 3, 5));
100-
assertTrue(BentleyOttmann.findIntersections(verticalSegments).isEmpty());
96+
Assertions.assertTrue(BentleyOttmann.findIntersections(verticalSegments).isEmpty());
10197

10298
// Test 3: Parallel horizontal segments
10399
List<BentleyOttmann.Segment> horizontalSegments = List.of(newSegment(0, 1, 5, 1), newSegment(0, 2, 5, 2), newSegment(0, 3, 5, 3));
104-
assertTrue(BentleyOttmann.findIntersections(horizontalSegments).isEmpty());
100+
Assertions.assertTrue(BentleyOttmann.findIntersections(horizontalSegments).isEmpty());
105101
}
106102

107103
@Test
108104
void testTouchingEndpoints() {
109105
List<BentleyOttmann.Segment> segments = List.of(newSegment(0, 0, 2, 2), newSegment(2, 2, 4, 0));
110106

111107
Set<Point2D.Double> intersections = BentleyOttmann.findIntersections(segments);
112-
assertEquals(1, intersections.size());
113-
assertTrue(containsPoint(intersections, 2.0, 2.0));
108+
Assertions.assertEquals(1, intersections.size());
109+
Assertions.assertTrue(containsPoint(intersections, 2.0, 2.0));
114110
}
115111

116112
@Test
@@ -120,8 +116,8 @@ void testOverlappingCollinearSegments() {
120116
Set<Point2D.Double> intersections = BentleyOttmann.findIntersections(segments);
121117
// Overlapping collinear segments share the point (2,2) where second starts
122118
// and (4,4) where first ends - at least one should be detected
123-
assertFalse(intersections.isEmpty(), "Should find at least one overlap point");
124-
assertTrue(containsPoint(intersections, 2.0, 2.0) || containsPoint(intersections, 4.0, 4.0), "Should contain either (2,2) or (4,4)");
119+
Assertions.assertFalse(intersections.isEmpty(), "Should find at least one overlap point");
120+
Assertions.assertTrue(containsPoint(intersections, 2.0, 2.0) || containsPoint(intersections, 4.0, 4.0), "Should contain either (2,2) or (4,4)");
125121
}
126122

127123
@Test
@@ -134,9 +130,9 @@ void testMultipleSegmentsAtOnePoint() {
134130
);
135131

136132
Set<Point2D.Double> intersections = BentleyOttmann.findIntersections(segments);
137-
assertTrue(containsPoint(intersections, 2.0, 2.0));
133+
Assertions.assertTrue(containsPoint(intersections, 2.0, 2.0));
138134
// All segments meet at (2, 2), so should be reported once
139-
assertEquals(1, intersections.size());
135+
Assertions.assertEquals(1, intersections.size());
140136
}
141137

142138
@Test
@@ -158,12 +154,12 @@ void testGridPattern() {
158154

159155
// Each vertical line crosses each horizontal line
160156
// 3 vertical × 3 horizontal = 9 intersections
161-
assertEquals(9, intersections.size(), "3x3 grid should have 9 intersections");
157+
Assertions.assertEquals(9, intersections.size(), "3x3 grid should have 9 intersections");
162158

163159
// Verify all grid points are present
164160
for (int x = 0; x <= 2; x++) {
165161
for (int y = 0; y <= 2; y++) {
166-
assertTrue(containsPoint(intersections, x, y), String.format("Grid point (%d, %d) should be present", x, y));
162+
Assertions.assertTrue(containsPoint(intersections, x, y), String.format("Grid point (%d, %d) should be present", x, y));
167163
}
168164
}
169165
}
@@ -178,10 +174,10 @@ void testTriangleIntersections() {
178174

179175
Set<Point2D.Double> intersections = BentleyOttmann.findIntersections(segments);
180176
// Triangle vertices are intersections
181-
assertTrue(containsPoint(intersections, 0.0, 0.0));
182-
assertTrue(containsPoint(intersections, 4.0, 0.0));
183-
assertTrue(containsPoint(intersections, 2.0, 3.0));
184-
assertEquals(3, intersections.size());
177+
Assertions.assertTrue(containsPoint(intersections, 0.0, 0.0));
178+
Assertions.assertTrue(containsPoint(intersections, 4.0, 0.0));
179+
Assertions.assertTrue(containsPoint(intersections, 2.0, 3.0));
180+
Assertions.assertEquals(3, intersections.size());
185181
}
186182

187183
@Test
@@ -190,29 +186,29 @@ void testCrossingDiagonals() {
190186
List<BentleyOttmann.Segment> segments = List.of(newSegment(0, 0, 10, 10), newSegment(0, 10, 10, 0), newSegment(5, 0, 5, 10), newSegment(0, 5, 10, 5));
191187

192188
Set<Point2D.Double> intersections = BentleyOttmann.findIntersections(segments);
193-
assertTrue(containsPoint(intersections, 5.0, 5.0), "Center point should be present");
194-
assertEquals(1, intersections.size());
189+
Assertions.assertTrue(containsPoint(intersections, 5.0, 5.0), "Center point should be present");
190+
Assertions.assertEquals(1, intersections.size());
195191
}
196192

197193
@Test
198194
void testVerySmallSegments() {
199195
List<BentleyOttmann.Segment> segments = List.of(newSegment(0.001, 0.001, 0.002, 0.002), newSegment(0.001, 0.002, 0.002, 0.001));
200196

201197
Set<Point2D.Double> intersections = BentleyOttmann.findIntersections(segments);
202-
assertEquals(1, intersections.size());
203-
assertTrue(containsPoint(intersections, 0.0015, 0.0015));
198+
Assertions.assertEquals(1, intersections.size());
199+
Assertions.assertTrue(containsPoint(intersections, 0.0015, 0.0015));
204200
}
205201

206202
@Test
207203
void testSegmentsShareCommonPoint() {
208204
List<BentleyOttmann.Segment> segmentsSameStart = List.of(newSegment(0, 0, 4, 4), newSegment(0, 0, 4, -4), newSegment(0, 0, -4, 4));
209205

210206
Set<Point2D.Double> intersectionsSameStart = BentleyOttmann.findIntersections(segmentsSameStart);
211-
assertTrue(containsPoint(intersectionsSameStart, 0.0, 0.0));
207+
Assertions.assertTrue(containsPoint(intersectionsSameStart, 0.0, 0.0));
212208
List<BentleyOttmann.Segment> segmentsSameEnd = List.of(newSegment(0, 0, 4, 4), newSegment(8, 4, 4, 4), newSegment(4, 8, 4, 4));
213209

214210
Set<Point2D.Double> intersectionsSameEnd = BentleyOttmann.findIntersections(segmentsSameEnd);
215-
assertTrue(containsPoint(intersectionsSameEnd, 4.0, 4.0));
211+
Assertions.assertTrue(containsPoint(intersectionsSameEnd, 4.0, 4.0));
216212
}
217213

218214
@Test
@@ -225,7 +221,7 @@ void testSegmentsAtAngles() {
225221
);
226222

227223
Set<Point2D.Double> intersections = BentleyOttmann.findIntersections(segments);
228-
assertTrue(containsPoint(intersections, 2.0, 2.0));
224+
Assertions.assertTrue(containsPoint(intersections, 2.0, 2.0));
229225
}
230226

231227
@Test
@@ -249,10 +245,10 @@ void testPerformanceWithManySegments() {
249245
long duration = endTime - startTime;
250246

251247
// Should complete in reasonable time (< 1 second for 100 segments)
252-
assertTrue(duration < 1000, "Algorithm should complete in less than 1 second for 100 segments. Took: " + duration + "ms");
248+
Assertions.assertTrue(duration < 1000, "Algorithm should complete in less than 1 second for 100 segments. Took: " + duration + "ms");
253249

254250
// Just verify it returns a valid result
255-
assertNotNull(intersections);
251+
Assertions.assertNotNull(intersections);
256252
System.out.println("Performance test: 100 segments processed in " + duration + "ms, found " + intersections.size() + " intersections");
257253
}
258254

@@ -267,8 +263,8 @@ void testIssueExample() {
267263
Set<Point2D.Double> intersections = BentleyOttmann.findIntersections(segments);
268264

269265
// Expected output: [(3, 3)]
270-
assertEquals(1, intersections.size(), "Should find exactly one intersection");
271-
assertTrue(containsPoint(intersections, 3.0, 3.0), "Intersection should be at (3, 3)");
266+
Assertions.assertEquals(1, intersections.size(), "Should find exactly one intersection");
267+
Assertions.assertTrue(containsPoint(intersections, 3.0, 3.0), "Intersection should be at (3, 3)");
272268
}
273269

274270
@Test
@@ -281,7 +277,7 @@ void testEventTypeOrdering() {
281277
);
282278

283279
Set<Point2D.Double> intersections = BentleyOttmann.findIntersections(segments);
284-
assertTrue(containsPoint(intersections, 2.0, 2.0));
280+
Assertions.assertTrue(containsPoint(intersections, 2.0, 2.0));
285281
}
286282

287283
@Test
@@ -291,8 +287,8 @@ void testCollinearOverlapWithInteriorPoint() {
291287
Set<Point2D.Double> intersections = BentleyOttmann.findIntersections(segments);
292288

293289
// Should find at least one overlap point (where segments touch/overlap)
294-
assertFalse(intersections.isEmpty(), "Should find overlap points for collinear segments");
295-
assertTrue(containsPoint(intersections, 2.0, 2.0) || containsPoint(intersections, 4.0, 4.0), "Should contain overlap boundary point");
290+
Assertions.assertFalse(intersections.isEmpty(), "Should find overlap points for collinear segments");
291+
Assertions.assertTrue(containsPoint(intersections, 2.0, 2.0) || containsPoint(intersections, 4.0, 4.0), "Should contain overlap boundary point");
296292
}
297293

298294
@Test
@@ -302,8 +298,8 @@ void testCollinearTouchingAtBothEndpoints() {
302298
List<BentleyOttmann.Segment> segments = List.of(newSegment(0, 0, 4, 4), newSegment(4, 4, 8, 8));
303299

304300
Set<Point2D.Double> intersections = BentleyOttmann.findIntersections(segments);
305-
assertEquals(1, intersections.size());
306-
assertTrue(containsPoint(intersections, 4.0, 4.0), "Should find touching point");
301+
Assertions.assertEquals(1, intersections.size());
302+
Assertions.assertTrue(containsPoint(intersections, 4.0, 4.0), "Should find touching point");
307303
}
308304

309305
@Test
@@ -314,9 +310,9 @@ void testCollinearOverlapPartialInterior() {
314310
Set<Point2D.Double> intersections = BentleyOttmann.findIntersections(segments);
315311

316312
// Should detect the overlap region
317-
assertFalse(intersections.isEmpty());
313+
Assertions.assertFalse(intersections.isEmpty());
318314
// The algorithm should return at least one of the boundary points
319-
assertTrue(containsPoint(intersections, 3.0, 3.0) || containsPoint(intersections, 5.0, 5.0));
315+
Assertions.assertTrue(containsPoint(intersections, 3.0, 3.0) || containsPoint(intersections, 5.0, 5.0));
320316
}
321317

322318
private static BentleyOttmann.Segment newSegment(double x1, double y1, double x2, double y2) {

0 commit comments

Comments
 (0)