2013-01-18 85 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; 
    }