×

Search anything:

# Bentley Ottmann Algorithm for Plane Sweeping

#### computational geometry Algorithms Get this book -> Problems on Array: For Interviews and Competitive Programming

• Introduction to Bentley Ottmann Algorithm for Plane Sweeping
• Binary Search Tree
• Priority Queue
• Algorithm
• Implementation
• Analysis
• Overview

# Introduction to Bentley Ottmann Algorithm for Plane Sweeping

In this article, we shall be discussing the Bentley-Ottmann algorithm for computing a plane sweep. This lists all the intersection points in a set of line segments. The Bentley-Ottman algorithm takes O((n+k)logn) time where k = o(n^2/logn), it makes the algorithm more efficient than a standard naive approach which would take O(n^2) by checking every pair of segments. Put simply, the Bentley-Ottmann algorithm sweeps across the given plan using a line. That said, a vertical line (let's call it L) will sweep across the plan horizontally or vertically, intersecting the input line segments in sequence as it move across. Generally, we can say:

• No two line segment endpoints or crossings have the same x-coordinate
• No line segment endpoint lies upon another line segment
• No three line segments intersect at a single point

When this is the case, line L will always intersect the input line segments in a set of points whose vertical ordering changes only at a set of discrete events. An event could be associated with an endpoint (far left or far right of a line) or an intersection point of two line segments. This means that the motion of line L can be decomposed into a finite sequence, and therefore simulated by an algorithm.

The outcome of the simulation has two possibilities. When our line sweeps across an endpoint of a segment, the intersection of L with the segment is added to/removed from the vertically ordered set of intersection points. We can predict this as the end points are known from the input to the algorithm. The other possibility is when L sweeps across an intersection of two line segments. We know this will happen if both the intersection points between L and each of the segments are adjacent in the vertical ordering of the intersection points.

To maintain the intersection points of the sweeping line L with the input line segments, the Bentley-Ottmann algorithm maintains two data structures:

## Binary Search Tree

The tree contains the set of input line segments which intersect with L, ordered by the y-coordinate of the intersection points. The points themselves however are not explicitly represented in the BST. A segment will be inserted into the tree when our sweep line crosses the left end point of the segment. The position of the segment can be computed with a binary search, which tests whether the endpoint is above or below another segment which is intersected by L. This means that an intersection can be done in logarithmic time. The algorithm can also delete segments from the BST, then use the tree to determine the segments that are immediately above or below others. It can be done entirely in the tree and does not need the geometry of the segments. ## Priority Queue

The second data structure used in the algorithm is a priority queue. The queue is used to maintain a potential sequence of future events in the algorithm. As described in the introduction, each event can either be related to when the line sweeps across an endpoint of a segment or an intersection point. The event happens when L sweeps over the left endpoint of a segment, meaning the events can be prioritised with respect to the x-coordinates of the points that are associated. The possible future events consist of the line segment endpoints which have not yet been swept and the intersection points of lines containing pairs of segments which are immediately above or below each other. # Algorithm

Having learned the principles of the algorithm, we can now formulate it into a computational approach.

• Initialise a priority queue (Q) of possible future events, each of which are associated with a point in the plane and are prioritised by x-coordinate. Meaning that initially, Q contains an event for each endpoint of the input segments.
• Initialise a self-balancing BST (T) of the line segments which intersect with our sweep line (L). This is ordered by y-coordinate of the intersection points. Initially T is empty.
• While Q is not empty, find and remove the event from Q associated with the point that has the minimum x-coordinate and determine the event type. Then, we can proceed in three ways:
• If the point (P) is the left endpoint of the given line segment (S), insert S into T. Find the line segments which are immediately above and below S in T if they exist (r and t). If the intersection of the segments forms a possible future event in the priority queue, remove the event. If origin line segment intersects the ones above or below it, add the intersection points to the queue of possible future events.
• If P is the right endpoint of S, remove S from T. Find the line segments that are immediately above and below S (prior to S's removal) in T if it exists. If these segments intersect, add the intersection point as a possible future event in the queue.
• If P is an intersection point of two segments (where S is below t), swap their positions in T. After swapping find segments r and u (if exist) that are immediately below and above t and s, respectively. Remove any intersection points r-s and t-u from the queue. If r and t cross or s and u cross, add the intersection points to the queue

## Implementation

Here is an implementation of the algorithm in Python

Note: Q and T are standard priority queue and BST classes

Algorithm implementation

``````from Q import Q
from T import T
from helper import *

ev = 0.00000001

# to determine which is the furthest right/left
lower_check = 100

# get the next point (eg. point with lower y-coordinate)
def getNextPoint(p, seg, y_lower):
p1 = seg
p2 = seg
if (p1-p2)==0:
return (p+10, p)
slope = float(p1-p2)/(p1-p2)
if slope==0:
return (p1, p-y_lower)
y = p-y_lower
x = p1-(p1-y)/slope
return (x, y)

"""
for each event point:
U_p = segments that have p as an upper endpoint
C_p = segments that contain p
L_p = segments that have p as a lower endpoint
"""
def handle_event_point(p, segs, q, t, intersections):
rightmost = (float("-inf"), 0)
rightmost_seg = None
leftmost = (float("inf"), 0)
leftmost_seg = None

U_p = segs
(C_p, L_p) = t.contain_p(p)
merge_all = U_p+C_p+L_p
if len(merge_all) > 1:
intersections[p] = []
for s in merge_all:
intersections[p].append(s)
merge_CL = C_p+L_p
merge_UC = U_p+C_p
for s in merge_CL:
# deletes at a point slightly above (to break ties) - where seg is located in tree
# above intersection point
t.delete(p, s)
# put segments into T based on where they are at y-val just below p
for s in merge_UC:
n = getNextPoint(p, s, lower_check)
if n > rightmost:
rightmost = n
rightmost_seg = s
if n < leftmost:
leftmost = n
leftmost_seg = s
t.insert(p, s)

# means only L_p -> check newly-neighbored segments
if len(merge_UC) == 0:
neighbors = (t.get_left_neighbor(p), t.get_right_neighbor(p))
if neighbors and neighbors:
find_new_event(neighbors.value, neighbors.value, p, q)

# of newly inserted pts, find possible intersections to left and right
else:
left_neighbor = t.get_left_neighbor(p)
if left_neighbor:
find_new_event(left_neighbor.value, leftmost_seg, p, q)
right_neighbor = t.get_right_neighbor(p)
if right_neighbor:
find_new_event(right_neighbor.value, rightmost_seg, p, q)

def find_new_event(s1, s2, p, q):
i = intersect(s1, s2)
if i:
if compare_by_y(i, p) == 1:
if not q.find(i):
q.insert(i, [])

# segment ((x, y), (x, y)) form
# first part in segment should have higher y-coordinate
def intersection(S):
s0 = S
if s0 > s0:
s0 = (s0, s0)
q = Q(s0, [s0])
q.insert(s0, [])
intersections = {}
for s in S[1:]:
if s > s:
s = (s, s)
q.insert(s, [s])
q.insert(s, [])
t = T()
while q.key:
p, segs = q.get_and_del_min()
handle_event_point(p, segs, q, t, intersections)
return intersections
``````

Helper functions

``````ev = 0.0000001
# floating-point comparison
def approx_equal(a, b, tol):
return abs(a - b) < tol

# ordering of tree
def compare_by_x(k1, k2):
if approx_equal(k1, k2, ev):
return 0
elif k1 < k2:
return -1
else:
return 1

# priority of Q
def compare_by_y(k1, k2):
if approx_equal(k1, k2, ev):
if approx_equal(k1, k2, ev):
return 0
elif k1 < k2:
return -1
else:
return 1
elif k1 > k2:
return -1
else:
return 1

# tests if s0 and s1 represent the same segment (i.e. pts can be in 2 different orders)
def segs_equal(s0, s1):
x00 = s0
y00 = s0
x01 = s0
y01 = s0
x10 = s1
y10 = s1
x11 = s1
y11 = s1
if (approx_equal(x00, x10, ev) and approx_equal(y00, y10, ev)):
if (approx_equal(x01, x11, ev) and approx_equal(y01, y11, ev)):
return True
if (approx_equal(x00, x11, ev) and approx_equal(y00, y11, ev)):
if (approx_equal(x01, x10, ev) and approx_equal(y01, y10, ev)):
return True
return False

# get gradient of a given segment
def get_slope(s):
x0 = s
y0 = s
x1 = s
y1 = s
if (x1-x0)==0:
return None
else:
return float(y1-y0)/(x1-x0)

# given a point p, return the point on s that shares p's y-val
def get_x_at(s, p):
m = get_slope(s)
if m == 0: # horizontal segment
return p
if m is None: # vertical segment
return (s, p)
x1 = s-(s-p)/m
return (x1, p)

# Returns intersection point of two segments, or none if doesn't exist
def intersect(seg1, seg2):
p = seg1
r = (seg1-seg1, seg1-seg1)
q = seg2
s = (seg2-seg2, seg2-seg2)
denom = r*s-r*s
if denom == 0:
return None
numer = float(q-p)*s-(q-p)*s
t = numer/denom
numer = float(q-p)*r-(q-p)*r
u = numer/denom
if (t < 0 or t > 1) or (u < 0 or u > 1):
return None
x = p+t*r
y = p+t*r
return (x, y)
``````

## Analysis of Algorithm

The algorithm can process one event per segment endpoint or intersection point, in order sorted by x-coordinate of these points since it may be proved by induction. Once the ith event has been processed, the next event (if it is an intersection point) must be an intersection of two segments that are adjacent in the ordering of the segments in our BST, because the algorithm maintains all the intersections between adjacent segments as possible future events in Q, the correct next event will always be present in the queue, meaning it will correctly find all the intersections of the input points.

The algorithm will process 2n+k events where n is the number of inputted line segments and k is the number of crossings. Each event is processed by a a constant number of BST and queue operations. Since the queue only contains segment endpoints and crossings between adjacent segments, it will not exceed more than 3n events. All given operations will take O(logn) to complete and therefore giving us the total time complexity of O((n+k)logn).

# Overview

In this article we have covered the Bentley-Ottmann algorithm for computing all the intersections of a set of line segments by using the plane-sweeping technique. I have described the general theory behind the algorithm, the data structures used, the computational approach and implementation before finally analysing this to get both the proof that the algorithm works, and its time complexity. 