Notes on Computational Geometry - Data Structure and Algorithms | CS 245, Study notes of Data Structures and Algorithms

Material Type: Notes; Professor: Galles; Class: Data Struct & Algorithms; Subject: Computer Science; University: University of San Francisco (CA); Term: Spring 2009;

Typology: Study notes

Pre 2010

Uploaded on 07/31/2009

koofers-user-84a
koofers-user-84a 🇺🇸

8 documents

1 / 26

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS245-2009S-24 Computational Geometry 1
24-0: Cross Products
Given any two points p1= (x1, y1)and p2= (x2, y2)
Cross Product: p1×p2=x1y2x2y1
p1×p2=x1y2x2y1
=1(x2y1x1y2)
=p2×p1
24-1: Cross Products
p1
p2
p2
p1
p1
2
p
p1p2> 0 p1p2< 0 p1p2= 0
Cross Product is positive if p1is clockwise of p2
Cross Product is negative if p1is counterclickwise of p2
Cross Product is zero if p1and p2are colinear
24-2: Cross Products
Given two vectors that share an origin:
p0p1and
p0p2
Is
p0p2clockwise or counterclockwise relative to
p0p2?
24-3: Cross Products
p
1
p
2
p
0
p
2
p
1
p
0
Counterclockwise Clockwise
24-4: Cross Products
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Notes on Computational Geometry - Data Structure and Algorithms | CS 245 and more Study notes Data Structures and Algorithms in PDF only on Docsity!

24-0: Cross Products

  • Given any two points p 1 = (x 1 , y 1 ) and p 2 = (x 2 , y 2 )
    • Cross Product: p 1 × p 2 = x 1 y 2 − x 2 y 1

p 1 × p 2 = x 1 y 2 − x 2 y 1

= − 1 ∗ (x 2 y 1 − x 1 y 2 )

= −p 2 × p 1

24-1: Cross Products

p 1

p 2

p 2

p 1

p 1

p

p 1

p 2

p 1

p 2

< 0 p 1

p 2

  • Cross Product is positive if p 1 is clockwise of p 2
  • Cross Product is negative if p 1 is counterclickwise of p 2
  • Cross Product is zero if p 1 and p 2 are colinear

24-2: Cross Products

  • Given two vectors that share an origin:
    • −p− 0 →p 1 and −p− 0 →p 2
  • Is −p− 0 →p 2 clockwise or counterclockwise relative to −p− 0 →p 2?

24-3: Cross Products

p

p

p

p

p

p

Counterclockwise Clockwise

24-4: Cross Products

  • Given two vectors that share an origin:
    • −p− 0 →p 1 and −p− 0 →p 2
  • Is −p− 0 →p 2 clockwise or counterclockwise relative to −p− 0 →p 2?
    • (p 1 − p 0 ) × (p 2 − p 0 ) is positive, −p− 0 →p 2 is counterclockwise from −p− 0 →p 1

24-5: Cross Products

  • Given two line segments p 0 p 1 and p 1 p 2 , which direction does angle 6 p 0 p 1 p 2 turn?

p 1

p 0

Left Turn

p 2 p 1 p 0

Right Turn

p 2

24-6: Cross Products

  • Given two line segments p 0 p 1 and p 1 p 2 , which direction does angle 6 p 0 p 1 p 2 turn?
    • (p 2 − p 0 ) × (p 1 − p 0 ) is positive, left turn
    • (p 2 − p 0 ) × (p 1 − p 0 ) is negative, right turn
    • (p 2 − p 0 ) × (p 1 − p 0 ) is zero, no turn (colinear)

24-7: Convex Hull

  • Given a set of points, what is the smallest convex polygon that contains all points
  • Alternately, if all of the points were nails in a board, and we placed a rubber band around all of them, what shape would it form?

24-8: Convex Hull

  • Push points onto a stack
  • Pop off points that don’t form part of the convex hull
  • When we’re done, stack contains the points in the convex hull

