Title: Convex Hull in Two Dimensions
1Convex Hull in Two Dimensions
- Jyun-Ming Chen
- Refs deBerg et al. (Chap. 1)
- ORourke (Chap. 3)
2Outline
- Definitions of CH
- Algorithms for CH
- Analysis of algorithms
3Convex Hull
??(Convex Hull)
- Convex hull of a point set S, CH(S)
- Smallest convex set containing S
- Intersection of all convex sets containing S
4Convex Hull
- Why computing CH?
- Many reasons e.g., faster collision detection
- How to compute CH?
- Good solutions come from
- A thorough understanding of the geometric
properties of the problem - Proper application of algorithmic techniques and
data structures
5General Steps
- Sketch your problem in general position
- Observe the desired output and reason about the
possible steps to get to the goal - Check for degeneracy
- Time/resource complexity
- General case
- Best/worst cases
6Computing CH
- of a finite set P of n points in 2D
- Geometric intuition Rubberband analogy
- Representation
- Input point set
- Output CH(P) CW-ordered point set
7Observation
- Idea
- Search through all pairs of points for convex
edges - Form a loop
8Naïve Algorithm
You can add a break here if you like
9Analysis of Naïve Algorithm
- Assumption points are in general position
- (usually this is done first to simplify the code)
- (consider the degenerate cases later)
- How to compute lie to the left of directed
line? - Complexity analysis O(n3)
- (n2n)/2 pairs, each with n2 other points to
examine - High complexity due to
- simply translate geometric insight into an
algorithm in a brute-force manner
10Considering Degeneracy
- Collinearity
- Modify the test to all points to the right, or
on the line - Does this solve the problem?!
11Degenerate Case (cont)
- Consider numerical inaccuracy
- Two possibilities
12Jarvis March (Gift Wrapping)
- A modification of naïve algorithm
- Start at some extreme point, which is guaranteed
to be on the hull. - At each step, test each of the points, and find
the one which makes the smallest right-hand turn.
That point has to be the next one on the hull. - The final CW loop is the CH.
13Jarvis March (cont)
- Complexity
- Crude count O(n2)
- Output sensitive
- O(nh), h of segments on the hull
Worst case
14A Better Algorithm
- Sort by x-coord
- The extreme points must be on the CH
- Goal in 2 linked lists
- Upper hull p1 to pn
- Lower hull pn to p1
- Idea
- In CW manner, always making right turns
- If fail to turn right, delete previous point
until the turn is correct.
15(No Transcript)
16Step-by-step
7
3
5
9
1
6
4
2
8
1,2
17Step-by-step
7
3
5
9
1
6
4
2
8
1,2,3
18Step-by-step
7
3
5
9
1
6
4
2
8
1,3
19Step-by-step
7
3
5
9
1
6
4
2
8
1,3,4
20Step-by-step
7
3
5
9
1
6
4
2
8
1,3,4,5
21Step-by-step
7
3
5
9
1
6
4
2
8
1,3,5
22Step-by-step
7
3
5
9
1
6
4
2
8
1,3,5,6
23Step-by-step
7
3
5
9
1
6
4
2
8
1,3,5,6,7
24Step-by-step
7
3
5
9
1
6
4
2
8
1,3,5,7
25Step-by-step
7
3
5
9
1
6
4
2
8
1,3,7
26Step-by-step
7
3
5
9
1
6
4
2
8
1,3,7,8
27Step-by-step
7
3
5
9
1
6
4
2
8
1,3,7,8,9
28Step-by-step
7
3
5
9
1
6
4
2
8
1,3,7,9 Upper hull completed
29Algorithm (cont)
- This is a variation of Graham Scan
- Proof of correctness p.8
- Complexity analysis O(n log n)
- Lexicographical sorting of points O(n log n)
- Computation of lower hull O(n)
- Consider the for and while
30Consider Degeneracy Again
- Effects on sorting
- Lexico. ordering
31Other Version of Graham Scan
- Find an extreme point. This point will be the
pivot, is guaranteed to be on the hull, and is
chosen to be the point with smallest y
coordinate. - Sort the points in order of increasing angle
about the pivot. We end up with a star-shaped
polygon (one in which one special point, in this
case the pivot, can "see" the whole polygon). - Build the hull, by marching around the
star-shaped poly, adding edges when we make a
left turn, and back-tracking when we make a right
turn.
32Graham Scan (ver2)
- Also handles collinearity
33Incremental Algorithm
- Suitable for dynamic CGeom and 3D hull
- If pn?CHn-1,
- CHnCHn-1
- Else
- Find two tangents, update CHn
34Incremental Algorithm
- Input a set of points P in 2D
- Output a list L containing the points of CH(P)
in CCW order - 1. Take first three points of P to form a
triangle - 2. For i 4 to n
- If (pi is in CHi-1) do nothing
- Else
- FindTangent (pi, CHi-1)?t1, t2
- Replace the items t2 t1 in L by t2, pi, t1
35FindTangent (p, CH)
- (Go through all points in CH circularly)
- For each point pi
- If XOR (p is left_or_on (pi-1,pi), p is
left_or_on(pi,pi1)) - Mark pi as the tangent point
- (There will be two tangent points)
- Determine t1, t2
36Incremental Algorithm (cont)
- Finding Tangents
- Analysis
37Quick Hull
38Quick Hull (cont)
- Time complexity
- Find extreme point c O(n)
- Cost of recursive call
- T(n) O(n) T(A) T(B)
- Best case A B n/2
- T(n) 2T(n/2) O(n) T(n) O(n log n)
- Worst case A 1, B n-1
- T(n) T(n-1) O(n) T(n) O(n2)
39Divide and Conquer Algorithm
- Computing 3D hull
- Graham scan (probably not ?angle sort)
- Preparata and Hong (1977)
a-1 a1 at left side of ab b-1 b1 at left
side of ab
T(n) 2T(n/2) O(n) T(n) O(n log n)
40Extend to 3D
41Floating Point Inaccuracies
- Handled by
- Interval arithmetic
- Exact (rational) arithmetic
42Interval arithmetic
- Ref http//www.eng.mu.edu/corlissg/VC02/READ_ME.h
tml
43Exact Math
- Rational arithmetic (never floating point)
- In6 1/12 - 7/9 31/36
- Out6 - 1/6
- The Exact Arithmetic Competition Level 0 Tests
- Ref XR
44Exercise
- From this applet, develop a Graham scan algorithm
that returns a CW-ordered convex hull - Express the algorithm in pseudocode. Make sure it
works for general position and degenerate cases
(shown right) - Run your example on test cases (general position
and degeneracies)
- Explain the correctness and time complexity of
divide-and-conquer algorithm
45(No Transcript)
46More reference on the web.