Take the Data Structures and Algorithms in Python course
Intersection detection of geometric objects is a fundamental operation in computational geometry. It has a wide range of applications, some of which are listed below:
Collision detection in computer games and simulations.
Ray shooting in computer graphics.
Map overlay in geographic information systems (GIS).
Collision avoidance in robot navigation.
Problem statement: Given a set
The line segments are defined by their endpoints.
The number of intersections of
The simple naive method checks for intersections between all pairs of line segments. Because all pairs of line segments can potentially intersect, the naive algorithm is optimal for the worst-case inputs with a time complexity of
For problems where the size of the output varies, output-sensitive algorithms are preferred because their time complexity depends on both the problem size and the output size. For an instance of the line segment intersection problem with
We’ll now discuss an
When solving geometric problems, certain special configurations of the geometric input can be conveniently ignored to keep the algorithm simple. These special cases are handled separately. When they are ignored, the input to the problem is considered to be in a general position, and the general position assumption defines the coincidental configurations.
For the line segment intersection problem, the following special configurations are ignored:
No three lines intersect at the same point.
No endpoint lies on another line segment.
The
A vertical sweep line scans the input from left to right. The intersection of the sweep line with the line segments is used to discover imminent intersection points.
Let
We notice that the sweep line status only changes when the sweep line encounters certain special points in the input. These points are known as event points and are listed below, together with the changes that they trigger in the sweep line status.
Left endpoint of a line segment
Right endpoint of a line segment
Intersection point of a pair of line segments
Because the sweep line status only changes at the event points, instead of continuously scanning the plane, the sweep line can jump from one event point to the next one.
To ensure
The sweep line status is the top-to-bottom ordered list of the line segments that intersect the sweep line and is stored in an ordered dictionary. The event points are stored in a priority queue. The following operations are supported by each of these data structures.
Ordered dictionary for the sweep line status
The following operations are supported by the ordered dictionary in
Insert a line segment
Delete a line segment
Fetch the predecessor
Fetch the successor
Swap two line segments
Priority queue for the event points
The following operations are supported by the event queue in
Insert an event point
Delete an event point
Extract the event point with the highest priority
The sweep line algorithm starts with an empty event queue and an empty ordered dictionary. At the start, the endpoints of all the line segments are inserted into the event queue. In the event queue, the point with the lowest
Note that any two intersecting line segments are adjacent on the sweep line right before the intersection. Therefore, the algorithm only inserts intersection points between adjacent line segments into the event queue, postponing the discovery of the intersections between nonadjacent line segments to a later stage.
When a new line segment gets inserted into the priority queue, the algorithm checks for its intersection with the preceding and succeeding line segments instead of checking for its intersection with all
Next, we see how the sweep line status and the event queue change for the three kinds of event points. In the next three figures, the dashed gray line is the sweep line.
Left endpoint of line segment
The discovery of the left endpoint marks the discovery of a new line segment triggering the following changes to the sweep line status and event points:
Insert
Remove the intersection point between
Check for intersections between the pairs
Right endpoint of line segment
The discovery of the right endpoint triggers the following changes to the sweep line status and event points:
Let
Remove
Check for an intersection point between
Note that possible intersections between the pairs
or lie in the past and are already discovered by the algorithm.
Intersection point of line segments
When an intersection point is discovered, it is added to the solution, and the following changes are made to the sweep line status and event points:
Let
Swap the line segments
Remove the intersection point between the pairs
Check for intersection points between the pairs
To summarize, the intersection of any pair of line segments that become adjacent on the sweep line is checked, and the intersection point is added to the event queue. The intersection point of any pair of line segments that are no longer adjacent, is removed from the event queue.
For an input with
The following course on the Educative platform helps developers learn and test their problem-solving skills.
Free Resources