2016-03-03 64 views
-4

我有一個我想排序的點的列表。根據函數的C#排序列表

產生的排序順序應該根據函數的輸出,該函數將列表中的一個點與兩個固定點一起作爲輸入。

是決定排序的功能是這樣的:

public static double GetAngle(Point P1, Point P2, Point P3) 
    { 
     Point Vec1 = P1.Vector(P2); 
     Point Vec2 = P1.Vector(P3); 
     double angle = Math.Atan2(CrossProduct(Vec1, Vec2), Dot(Vec1, Vec2)) * 180/Math.PI; 
     if (angle < 0) 
      angle += 360; 
     return angle; 
    } 

其中P1是從列表和P2和P3固定點我的輸入。

如何結合GetAngle()函數和兩個固定點來正確排序列表?

+3

祝你好運!你有問題嗎? –

+0

有些關於名單和那些'其他特定點'的信息將是必需的。他們是否也在名單上? –

+0

另外你已經試過這個功能來獲得角度? – Blackunknown

回答

1

如果我正確理解你的問題:你點的名單,我會打電話給他們List<Point> myListOfPoints

那麼我想你的問題是如何獲得這些點按排序他們的輸出來自MyFunc方法。輸出是列表中單個點的相對角度,以及兩個固定點,我將這個例子稱爲myFixedPoint1myFixedPoint2

myListOfPoints.Sort((a,b) => MyFunc(a, myFixedPoint1, myFixedPoint2).CompareTo(MyFunc(b, myFixedPoint1, myFixedPoint2))); 

或者通過部分採用固定點來創建一個排序函數接受單個參數:

var sortFunc = new Func<Point, double>(x => MyFunc(x, myFixedPoint1, myFixedPoint2)); 
myListOfPoints.Sort((a,b) => sortFunc(a).CompareTo(sortFunc(b)));