Skip to content

Instantly share code, notes, and snippets.

@wuchengwei
Last active January 16, 2019 03:33
Show Gist options
  • Save wuchengwei/7fc521afab1ddfc4186d615ca7df8ca1 to your computer and use it in GitHub Desktop.
Save wuchengwei/7fc521afab1ddfc4186d615ca7df8ca1 to your computer and use it in GitHub Desktop.
判断一个平面多边形顶点列表方向是顺时针还是逆时针
https://stackoverflow.com/a/1165943
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment