orthogonal line segment intersection given n horizontal and n vertical line segments, find all...
TRANSCRIPT
Orthogonal Line Segment Intersection• Given N horizontal and N
vertical line segments, find all intersections.
All x and y coordinates are distinct.
Solution• Brute Force: O(n^2)• Line Sweep: 1. Sort the points according to x coordinates.2. Sweep a vertical line from left to right.3. X- coordinates define event Event 1: Left Endpoint of horizontal line
encountered: Action: Insert y coordinate into BST Event 2: Right endpoint of horizontal line encountered. Action: Remove y coordinate from BST as we have
processed the line.
• Event 3: A vertical line is encountered
• Action: Do a range search of
endpoints of y in the bst. Number of y in between current endpoints is number of
intersection this vertical line makes.
Time Complexity ??
Q. What if the lines were not horizontal or vertical ??
Psuedo Code• Maintain a priority queue Q • Initially all end points are added to Q• While(Q not empty){
event E= Q.delete – min()if(event is left){
set.insert(seg E) seg A = the segment Above segE in set
seg B = the segment below segE in setIf (I = Intersect( segE,segA) exists) Insert I into Q; If (I = Intersect( segE,segB) exists) Insert I into EQ;}else if(event is right){ seg A = the segment Above segE in set seg B = the segment below segE in setIf (I = Intersect( segA,segB) exists) Insert I into Q;
set.delete(segE)}
Psuedo-Code• else{
output intersection pointswap positions of intersecting segment in segA =
the segment above segE2 in SL; segB = the segment below segE1 in SL; If (I = Intersect(segE2,segA) exists) If (I is not in Q already) Insert I into Q; If (I = Intersect(segE1,segB) exists) If (I is not in Q already) Insert I into Q;
}}
Applications
• Complexity: O ( (N +I)lg(N+I))
Find area of union of all rectangles.
Problems
• http://www.spoj.com/problems/POSTERS/• http://www.spoj.com/problems/RAIN1/• Topcoder SRM 283 PowerSupply
Problems• There are N posters hung on
a wall one after another. How many posters have at least one visible section in the end?
Input:5 1 42 6 8 10 3 4 7 10Output:4