Presentation is loading. Please wait.

Presentation is loading. Please wait.

Orthogonal Line Segment Intersection Given N horizontal and N vertical line segments, find all intersections. All x and y coordinates are distinct.

Similar presentations


Presentation on theme: "Orthogonal Line Segment Intersection Given N horizontal and N vertical line segments, find all intersections. All x and y coordinates are distinct."— Presentation transcript:

1 Orthogonal Line Segment Intersection Given N horizontal and N vertical line segments, find all intersections. All x and y coordinates are distinct.

2 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.

3 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 ??

4 Q. What if the lines were not horizontal or vertical ??

5 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 set If (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 set If (I = Intersect( segA,segB) exists) Insert I into Q; set.delete(segE) }

6 Psuedo-Code else{ output intersection point swap 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; }

7 Applications Complexity: O ( (N +I)lg(N+I))

8 Find area of union of all rectangles.

9 Problems http://www.spoj.com/problems/POSTERS/ http://www.spoj.com/problems/RAIN1/ Topcoder SRM 283 PowerSupply

10 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 4 2 6 8 10 3 4 7 10 Output: 4


Download ppt "Orthogonal Line Segment Intersection Given N horizontal and N vertical line segments, find all intersections. All x and y coordinates are distinct."

Similar presentations


Ads by Google