orthogonal line segment intersection given n horizontal and n vertical line segments, find all...

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

Upload: maude-lawson

Post on 24-Dec-2015

217 views

Category:

Documents


4 download

TRANSCRIPT

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

Orthogonal Line Segment Intersection• Given N horizontal and N

vertical line segments, find all intersections.

All x and y coordinates are distinct.

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

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

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

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

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

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

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)}

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

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;

}}

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

Applications

• Complexity: O ( (N +I)lg(N+I))

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

Find area of union of all rectangles.

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

Problems

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

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

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