This algorithm, FindIntersections(S), is a plane sweep algorithm designed to find all intersection points among a set S of line segments in the plane. It's a fundamental algorithm in computational geometry. Here's a breakdown:
Input: A set S of line segments in the plane.
Output: The set of intersection points. For each intersection point, the algorithm also identifies the segments that contain it.
1. Initialization:
Initialize an empty event queue Q: An event queue Q is created. This queue will store event points, which are points where something interesting happens (like the start or end of a segment, or an intersection). Q is typically implemented as a priority queue, sorted by the x-coordinate of the event points (and then the y-coordinate if x-coordinates are equal).Insert segment endpoints into Q: All endpoints of the segments in S are inserted into Q. Crucially:
Initialize an empty status structure T: A status structure T is created. This structure will store the segments that are currently "active" at the sweep line (an imaginary vertical line that moves across the plane from left to right). T is usually implemented as a balanced binary search tree, ordered by the y-coordinate of the segments at the sweep line.2. Main Loop:
while Q is not empty: The algorithm continues as long as there are events in the queue.Determine the next event point p in Q and delete it: The event point p with the smallest x-coordinate (and smallest y-coordinate if x's are equal) is retrieved and removed from Q.HandleEventPoint(p): The core logic of the algorithm is contained in the HandleEventPoint function. This function is called for each event point p and handles the different cases:Inside HandleEventPoint(p):
There are three possible scenarios for the event point p:
s associated with p into the status structure T.s for intersections with its neighbors in T (the segments immediately above and below it). If intersections are found to the right of the sweep line, add those intersection points as new events to Q.s associated with p from the status structure T.s in T now become neighbors, check them for intersections. If they intersect to the right of the sweep line, add that intersection point as a new event to Q.p and the segments that intersect at p.T. This is because after the intersection point, their vertical order changes.Q.Key Ideas and Why it Works: