In the well known "Introduction to Algorithms - 3rd edition" book the Gift Wrapping Algorithm for finding the Convex Hull of a set of points in 2D space is described as requiring either:
- 2 runs for finding the left and right chain of the convex hull separately
- sorting the polar angles with respect to the last segment of the convex hull
in order to use the Cross Product trick for sorting polar angles and getting correct results.
However, it seems to me that it is enough to consider just the latest point of the convex hull. Here is my approach (assuming no 3 collinear points appear in the input):
pLast = latest found point of the Convex Hull
<p1, p2, ..., pk> = points that are not (yet) in the Convex Hull
pNext = the next point of the Convex Hull (trying to find this)
pNext = p1
for i=2 to k
if (nonLeftTurn(pLast, pNext, pi) == true)
pNext = pi
Here nonLeftTurn(p1, p2, p3) return true if p3 is to the right of oriented line p1->p2:
nonLeftTurn(p1, p2, p3):
if ( (p2 - p1) x (p3 - p1) <= 0)
return true
else
return false
It seems to me that this approach is correct. What am I missing? Could you please provide a counter-example as I'm not able to find one.
Thanks!