Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort 2d points in a list clockwise

Tags:

c#

coordinates

Basically I have a list of points, each with X,Y,Z (Z is always the same).

For example:

pointList.add(Point p = new Point(1, 2, 3));

however I am having trouble sorting them into a clockwise order.

I know the centre and I know the there are roughly 600 points in each list.

I have accomplished this before in Python, but I am having trouble in C#.

Python code:

pointlist.sort(key=lambda c:atan2(c[0], c[1]))
like image 444
bolt19 Avatar asked Mar 20 '23 05:03

bolt19


1 Answers

Not sure if this would accomplish what you need.

points = points.OrderBy(x => Math.Atan2(x.X, x.Y)).ToList();

Not very optimized or anything, just looked at your python code and thought this would accomplish the same.

Note: You may need using System.Linq unless you already have it.

Edit: Sturm pointed out that reversing the order might be necessary to get them 'clock-wise' One way of accomplishing this is using OrderByDescending instead of OrderBy.

like image 66
t0yk4t Avatar answered Mar 28 '23 23:03

t0yk4t