How to determine if a list of polygon points are in clockwise order?
Some of the suggested methods will fail in the case of a non-convex polygon, such as a crescent. Here's a simple one that will work with non-convex polygons (it'll even work with a self-intersecting polygon like a figure-eight, telling you whether it's mostly clockwise).
Sum over the edges, (x2 − x1)(y2 + y1). If the result is positive the curve is clockwise, if it's negative the curve is counter-clockwise. (The result is twice the enclosed area, with a +/- convention.)
point[0] = (5,0) edge[0]: (6-5)(4+0) = 4
point[1] = (6,4) edge[1]: (4-6)(5+4) = -18
point[2] = (4,5) edge[2]: (1-4)(5+5) = -30
point[3] = (1,5) edge[3]: (1-1)(0+5) = 0
point[4] = (1,0) edge[4]: (5-1)(0+0) = 0
---
-44 counter-clockwise
Find the vertex with smallest y (and largest x if there are ties). Let the vertex be A
and the previous vertex in the list be B
and the next vertex in the list be C
. Now compute the sign of the cross product of AB
and AC
.
References:
How do I find the orientation of a simple polygon? in Frequently Asked Questions: comp.graphics.algorithms.
Curve orientation at Wikipedia.