Docsity

Data Structures and Algorithms: Cross Products, Convex Hull, and Closest Pair of Points - , Study notes of Data Structures and Algorithms

Partial preview of the text

Download Data Structures and Algorithms: Cross Products, Convex Hull, and Closest Pair of Points - and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Data Structures and Algorithms

CS245-2009S-

Computational Geometry

David Galles

Department of Computer Science

University of San Francisco

Cross Products

Given any two points

p

1

x

, y 1

1

and

p

2

x

2

, y

Cross Product:

p

1

×

p

2

x

y 1

2

x

2

y

1

p

1

×

p

2

x

1

y

2

x

y 2

1

x

y 2

1

x

y 1

p

2

×

p

1

Cross Products

Given two vectors that share an origin:

p

p 0

1

and

p

0

p

2

Is

−→p^0

p

2

clockwise or counterclockwise relative to

p

p 0

2

?

24-3:

Cross Products

p^1

p^2

p^0

p^2

p^1

p^0

Counterclockwise

Clockwise

Cross Products

Given two line segments

p

p 0

1

and

p

1

p

2

, which

direction does angle

p

p 0

p 1

2

turn?

p^1

p^0

Left Turn

p^2

p^1

p^0 Right Turn

p^2

Cross Products

Given two line segments

p

p 0

1

and

p

1

p

2

, which

direction does angle

p

p 0

p 1

2

turn?

p

2

p

×

p

1

p

0

is positive, left turn

p

2

p

×

p

1

p

0

is negative, right turn

p

2

p

×

p

1

p

0

is zero, no turn (colinear)

Convex Hull

Convex Hull

Convex Hull

Graham’s Scan Algorithm

Go through all the points in order Push points onto a stack Pop off points that don’t form part of the convexhull When we’re done, stack contains the points inthe convex hull

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(

p

)i

return

S

Graham’s Scan

 - p - p - p - p - p - p 
  • p - p
    • p - p - p Graham’s Scan - p - p - p
      • p
        • p
  • p - p
    • p - p - p^2 p^1 p Stack - p Graham’s Scan - p - p - p
      • p
        • p
  • p - p
    • p - p - p^3 p^2 p^1 p Stack - p Graham’s Scan - p - p - p
      • p
        • p
  • p - p
    • p - p - p^4 p^3 p^2 p^1 p Stack - p Graham’s Scan - p - p - p
      • p
        • p
  • p - p
    • p - p - p^4 p^2 p^1 p Stack