0

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:

  1. 2 runs for finding the left and right chain of the convex hull separately
  2. 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!

  • Just to make sure I understand you correctly: you want to skip the part where you sort the points, either by angles (for the single-pass version) or from left to right (for the two-pass version)? Also, where are you storing the points in the convex hull? I don't see this in your code. – Jordi Vermeulen May 17 '15 at 08:34
  • At which moment you update pLast? Your algorithm is a little confusing, I can see how you construct the convex hull here. It looks like if the last pNext is the one that you will take as the point next to pLast, I can see several ways this could go wrong. Are p1,p2,...,pk already sorted? Could you please show the complete algorithm? – Javier Cano May 18 '15 at 14:30
  • Nevermind my previous comment, I was confused with Graham scan algorithm, that's why it didn't made sense to me. – Javier Cano May 18 '15 at 15:04

1 Answers1

0

The approach is correct, you don't need the last segment of the convex hull. You need to find the "rightmost" segment that you can form with pLast, that means that you need that all the remaining points will be on the left of that segment, such a segment is clearly part of the convex hull, and your procedure is doing precisely that. You can picture it as follows, you take any point p and join it with pLast, then you verify that all remaining points lie to its left, if there is any other point q to its right, then you take q instead of p, notice that you don't need to check again all points you already checked for p, since all points to the left of pLast->p are also to the left of pLast->q, so then you continue checking q with the remaining points. The previous observation implies that when you finished checking all points, all remaining points lie to the left of pLast->pNext, and thus pNext is a point in the convex hull.

Jarvis march objective is to find such rightmost segment, and that is the way the algorithm is designed, the way you find such rightmost segment could differ, and I suppose the book's scope is not that thorough to show all possible implementations, so I guess it only suggest these two since it is not a computational geometry book.

Javier Cano
  • 611
  • 3
  • 8