24-12: Convex Hull

Gram-Scan

Let p 0 be the point with the minimum y-coordinate Sort the points by increasing polar angle around p 0 Push p 0 , p 1 , and p 2 on the stack S for i ← 3 to n do while angle formed by top two points on S doesn’t turn left do Pop Push(pi) return S

24-13: Graham’s Scan

24-14: Graham’s Scan

p 0

p 1

p 2

p 3

p 4

p 5

p 6

p 7

p 8

p 9

24-15: Graham’s Scan

p 0

p 1

p 2

p 3

p 4

p 5

p 6

p 7

p 8

p 9

Stack

p 0

p 1

p 2

24-16: Graham’s Scan

p 0

p 1

p 2

p 3

p 4

p 5

p 6

p 7

p 8

p 9

Stack

p 0

p 1

p 2

p 4

24-19: Graham’s Scan

p 0

p 1

p 2

p 3

p 4

p 5

p 6

p 7

p 8

p 9

Stack

p 0

p 1

p 2

p 4

p 5

24-20: Graham’s Scan

p 0

p 1

p 2

p 3

p 4

p 5

p 6

p 7

p 8

p 9

Stack

p 0

p 1

p 2

p 4

p 5

p 6

24-21: Graham’s Scan

p 0

p 1

p 2

p 3

p 4

p 5

p 6

p 7

p 8

p 9

Stack

p 0

p 1

p 2

p 4

p 6

24-22: Graham’s Scan

p 0

p 1

p 2

p 3

p 4

p 5

p 6

p 7

p 8

p 9

Stack

p 0

p 1

p 2

p 4

p 6

p 8

24-25: Graham’s Scan

p 0

p 1

p 2

p 3

p 4

p 5

p 6

p 7

p 8

p 9

Stack

p 0

p 1

p 2

p 4

p 6

p 8

p 9

24-26: Graham’s Scan

p 0

p 1

p 2

p 3

p 4

p 5

p 6

p 7

p 8

p 9

Stack

p 0

p 1

p 2

p 4

p 6

p 8

p 9

24-27: Graham’s Scan

p 0

p 1

p 2 p 3 p 4 p 5 p 6

p 7

p 8

Stack

p 0

p 1

p 2

p 3

p 4

p 5

p 6

24-28: Graham’s Scan

p 0

p 1

p 2 p 3 p 4 p 5 p 6

p 7

p 8

Stack

p 0

p 1

p 2

p 3

p 4

p 7

24-31: Graham’s Scan

p 0

p 1

p 2 p 3 p 4 p 5 p 6

p 7

p 8

Stack

p 0

p 1

p 2

p 3

p 7

24-32: Graham’s Scan

p 0

p 1

p 2 p 3 p 4 p 5 p 6

p 7

p 8

Stack

p 0

p 1

p 2

p 7

24-33: Graham’s Scan

  • Time required:
    • O(n lg n) to sort points by polar degree
      • Note that you don’t need to calculate the polar degree, just determine if one vector is clockwise or counterclockwise of another – can be done with a single cross product
    • Each element is added to the stack once, and removed at most once (each taking constant time) for a total time of O(n)
    • Total: O(n lg n)

24-34: Convex Hull

  • Different Convex Hull algorithm
  • Idea:
    • Attach a string to the lowest point
    • Rotate string counterclockwise, unti it hits a point – this point is in the Convex Hull
    • Keep going until the highest point is reached
    • Continue around back to initial point

24-35: Jarvis’s March

24-39: Jarvis’s March

24-40: Jarvis’s March

24-41: Jarvis’s March

24-42: Jarvis’s March

24-43: Jarvis’s March

24-44: Jarvis’s March

24-49: Jarvis’s March

24-50: Jarvis’s March

24-51: Jarvis’s March

24-52: Jarvis’s March

24-53: Jarvis’s March

24-54: Jarvis’s March