1 Convex Hull in Two Dimensions Jyun-Ming Chen Refs: deBerg et al. (Chap. 1) O’Rourke (Chap. 3)

46
1 Convex Hull in Two Dimensions Jyun-Ming Chen Refs: deBerg et al. (Chap. 1) O’Rourke (Chap. 3)

Transcript of 1 Convex Hull in Two Dimensions Jyun-Ming Chen Refs: deBerg et al. (Chap. 1) O’Rourke (Chap. 3)

1

Convex Hull in Two Dimensions

Jyun-Ming ChenRefs: deBerg et al. (Chap. 1)O’Rourke (Chap. 3)

2

Outline

Definitions of CHAlgorithms for CHAnalysis of algorithms

3

Convex Hull

Definition of convexity

Convex hull of a point set S, CH(S) Smallest convex

set containing S Intersection of all

convex sets containing S

凸包 (Convex Hull)

4

Convex 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

5

General Steps

Sketch your problem in general positionObserve the desired output and reason about the possible steps to get to the goalCheck for degeneracyTime/resource complexity General case Best/worst cases

6

Computing CH

… of a finite set P of n points in 2DGeometric intuition: Rubberband analogy

Representation: Input: point set Output: CH(P): CW-

ordered point set

7

Observation

Def: convex edges

Idea: Search through all

pairs of points for convex edges

Form a loop

8

Naïve AlgorithmYou can add a

break here if you like …

9

Analysis 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) (n2–n)/2 pairs, each with n–2 other points to

examine

High complexity due to: simply translate geometric insight into an

algorithm in a brute-force manner

10

Considering Degeneracy

Collinearity: Modify the test to “all points to the

right, or on the line” Does this solve the problem?!

11

Degenerate Case (cont)

Consider numerical inaccuracyTwo possibilities:

12

Jarvis’ March (Gift Wrapping)

A modification of naïve algorithmStart 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.

13

Jarvis’ March (cont)

Complexity Crude count: O(n2)

Output sensitive O(nh), h:# of

segments on the hull

Worst case

14

A Better AlgorithmSort 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

16

Step-by-step

1

24

8

6

7

95

3

[1,2]

17

Step-by-step

1

24

8

6

7

95

3

[1,2,3]

18

Step-by-step

1

24

8

6

7

95

3

[1,3]

19

Step-by-step

1

24

8

6

7

95

3

[1,3,4]

20

Step-by-step

1

24

8

6

7

95

3

[1,3,4,5]

21

Step-by-step

1

24

8

6

7

95

3

[1,3,5]

22

Step-by-step

1

24

8

6

7

95

3

[1,3,5,6]

23

Step-by-step

1

24

8

6

7

95

3

[1,3,5,6,7]

24

Step-by-step

1

24

8

6

7

95

3

[1,3,5,7]

25

Step-by-step

1

24

8

6

7

95

3

[1,3,7]

26

Step-by-step

1

24

8

6

7

95

3

[1,3,7,8]

27

Step-by-step

1

24

8

6

7

95

3

[1,3,7,8,9]

28

Step-by-step

1

24

8

6

7

95

3

[1,3,7,9]Upper hull completed

29

Algorithm (cont)

This is a variation of Graham ScanProof of correctness: p.8Complexity analysis: O(n log n) Lexicographical sorting of points: O(n

log n) Computation of lower hull: O(n)

Consider the for and while

30

Consider Degeneracy Again …

Effects on sorting Lexico. ordering

Effects on making turns…

31

Other Version of Graham Scan

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

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

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

32

Graham Scan (ver2)

Also handles collinearity

33

Incremental Algorithm

Suitable for dynamic CGeom and 3D hull

If pnCHn-1,

CHn=CHn-1

ElseFind two tangents, update CHn

Incremental Algorithm

Input: a set of points P in 2DOutput: a list L containing the points of CH(P) in

CCW order1. Take first three points of P to form a triangle2. 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}

34

t1t2

CHi-1

pi

FindTangent (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,pi+1)) Mark pi as the tangent point

(There will be two tangent points)Determine t1, t2

35

t1t2

CHi-1

pi

36

Incremental Algorithm (cont)

Finding Tangents …Analysis

37

Quick HullDiscard points in the extreme quadrilateral

38

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

39

Divide and Conquer Algorithm

Computing 3D hull: Graham scan

(probably not ∵angle sort)

Preparata and Hong (1977)

T(n) = 2T(n/2) + O(n); T(n) = O(n log n)a-1 & a+1 at left side of abb-1 & b+1 at left side of ab

40

Extend to 3D

41

Floating Point Inaccuracies

Handled by Interval arithmeticExact (rational) arithmetic

42

Interval arithmetic

Ref: http://www.eng.mu.edu/corlissg/VC02/READ_ME.html

43

Exact Math

Rational arithmetic (never floating point) In[6]:= 1/12 - 7/9 + 31/36 Out[6]= - 1/6

The Exact Arithmetic Competition: Level 0 TestsRef: XR

44

Exercise

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

)log(log)(

22)2(2)2(2)2(

23)2(2222)2(22)2(

2)2(2)2(

22)2(222)2(22)2(

2)2(2)2(

2)2(2)2(

)log(2

33232

232

2212

121

1

2

nnOncnnnT

kcTmcTT

cTccTT

cTT

cTccTT

cTT

cTT

nkn

kkkkmkmkmk

kkkkkk

kkk

kkkkkk

kkk

kkk

k

cnnTnT )2/(2)( cnnTnT )1()(

)(21

)1()2()3(

)1()2()(

)1()2()1(

2nOnc

cnncncnT

cnncnTnT

ncnTnT

46

More reference on the web.