90 likes | 176 Views
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
E N D
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 • WhileQ is not emptyDo • Determine the next event point p in Q and delete it. • HANDLEEVENTPOINT(p) • End While
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)
If U(p) ∪C(p) is emptythen • Let SLand SRbe 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)
FINDNEWEVENT(SL , SR, p) • If • SLand SRintersect 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
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)
Algorithm FINDINTERSECTIONS(S) • Input:A set S of 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 • WhileQ is not emptyDo • Determine the next event point p in Q and delete it. • HANDLEEVENTPOINT(p) • End While O(n log n) m(p) O( log n)
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 • IfL(p)∪U(p) ∪ C(p) ∪contains more than one segmentthen • 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)|+|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) (|L(p)|+|C(p)|)O( log n) (|U(p)|+|C(p)|)O( log n)
IfU(p) ∪C(p) is emptythen • Let SLand SRbe 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)
FINDNEWEVENT(SL , SR, p) • If • SLand SRintersect 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)