Skip to content

Conversation

Microindole
Copy link
Contributor

Description

This PR implements the Bentley-Ottmann sweep-line algorithm for efficiently finding all intersection points among a set of line segments in a 2D plane.

Changes

  • Added BentleyOttmann.java with complete implementation
  • Added BentleyOttmannTest.java with 19 comprehensive test cases
  • Implemented O((n + k) log n) time complexity using sweep-line approach
  • Handles edge cases: vertical/horizontal segments, collinear overlaps, touching endpoints
  • Includes detailed Javadoc with algorithm explanation and usage examples

Algorithm Details

  • Time Complexity: O((n + k) log n), where n = number of segments, k = number of intersections
  • Space Complexity: O(n + k)
  • Data Structures:
    • Event Queue: PriorityQueue<Event>
    • Status Structure: TreeSet<Segment> (balanced BST)

Test Coverage

  • Single and multiple intersections
  • Vertical and horizontal segments
  • Parallel segments (no intersection)
  • Coincident and overlapping collinear segments
  • Grid patterns and star patterns
  • Touching endpoints
  • Performance test with 100 random segments
  • Edge cases: empty list, single segment, null input

Example Usage

List segments = Arrays.asList(
    new Segment(new Point2D.Double(1, 1), new Point2D.Double(5, 5)),
    new Segment(new Point2D.Double(1, 5), new Point2D.Double(5, 1))
);
Set intersections = BentleyOttmann.findIntersections(segments);
// Returns: {(3.0, 3.0)}

Related Issue

Closes #6870

Checklist

  • Code follows the project's style guidelines (clang-format applied)
  • Self-review of code completed
  • Code is well-commented with Javadoc
  • Comprehensive unit tests added
  • All tests pass locally
  • No new warnings introduced
  • Algorithm is not already implemented in the repository

- Implement sweep-line algorithm for finding all intersection points
- Time complexity: O((n + k) log n) where n is segments, k is intersections
- Uses event queue (PriorityQueue) and status structure (TreeSet)
- Handles vertical/horizontal segments, collinear overlaps, and touching endpoints
- Includes comprehensive Javadoc with examples and references
- 19 test cases covering typical, edge, and degenerate cases
- Tests include: single/multiple intersections, parallel segments, grid patterns
- Performance test with 100 random segments
- All tests validate correctness of intersection detection
@codecov-commenter
Copy link

codecov-commenter commented Oct 21, 2025

Codecov Report

❌ Patch coverage is 81.48148% with 35 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.02%. Comparing base (d5289b9) to head (42d8336).
⚠️ Report is 3 commits behind head on master.

Files with missing lines Patch % Lines
...ava/com/thealgorithms/geometry/BentleyOttmann.java 81.48% 24 Missing and 11 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #6871      +/-   ##
============================================
+ Coverage     77.95%   78.02%   +0.06%     
- Complexity     6429     6499      +70     
============================================
  Files           736      738       +2     
  Lines         21476    21708     +232     
  Branches       4204     4255      +51     
============================================
+ Hits          16742    16937     +195     
- Misses         4060     4087      +27     
- Partials        674      684      +10     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE REQUEST] Proposal: Add Bentley–Ottmann Algorithm for Line Segment Intersection

2 participants