Are points of a triangle in clockwise order?

Doing some research on triangulation algorithms I found an interesting code snipped that I’d like to share with you. Its intention is to determine if the three points of a triangle are in (counter-)clockwise order.

double Math::crossProductZ(const Vector2 &a, const Vector2 &b) {
	return a.x * b.y - a.y * b.x;
}
 
// If orientation is positive, if a->b->c is counterclockwise.
double Math::orientation(const Vector2 &a, const Vector2 &b, const Vector2 &c) {
        return Math::crossProductZ(a, b) +
               Math::crossProductZ(b, c) +
               Math::crossProductZ(c, a);
}

Vector2 is a simple class with x and y coordinates. The method crossProductZ calculates the z component of the cross product of both Vector2s while orientation sums them up. If orientation returns a positive result the points are sorted counterclockwise, otherwise clockwise.

This method is helpful if you write code for different game engines which require an opposite order of indices to set the orientation (the normal) of each triangle in a mesh.

Leave a Reply

Your email address will not be published. Required fields are marked *

*