VOOZH about

URL: https://www.geeksforgeeks.org/dsa/given-a-set-of-line-segments-find-if-any-two-segments-intersect/

⇱ Sweep Line Algorithm - Find if any Two Segments Intersect - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Sweep Line Algorithm - Find if any Two Segments Intersect

Last Updated : 23 Jul, 2025

Given n line segments represented as a 3D vector points[][][], where each line segment i is defined by its endpoints stored in points[i][0] and points[i][1] (each containing 2 integers), your task is to determine if any two of these line segments intersect and find the number of intersection points.

Examples:

Input: points[][][] = [ [ [1, 5], [4, 5] ],
[ [2, 5], [10, 1] ],
[ [3, 2], [10, 3] ],
[ [6, 4], [9, 4] ],
[ [7, 1], [8, 1] ] ]
Output: 2
Explanation: Lines [ [1, 5], [4, 5] ] intersects with [ [2, 5], [10, 1] ] which further intersects with [ [3, 2], [10, 3] ]. Thus there are two intersection points.

[Naive Approach] - Check Every Pair - O(n^2) Time

A naive solution to solve this problem is to check every pair of lines and check if the pair intersects or not. We can check two line segments in O(1) time. Therefore, this approach takes O(n2). 

[Expected Approach] - Sweep Line Algorithm - O(n Log n) Time and O(n) Space

The idea is to use a sweep line method that processes the endpoints of the line segments from left to right, dynamically maintaining a set of segments currently "active" as the sweep line moves. With this approach, we only check for intersections between line segments that are close to each other in the vertical ordering, thus ensuring we handle the problem in a methodical and efficient manner. To manage the active segments, a self-balancing binary search tree (like an AVL or Red-Black Tree) or a similar data structure is used; this allows us to quickly insert new segments, remove segments once their right endpoint is passed, and find the segments immediately above and below a given segment.

Below is given the step-by-step approach to solve the problem:

  • Begin by representing every line segment with its two endpoints and mark each endpoint to indicate whether it is the left or the right end of the segment.
  • Sort all the endpoints according to their x-coordinate values.
  • Initialize an empty self-balancing binary search tree (or min heap for dynamic endpoint processing) to keep track of the active segments along the y-axis.
  • Process each endpoint from left to right:
    • If the current endpoint is a left endpoint, insert its segment into the active set and check for intersections with the immediate neighbors (the segment just above and the one just below) in the active set.
    • If the current endpoint is a right endpoint, remove its segment from the active set and check if the segments that were immediately above and below (now adjacent after removal) intersect with each other.
  • Continue the process until all endpoints have been handled and all potential intersections are checked.

Pseudocode:

Example:

There are 5 line segments 1, 2, 3, 4 and 5 and the dotted green lines show sweep lines. 

👁 sweepline

Following are steps followed by the algorithm

  • Process the left endpoint of segment 1.
    • Insert segment 1 into the BST.
    • The BST now contains: 1
    • No intersection is found.
  • Process the left endpoint of segment 2
    • Check for intersection between segment 2 and segment 1.
    • Insert segment 2 into the BST.
    • Intersection of 1 and 2 is detected. (In the pseudocode, we would return here, but we continue in this example to find all possible intersections.)
    • The BST now contains: 1, 2
  • Process the left endpoint of segment 3
    • Check for intersection between segment 3 and segment 1.
    • No intersection is found.
    • Insert segment 3 into the BST.
    • The BST now contains: 1, 2, 3
  • Process the right endpoint of segment 1
    • Remove segment 1 from the BST.
    • Check for intersection between segments 2 and 3.
    • Intersection of 2 and 3 is found and reported.
    • The BST now contains: 2, 3
  • Process the left endpoint of segment 4
    • Check for intersections between segment 4 and existing segments (2 and 3).
    • No intersection is found.
    • Insert segment 4 into the BST.
    • The BST now contains: 2, 4, 3
  • Process the left endpoint of segment 5
    • Check for intersection between segment 5 and segment 3.
    • No intersection is found.
    • Insert segment 5 into the BST.
    • The BST now contains: 2, 4, 3, 5
  • Process the right endpoint of segment 5
    • Remove segment 5 from the BST.
    • The BST now contains: 2, 4, 3.
  • Process the right endpoint of segment 4
    • Remove segment 4 from the BST.
    • The BST now contains: 2, 3
    • Check for intersection between segments 2 and 3.
    • Intersection of 2 and 3 is reported again. (Additional logic might be added to avoid duplicating the same intersection.)
    • The BST still contains: 2, 3
  • Process the right endpoints of segments 2 and 3
    • Remove segments 2 and 3 from the BST.
    • The BST becomes empty.

After these steps, all endpoints have been processed, and all intersections have been identified (with duplicates possibly reported depending on the implementation details). 


Output
Line: 2 3
Line: 1 2
2


Note: The above code does not handle cases when segments share end points (Same starting points or same ending points or same starting and ending points) because we handle all points in the same way without considering the line segment number. We have done that way to keep the code simple. The Event class currently compares events based only on y and x, and does not distinguish between segments with the same start point. To handle the common end points, we need to differentiate events more clearly in the comparator to avoid ambiguity between events with the same coordinates.

Time Complexity: O(n * log n), initially, sorting the points requires O(n log n) time. Next, each point is processed in O(log n) time, so processing all 2n points takes O(2n log n) time. This simplifies to an overall time complexity of O(n log n).
Space Complexity: O(n)

Comment
Article Tags: