문제

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]))
도움이 되었습니까?

해결책

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.

다른 팁

For anyone struggling with how to sort around any point (XX, YY) not only (0,0) the code needs a slight modification.

points.OrderBy(x => Math.Atan2(x.X - XX, x.Y - YY)).ToList();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top