Professional Documents
Culture Documents
Yowzer! Theyre
electrifying!
I hate my job
Geometric Algorithms
Geometric Algorithms
Geometric Algorithms
Geometric Algorithms
c
a
b
counterclockwise (left turn)
Geometric Algorithms
p2
Geometric Algorithms
q2
q1
(p1,q1,p2)
q2
(p1,q1,q2)
p2
(p2,q2,p1)
(p2,q2,q1)
p1
q2
p2
q1
(p1,q1,p2)
(p1,q1,q2)
(p2,q2,p1)
(p2,q2,q1)
p1
Geometric Algorithms
(p1,q1,p2)
q2
p2
(p1,q1,q2)
(p2,q2,p1)
(p2,q2,q1)
p1
p2
q1
q2
(p1,q1,p2)
(p1,q1,q2)
(p2,q2,p1)
(p2,q2,q1)
p1
Geometric Algorithms
q1
p2
q2
(p1,q1,p2)
(p1,q1,q2)
(p2,q2,p1)
p1
(p2,q2,q1)
q1
p1
p2
q2
(p1,q1,p2)
(p1,q1,q2)
(p2,q2,p1)
(p2,q2,q1)
Geometric Algorithms
y3y2
x3x2
y2y1
p2
p1
x2x1
Orientation test
- counterclockwise (left turn): <
- clockwise (right turn): >
- collinear (left turn): =
The orientation depends on whether the expression
(y2y1) (x3x2) (y3y2) (x2x1)
is positive, negative, or null.
Geometric Algorithms
10
Point Inclusion
given a polygon and a point, is the point inside or
outside the polygon?
orientation helps solving this problem in linear time
Geometric Algorithms
11
Geometric Algorithms
12
Geometric Algorithms
13
a
For each point p, compute the angle q(p) of the
segment (a,p) with respect to the x-axis:
(p)
a
Geometric Algorithms
14
a
The question is: how do we compute angles?
- We could use trigonometry (e.g., arctan).
- However, the computation would be inefficient
since trigonometric functions are not in the normal
instruction set of a computer and need a call to a
math-library routine.
- Observation:, we dont care about the actual
values of the angles. We just want to sort by angle.
- Idea: use the orientation to compare angles
without actually computing them!!
Geometric Algorithms
15
p
q
a
(p) < (q) orientation(a,p,q) = CCW
We can sort the points by angle by using any
sorting-by-comparison algorithm (e.g., heapsort
or merge-sort) and replacing angle comparisons with
orientation tests
We obtain an O(N log N)-time algorithm for the
simple closed path problem on N points
Geometric Algorithms
16
repeat
next S.after(curr)
{advance}
if points (point(prev), point(curr), point(next))
make a left turn then
prev curr
else
S.remove(curr) {point curr is on the convex hull}
prev S.before(prev)
curr S.after(prev)
until curr = S.last()
S.remove(S.last())
{remove the copy of a}
Geometric Algorithms
17