2013-01-18 74 views
4

目前,我正在使用凸包算法從隨機放置的一組點中獲取最外面的點。我想要做的是從凸包返回的一組點中繪製一個多邊形,然而,當我嘗試繪製多邊形時,它看起來很奇怪。如何從一組無序點中繪製多邊形

enter image description here

我的問題,我怎麼訂購點,使多邊形繪製是否正確?

謝謝。

編輯:

另外,我還嘗試使用排序依據(...)排序ThenBy(...),我似乎無法得到它的工作。

回答

2

我有一個問題,其中隨機生成一組點,其中包絡仰角向量需要一個基礎輪廓。已經閱讀@ user1149913提供的鏈接,發現gift-wrapping a hull樣品,下面是我實現的一個樣本:

private static PointCollection CalculateContour (List<Point> points) { 
    // locate lower-leftmost point 
    int hull = 0; 
    int i; 
    for (i = 1 ; i < points.Count ; i++) { 
     if (ComparePoint(points[i], points[hull])) { 
      hull = i; 
     } 
    } 

    // wrap contour 
    var outIndices = new int[points.Count]; 
    int endPt; 
    i = 0; 
    do { 
     outIndices[i++] = hull; 
     endPt = 0; 
     for (int j = 1 ; j < points.Count ; j++) 
      if (hull == endPt || IsLeft(points[hull], points[endPt], points[j])) 
       endPt = j; 
     hull = endPt; 
    } while (endPt != outIndices[0]); 

    // build countour points 
    var contourPoints = new PointCollection(points.Capacity); 
    int results = i; 
    for (i = 0 ; i < results ; i++) 
     contourPoints.Add(points[outIndices[i]]); 
    return contourPoints; 
    }