# Sorting coplanar vertices clockwise/counterclockwise

This topic is 4904 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I have just come up with a problem: how can I sort a set of coplanar vertices clockwise. At the moment I don't know if I need clockwise or counterclockwise but I guess there is little difference. Thanks,999999999

##### Share on other sites
Do the vertices form a convex polygon?

Yes, they do.

##### Share on other sites
Pick an arbitrary point on the polygon and walk around the edges...

Or if you don't know what the edges are, use a convex hull algorithm to create the convex polygon structure from the points. Convex hull algorithms calculate the edges of the convex hull.

##### Share on other sites
There may be a better way, but here's how I'd do it.

1. Find the center as the average of the vertex set.

2. Choose a vertex arbitrarily and make a reference vector as:

ref = vertex[0] - center;

3. For each of the other vertices find the unsigned angle to the reference vector as:

diff = vertex - center;
angle = atan2(length(cross(diff,ref)), dot(diff,ref));

4. Find the sign of the angle by dotting cross(diff,ref) with the plane normal.

5. Sort the vertices by the signed angle.

Let me know is that needs any clarification.

##### Share on other sites
If you already know the points form a convex polygon, you don't need a convex hull algorithm.

##### Share on other sites
Thank you very much guys, now I can't try it but tomorrow I'll see what happens (I am not completely sure that I need that kind of sorting but who knows)

##### Share on other sites
I have solved my problem! It was not a matter of vertex order, well it was but not sorted cw / ccw ! Anyway thank you very much.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 13
• 9
• 15
• 14
• 46
• ### Forum Statistics

• Total Topics
634059
• Total Posts
3015295
×