Turning and orientations:
Before proceeding with the presentation of the algorithm, we should first make a short
digression to discuss the meaning of “lefthand turn.” Given an ordered triple of points
(
p, q, r
)
in the plane,
we say that they have
positive orientation
if they define a counterclockwise oriented triangle (see Fig. 12(a)),
negative orientation
if they define a clockwise oriented triangle (see Fig. 12(b)), and
zero orientation
if they are
collinear, which includes as well the case where two or more of the points are identical (see Fig. 12(c)). Note
that orientation depends on the order in which the points are given.
(a)
(b)
(c)
p
q
r
p
q
r
p
q
r
p
=
r
q
orient(
p,q,r
)
>
0 orient(
p,q,r
)
<
0
orient(
p,q,r
)=0
Fig. 12: Orientations of the ordered triple
(
p, q, r
)
.
Orientation is formally defined as the sign of the determinant of the points given in homogeneous coordinates,
that is, by prepending a 1 to each coordinate. For example, in the plane, we define
Orient
(
p, q, r
) =
det
1
p
x
p
y
1
q
x
q
y
1
r
x
r
y
.
Lecture Notes
12
CMSC 754
Observe that in the 1dimensional case, Orient
(
p, q
)
is just
q
−
p
. Hence it is positive if
p < q
, zero if
p
=
q
, and
negative if
p > q
. Thus orientation generalizes
<,
=
, >
in 1dimensional space. Also note that the sign of the
orientation of an ordered triple is unchanged if the points are translated, rotated, or scaled (by a positive scale
factor). A reflection transformation, e.g.,
f
(
x, y
) = (
−
x, y
)
, reverses the sign of the orientation. In general,
applying any affine transformation to the point alters the sign of the orientation according to the sign of the
matrix used in the transformation.
Given a sequence of three points
p
,
q
,
r
, we say that the sequence
(
p, q, r
)
makes a (strict)
lefthand turn
if
Orient
(
p, q, r
)
>
0
.
Graham’s algorithm continued:
Let
p
i
denote the next point to be added in the lefttoright ordering of the points
(see Fig. 13(a)). If the triple
(
p
i
, H
[
top
]
, H
[
top
−
1]
)
forms a strict lefthand turn, then we can simply push
p
i
onto the stack. Otherwise, we can infer that the middle point of the triple
H
[
top
]
cannot be on the upper hull,
and so we pop it off the stack. We repeat this until reaching a positively oriented triple (see Fig. 13(b)), or there
are fewer than two elements on the stack. The popping process ends when
p
i
’s predecessor on the stack is its
predecessor on the convex hull (see Fig. 13(c)). The algorithm is presented in the code block below.
(b)
p
i
p
j
(c)
pop
p
i
p
j
(a)
processing
p
i
after adding
p
i
p
i
p
j
pop
before adding
p
i
Fig. 13: Graham’s scan.
Graham’s Scan
(1) Sort the points according to increasing order of their
x
coordinates, denoted
(
p
1
,p
2
,...,p
n
)
.
(2) push
p
1
and then
p
2
onto
H
.
(3) for
i
←
3
,...,n
do:
(a) while (

H
≥
2
and Orient
(
p
i
,H
[
top
]
,H
[
top
−
1])
≤
0
) pop
H
.
(b) push
p
i
onto
H
.
Correctness:
Why is Graham’s algorithm correct? We can show inductively that the contents of
H
at any stage of
the algorithm constitute the upper hull of the points that have been processed so far. For the induction basis
(
H
=
{
p
1
, p
2
}
) this is trivially true. For the induction step, observe that
p
i