# Sorting coplanar vertices clockwise/counterclockwise

This topic is 4847 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.

1. 1
Rutin
69
2. 2
3. 3
4. 4
5. 5

• 21
• 10
• 33
• 20
• 9
• ### Forum Statistics

• Total Topics
633421
• Total Posts
3011794
• ### Who's Online (See full list)

There are no registered users currently online

×