Information Processing Letters 25 (1987) 11-12 20 April 1987
North-Holland
ON-LINE CONSTRUCTION OF THE CONVEX HULL OF A SIMPLE POLYLINE
Avraham A. M E L K M A N *
Ben Gun'on University of the Negev, Beer Sheva, Israel
Communicated by Alan Shaw
Received 2 April 1985
Revised 27 November 1985 and 9 September 1986
Keywords: Convex hull, simple polygon, analysis of algorithms
1. Introduction and description of the algorithm be given by an input list of its vertices as they are
encountered in an ordered traversal, and the al-
After McCallum and Avis [4] showed that the gorithm will consider the vertices in that order.
convex hull of a simple polygon P with n vertices For an ordered triple of points x, y, z, the function
can be constructed in O(n) time, several authors (x, y, z) assumes values 1, 0, or - 1 depending on
[1,2,3] devised simplified algorithms for this prob- whether z is to the right of, collinear with, or to
lem. Graham and Yao [2] presented a particularly the left of the directed line from x to y.
simple and elegant one. After finding two points The main data structure used is a deque of
of the convex hull, their algorithm generated all vertices, D = ( d b , . . . , d t ) , where the variable b
other hull vertices using only one stack for inter- points to the bottom of the deque and t to its top,
mediate storage. It is the purpose of this short and db and d t will always refer to the same vertex.
article to show that a slightly modified version of Thus, D describes a closed curve; the polygon
their algorithm constructs, on-line, the convex hull enclosed by this curve will also be referred to as
of any simple polyline in O(n) time. In contrast, D. Variable v refers to the input vertex under
the on-line construction of a nonsimple polyline consideration. 'Pushing v' means executing It
requires O(n log n) time, as shown by Preparata t + 1 ;dt ~ v], 'popping dt' means setting [t
[5]. In the special case of a simple polygon our t - 1], 'inserting v' means executing [b ,,-- b - 1 ;
algorithm produces the convex hull without first db *- v], and 'removing db' means setting [b
identifying two of the hull vertices, as was re- b + 1]. The algorithm halts when its input is ex-
quired in [2]. The price that we pay is the use of a hausted.
deque instead of a queue. After this article was
submitted, we learned of a similar approach taken Algorithm Hull
by Tor and Middleditch [6], who embed it in an 1. t ( - - - 1 ; b ( - - 0 ;
algorithm for the convex decomposition of .a sim- vl ,-- input; v2 ' - input'; v3 ' - input;
ple polygon. if (Vl, v2, v3) > 0
To keep this article short, we use where possi- then begin push vl; push v2 ; end
ble the definitions and notations of Graham and else begin push v2; push v 1 ; end
Yao. The polyline P = (vl,... , Vm) is assumed to push v3 ; insert v3;
2. v (-- input;
* Present affiliation: Department of Computer Science, Col- until (v, db, d b + l ) < b or (dt_ 1, d t , v) < 0
umbia University, New York, NY 10027, U.S.A. do v ~ input end;
0020-0190/87/$3.50 © 1987, Elsevier Science Publishers B.V. (North-Holland) 11
Volume 25, Number 1 INFORMATION PROCESSING LETTERS 20 April 1987
3. until ( d t _ l , d t , v) > 0 do p o p d t end; 3 and 4 are executed immediately after v is read
push v; and before the next vertex is read.
4. until (v, db, d b + l ) > 0 do r e m o v e d b end; Suppose that, before execution of steps 3 and 4,
insert v; the deque is D = ( d b , . . . , d t ) and that after their
goto 2. execution the deque is D ' = ( d k , . . . , d m ) , with
v = d k = d m" TO prove D ' to be convex we show
that it describes a closed simple curve and that
2. Correctness of Algorithm Hull (di, di+l, di+2) > 0, i = k . . . . , m - 2, and
(d m- 1, d m, d k÷ 1) > 0. Because D is simple, D '
In this section we prove the following theorem. can only be nonsimple if the edges (v, d k÷ 1) and
(dm_l, v) intersect edges of D; this cannot be the
Theorem. Algorithm HuH finds the convex hull of P case since the polyline P is simple. Now,
correctly and m linear time. (di, di+l, d i + 2 ) > 0 , i = k , . . . , m - 2 , is either al-
ready true for D or else it is the result of steps 3
Proof. Evidently, the algorithm is linear in the and 4. Suppose then by contradiction that
number of vertices of P, as each vertex is pushed (dm_l, dm, d k + 0 < 0 . Then, v = d m is on or to
(inserted) at most once and popped (removed) at the right of the directed line from d m_ 1 to d k+ 1,
most once. We use the following characterization and at the same time it must be to the right of the
of the convex hull H of P: polyline ( d k + l , . . . , d i n + l ) . Thus, v is within D, a
(i) H is convex, contradiction.
(ii) P is contained in H, Finally, it is easily seen by induction that P is
(iii) the set of vertices of H is a subset of the set contained in D', because it is contained in D and
of vertices of P. each time a vertex dj is popped, the polygon
The last property is built in to the algorithm. (v, d i , . . . , d j, v) is contained withix~_ the polygon
Thus, we will prove by induction that the follow- (v, d i , . . . , d j + l , v); similarly, the removal of a
ing hypothesis is true each time a new vertex is vertex only enlarges the polygon. []
read in step 2:
(H) The deque D = ( d b , . . . , d t ) , considered as a Acknowledgment
polygon, is convex and contains that part of
the polyline P seen so far. Karl Abrahamson and David Kirkpatrick de-
vised a similar algorithm as part of their refer-
First of all, observe the following.
eeing. The helpful suggestions of an anonymous
referee, which simplified the algorithm and its
Claim. All vertices rejected in step 2 are in, or on,
proof, are also gratefully acknowledged.
the current convex hull contained in D.
Indeed, if a vertex v is rejected, it is because References
b o t h (db, d b + l , v) ~ 0 a n d ( d t _ l , d t , v) >i 0, i.e., v
is to the right of both the edges (dr_ 1, d t ) and [1] B.K. Bhattacharya and H. E1 Gindy, A new linear convex
(db, db+l). Moreover, the polyline connects v hull algorithm for simple polygons, IEEE Trans. Inform.
with d t and, because it is simple, does not cross Theory IT-30 (1984) 85-88.
[2] R.L. Graham and F.F. Yao, Finding the convex hull of a
the part of the polyline connecting db+ 1 and dr_ 1.
simple polygon, J. Algorithms 4 (1984) 324-331.
Hence, v must lie within the convex polygon de- [3] D.T. Lee, On finding the convex huff of a simple polygon,
scribed by D. Internat. J. Comput. Inform. Sci. 12 (1983) 87-98.
Step 1 ensures that (H) holds when three vertices [4] D. McCalhim and D. Avis, A linear algorithm for finding
have been read (assuming they axe not collinear). the convex hull of a simple polygon, Inform. Process. Lett.
9 (1979) 201-206.
Suppose then inductively that (H) holds when k
[5] F.P. Preparata, An optimal real time algorithm for planar
vertices have been read. Because of the Claim it convex hulls, Comm. ACM 22 (1979) 402-405.
may be assumed that v satisfies either (db, db+l, [6] S.B. Tot and A.E. Middleditch, Convex decomposition of
V) < 0 or (d t_ 1, d t, v) < 0 (or b o t h ) , a n d that steps simple polygons, ACM Trans. Graphics 3 (1984) 244-265.
12