Algorithm FINDINTERSECTIONS( S ) Input: A set S of n line segments

9
Algorithm FINDINTERSECTIONS(S) Input: A set S of n line segments Output: The set of intersection points (so that for each intersection point we also return the segments that contain it) Initialize an empty event queue Q Insert the segment endpoints into Q (when an upper endpoint is inserted, the corresponding segment should be stored with it) Initialize an empty status structure T While Q is not empty Do Determine the next event point p in Q and delete it.

description

Algorithm FINDINTERSECTIONS( S ) Input: A set S of n line segments Output: The set of intersection points (so that for each intersection point we also return the segments that contain it) Initialize an empty event queue Q - PowerPoint PPT Presentation

Transcript of Algorithm FINDINTERSECTIONS( S ) Input: A set S of n line segments

Page 1: Algorithm FINDINTERSECTIONS( S ) Input: A set  S  of  n  line segments

Algorithm FINDINTERSECTIONS(S)

Input: A set S of n line segments

Output: The set of intersection points (so that for eachintersection point we also return the segments that contain it)

Initialize an empty event queue Q

Insert the segment endpoints into Q (when an upper endpoint is inserted, the corresponding segment should be stored with it)

Initialize an empty status structure T

While Q is not empty Do

Determine the next event point p in Q and delete it.

HANDLEEVENTPOINT(p)

End While

Page 2: Algorithm FINDINTERSECTIONS( S ) Input: A set  S  of  n  line segments

HANDLEEVENTPOINT(p)

U(p)= the set of segments whose upper endpoint is p

(note: segments from U(p) are not in T)

Find all segments stored in T that contain p; they are adjacent in T

L(p) = the subset of segments found whose lower endpoint is p

C(p) = the subset of segments found that contain p in their interior

If L(p) ∪U(p)∪C(p) ∪contains more than one segment then

Report p as an intersection, together with L(p), U(p), and C(p)

End If

Delete the segments in L(p) ∪ C(p) from T

Insert the segments in U(p) ∪ C(p) into T

The order of the segments in T should correspond to the order in which they are intersected by a sweep line just below (deleting and re-inserting the segments of C(p) reverses their order)

Page 3: Algorithm FINDINTERSECTIONS( S ) Input: A set  S  of  n  line segments

If U(p) ∪C(p) is empty then

Let SL and SR be the left and right neighbors of p in T

FINDNEWEVENT( SL , SR , p )

Else

Let S’ be the leftmost segment of U(p)∪C(p) in T

Let SL be the left neighbor of s in T

FINDNEWEVENT(SL, S’, p)

Let S’’ ‘be the rightmost segment of U(p)∪C(p) in T

Let SR be the right neighbor of S’’ in T

FINDNEWEVENT(S’’ , SR , p)

Page 4: Algorithm FINDINTERSECTIONS( S ) Input: A set  S  of  n  line segments

FINDNEWEVENT(SL , SR, p)

If

SL and SR intersect below the sweep line, or on it and to the right of the current event point p, and the intersection is not yet present as an event in Q

Then

Insert the intersection point as an event into Q

Page 5: Algorithm FINDINTERSECTIONS( S ) Input: A set  S  of  n  line segments

Let q be the maximal number of event points in Q

Let I be the number of intersection points

Note that q ≤ number of intersection points + number of endpoints ≤ n2 (if n > 1)

Page 6: Algorithm FINDINTERSECTIONS( S ) Input: A set  S  of  n  line segments

Algorithm FINDINTERSECTIONS(S)

Input: A set S of line segments

Output: The set of intersection points (so that for eachintersection point we also return the segments that contain it)

Initialize an empty event queue Q

Insert the segment endpoints into Q (when an upper endpoint is inserted, the corresponding segment should be stored with it)

Initialize an empty status structure T

While Q is not empty Do

Determine the next event point p in Q and delete it.

HANDLEEVENTPOINT(p)

End While

O(n log n)

m(p) O( log n)

Page 7: Algorithm FINDINTERSECTIONS( S ) Input: A set  S  of  n  line segments

HANDLEEVENTPOINT(p)

U(p)= the set of segments whose upper endpoint is p

Find all segments stored in T that contain p; they are adjacent in T

L(p) = the subset of segments found whose lower endpoint is p

C(p) = the subset of segments found that contain p in their interior

If L(p) ∪U(p) ∪ C(p) ∪contains more than one segment then

Report p as an intersection, together with L(p), U(p), and C(p)

End If

Delete the segments in L(p) ∪ C(p) from T

Insert the segments in U(p) ∪ C(p) into T

The order of the segments in T should correspond to the order in which they are intersected by a sweep line just below (deleting and re-inserting the segments of C(p) reverses their order)

(|L(p)|+|C(p)|)O( log n)

(|U(p)|+|C(p)|)O( log n)

(|L(p)|+|U(p)|+|C(p)|)O( log n) = m(p) O( log n)

(|L(p)|+|U(p)|+|C(p)|)O( log n) = m(p) O( log n)

Page 8: Algorithm FINDINTERSECTIONS( S ) Input: A set  S  of  n  line segments

If U(p) ∪C(p) is empty then

Let SL and SR be the left and right neighbors of p in T

FINDNEWEVENT( SL , SR , p )

Else

Let S’ be the leftmost segment of U(p)∪C(p) in T

Let SL be the left neighbor of s in T

FINDNEWEVENT(SL, S’, p)

Let S’ be the rightmost segment of U(p)∪C(p) in T

Let SR be the right neighbor of S’ in T

FINDNEWEVENT(s, sr, p) O(log n)

O(log n)

O(log n)

Page 9: Algorithm FINDINTERSECTIONS( S ) Input: A set  S  of  n  line segments

FINDNEWEVENT(SL , SR, p)

If

SL and SR intersect below the sweep line, or on it and to the right of the current event point p, and the intersection is not yet present as an event in Q

Then

Insert the intersection point as an event into Q

O(log q) ≤ O(log n2) ≤ O(2 log n) = O(log n)