In the Gift Wrap algorithm we need to determine if a point is to the left of the line through two other points. In the Incremental algorithm we need to determine if a point can see an edge.
Actually, both of these tests are the same and we can implement
them by calculating the area of a triangle.
The area of the triangle
P0P1P2
where
P0=(x0,y0),
P1=(x1,y1) and
P2=(x2,y2) is
If the area is negative then they are in clockwise order. If the convex hull is kept in anti-clockwise order and P0P1 is a hull edge this means that P2 can see P0P1 (the test required by the Incremental algorithm).
Finally, if the area is zero the three points are collinear.
You can move a point in the triangle below by dragging it with the mouse and see how the area changes. If the area is positive the triangle is coloured black, if it is negative then is colored red.
Note that since we only care whether the area is positive, negative or zero it
is only necessary to calculate
This approach generalises to higher dimensions.
The volume of the tetrahedron
P0P1P2P3where
P0=(x0,y0,z0),
P1=(x1,y1,z1),
P2=(x2,y2,z2) and
P3=(x3,y3,z3) is
If this volume is positive then the triangle P0P1P2 is anti-clockwise when viewed from the side away from P3. Another way to say this is that the normal to P0P1P2 determined by the right-hand rule (curl you fingers from P0 to P1 P2, your thumb shows the direction of the normal) points out from the tetrahedron.
If the volume is negative then the normal to P0P1P2 points into the tetrahedron. By convention, we store polyhedra with outward normals. This means that if P0P1P2 is any triangle of the convex hull and P3 is any other vertex, we want the volume of P0P1P2P3 to be positive. So, testing the sign of the volume is just the test needed for the 3D Gift Wrap algorithm.
The same volume test also allows us to determine if P3 can see P0P1P2, the test needed by the Incremental algorithm.
Similarly, in 4D we must compute a 5x5 determinant, and so on.