280 likes | 291 Views
This algorithm efficiently recognizes and sorts Jordan sequences, useful for polygon clipping, and triangulating simple polygons. The process involves building upper and lower trees, analyzing pairs, and maintaining a sorted list of points.
E N D
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 Jordan curve Jordan curve in the plane: A continous curve that does not intersect itself
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 Jordan sequence The sequence x1,x2,.....,xn of the intersection points of a jordan curve C with the x-axis in the order in which they occur on C. 6, 1, 21, 13, 12, 7, 5, 4, 3, 2, 20, 18, 17, 14, 11, 10, 9, 8, 15, 16, 19
Our problem An efficient algorithm which would recognize and sort a jordan sequence.
A special case: sort the intersections of an n-sided simple polygon with a line.
Application: Polygon clipping Return the intersection of a polygon with a rectangular window (a collection of polygons)
Polygon clipping Return the intersection of a polygon with a rectangular window (a collection of polygons)
You find the intersections by the order they occur on the polygon. But you also need them by the order they occur on the rectangle
Polygon triangulation Useful for triangulating simple polygons.
Assume to simplify presentation Curve nowhere tangent to the x-axis. Curve start below the x-axis (if not reflect)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 Nested parenthesis Each pair {x2i-1 ,x2i} for i = 1,....,n/2 corresponds to part of C going from the x-axis up and back to the x-axis. 6, 1, 21, 13, 12, 7, 5, 4, 3, 2, 20, 18, 17, 14, 11, 10, 9, 8, 15, 16, 19
Nested parenthesis (cont) The sequence is a jordan sequence iff different pairs {x2i-1 ,x2i} {x2j-1 ,x2j} cannot cross Cross: either or
Upper and lower trees The parent of {x2i-1 ,x2i} is the “immediate” enclosing pair. This defines a forest . Add {-, } to make it a tree.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 Upper and lower trees (example) - 1 6 7 12 13 21 14 17 2 3 4 5 8 9 10 11 18 20 15 16 6, 1, 21, 13, 12, 7, 5, 4, 3, 2, 20, 18, 17, 14, 11, 10, 9, 8, 15, 16, 19
The algorithm The algorithm processes x1,x2,.....,xn one at a time building the upper tree, the lower tree, and a list of the points in sorted order. Initialization: Make {-, } the only pair in both trees. Initialize the sorted list to be {-, x1, } For i = 2,....n process xi as follows Suppose i is even, i.e. {xi-1 , xi} is to be added to the upper tree Assume xi-1 < xi (otherwise it is symmetric)
The algorithm (cont) Find the point x that follows xi-1 in the sorted list. Find the pair {xj-1 , xj} in the upper tree such that x {xj-1 , xj} Let {lj , rj } = {xj-1 , xj} Apply one of the following 4 cases Case A: lj < xi-1 < rj < xi lj xi-1 rj xi Stop, this is not a jordan sequence
The algorithm (cont) Case B: lj < xi-1 < xi < rj lj xi-1 xi rj Make {xi-1 , xi} the new last child of {xj-1 , xj}. Insert xi after xi-1 in the sorted list
The algorithm (cont) Case C: xi-1 < xi < lj xi-1 xi lj rj Insert {xi-1 , xi} into the list of siblings of {xj-1 , xj} just before {xj-1 , xj} Insert xi after xi-1 in the sorted list
lk xi rk The algorithm (cont) Case D: xi-1 < lj < xi xi-1 lj rj In the list of siblings of {xj-1 , xj} find the last pair {xk-1 , xk} such that lk < xi If rk > xi then it is not a jordan sequence.
The algorithm (cont) Case D: xi-1 < lj < xi lp xi-1 lj rj lk rk rp xi Otherwise if {xk-1 , xk} is the last child of its parent pair {xp-1 , xp} and rp < xi it is not a jordan sequence.
The algorithm (cont) Case D: xi-1 < lj < xi lp xi-1 lj rj lk rk xi rp Remove from the list of siblings of {xj-1 , xj} the sublist from {xj-1 , xj} to {xk-1 , xk} (inclusive) and replace it by {xi-1 , xi}. Make the removed sublist the list of children of {xi-1 , xi}. Insert xi after rk into the sorted list.
The algorithm (cont) If i is odd we work similarly with the lower tree.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 6 7 12 13 21 19 20 17 18 15 16 2 3 4 5 The algorithm (example) 6, 1, 21, 13, 12, 7, 5, 4, 3, 2, 20, 18, 17, 14, 11, 10, 9, 8, 15, 16, 19 - -
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 6 7 12 13 21 2 3 4 5 The algorithm (example) 6, 1, 21, 13, 12, 7, 5, 4, 3, 2, 20, 18, 17, 14, 11, 10, 9, 8, 15, 16, 19 - - 14 18.5 19 20 17 18 15 16
Representation The sorted list of processed numbers is a doubly linked list. Each list of siblings in the upper or lower trees is represented by a homogenous finger search tree. - 1 6 7 12 13 21 14 17 2 3 4 5 8 9 10 11 18 20 15 16
Analysis Inserting a pair before or after some other pair takes O(1) time. Finding and removing a sublist of d pairs from a list of s pairs takes O(1 + log(min{d,s-d})) We perform at most n/2 operations of the form “remove a sublist/insert a pair” each costs O(1 + log(min{d,s-d} )) for some 1 d s if it operates on a list of length s. What is the total time ?
Analysis (Cont) Assume all elements are there to begin with. This would just make the split operations more expensive. Charge the split evenly to the items in the smaller part, if d is the length of the smaller part then each element is charged log(d)/d The total charge per element is at most log(x1)/x1 + log(x2)/(x2) + … log(xk)/xk The xi decrease exponentially : xi≤ xi-1/2 Therefore the sum is at most Σ k/2k = O(1)