Convex Hull - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Convex Hull

Description:

Is C counterclockwise as seen going from A to B? A. B. C. Useful code ... Use min and max points in x and y; sweep down from the top. Divide and conquer ... – PowerPoint PPT presentation

Number of Views:305
Avg rating:3.0/5.0
Slides: 28
Provided by: JFH
Category:
Tags: convex | hull

less

Transcript and Presenter's Notes

Title: Convex Hull


1
Convex Hull
  • 3/19/09

2
Warmup
  • Points in the plane P (x, y)
  • in code P.x, P.y
  • Vectors differences between points
  • v Q P means v is what youd add to P to
    get to Q
  • Computationally vx Qx Px sim for y.
  • Notation written vertically
  • x
  • y



3
More vectors
  • Vertical notation is a pain instead we write
  • v x yt the small t means transpose or
    flip over to make this vertical.
  • Can add, subtract pairs of vectors
  • Can add vectors to points
  • Can rotate a vector 90 degrees counterclockwise
  • if v x yt, then vr -y xt
  • is 90 degrees CCW from v

4
More Vectors
  • Can use vectors to compare directions.
  • If v a bt and w c dt, then v . w is
    defined to be ac bd.
  • v . w is positive if and only if v and w point in
    the same half-plane (i.e., angle between them is
    less than 90 degrees)
  • v . w 0 if either v or w is zero, or if theyre
    perpendicular

5
Useful code
  • Is C counterclockwise as seen going from A to B?

C
A
B
6
Useful code
(B-A)r
  • Is C counterclockwise as seen going from A to B?
  • Yes, if (C B) . (B A)r gt 0.

C
A
B
7
CCW(A, B, C)
  • Assumes that A, B, C distinct
  • boolean CCW(Point A, Point B, Point C)
  • Vector v new Vector(B.x A.x, B.y A.y)
  • Vector w new Vector(C.x B.x, C.y B.y)
  • return (Dot(Perp(v), w) gt 0)

8
More utility code
  • Represent a polygon as a sequence of points
  • First and last points NOT equal (i.e., we dont
    duplicate the last point)
  • Could have made opposite choice

9
Inside testing
  • Assumes no two points the same, no three
    collinear
  • boolean Inside(Point Q, Polygon P)
  • boolean inside false
  • for each edge E (A, B) of P
  • if (Q.x between A.x and B.x)
  • inside NOT(inside)
  • return inside
  • Observation this is O(k), where the polygon has
    k vertices.

10
Convex Hull Approaches
  • Brute force
  • Use min and max points in x and y sweep down
    from the top
  • Divide and conquer
  • Incremental (really just brute force)
  • Angular sweep

11
Brute Force
  • for all algorithms special cases for n 1, 2,
    3
  • boolean BFHull(Points P)
  • hull hull for first 3 points.
  • for Q in P
  • if Q inside hull, do nothing
  • else add Q to hull
  • return hull
  • Observation takes at least n2 ops in worst case,
    even ignoring add Q to hull. Inside-testing is
    n ops, repeated about n times.

12
Divide and Conquer
  • Divide points into two piles (split at some
    x-coordinate)
  • Run CHull on each pile
  • Merge together somehow

13
(No Transcript)
14
(No Transcript)
15
(No Transcript)
16
Fix Hull
  • Need to get rid of all reflex nodes
  • Polygon FixHull(Polygon H, Position P)
  • input a polygon H and a position in the
    polygon P the polygon may have reflex vertices
  • output the polygon with all reflex vertices
    removed.
  • Position Q P
  • P P.next()
  • while P ltgt Q
  • S P.next()
  • if not CCW(P.prev(), P, P.next())
  • remove P from H
  • P S
  • if not CCW(P.prev(), P, P.next())
  • remove P from H
  • return H

17
Divide And Conquer
  • Divide into two sets of vertices
  • Find convex hull for each
  • Splice together at rightmost/leftmost nodes of
    left and right sub-hulls
  • FixHull to get rid of reflex vertices.

18
Problems
  • Bad splitting need to divide approximately in
    half to get efficiency
  • Could sort by x and split evenlybut that adds an
    O(n log n) term
  • Could compute average x and split there
  • very bad idea

19
Max/Min sweep
  • Sounds promising
  • Amazingly messy and error prone
  • Tons of special cases
  • Used to be most-hated CS16 project

20
Angular sweep
  • Find a center point somehow
  • Order all other points by angle relative to
    center point
  • Add them in angular order

21
(No Transcript)
22
Finding Angles
  • Angle for ray from P to Q
  • v Q P
  • angle atan2(v.y, v.x)
  • Atan2 returns values from pi to pi
  • We prefer 0 to 360 (easier to read!)
  • angle (180/pi) atan2(v.y, v.x)
  • if (angle lt 0) angle 360

23
Building the CHULL incrementally
  • If next point inside CHULL do nothing
  • Else
  • add next point to hull (just after last one!)
  • fixHull backwards from here

24
Building the CHULL incrementally
  • Analysis FixHull could do k fixes on the kth
    point
  • Sounds like O(n2)

25
Observations
  • Any point can be inserted at most once, and
    deleted at most once!
  • When fix-hull reaches a vert that doesnt need
    fixing, it can stop
  • With O(n) insertions, and O(n) deletions, sounds
    like O(n)!

26
Sanity check
  • For those insertionswe need to check whether the
    new point is insidecould be O(n)
  • But its not! The new point cant possibly be
    inside!

27
Sanity Check 2
  • How do we process the points in angular order?
  • We have to sort firsttakes n log n time.
  • Summary n log n algorithm for CHULL
Write a Comment
User Comments (0)
About PowerShow.com