2014-07-03 87 views

回答

0

我不知道这是否回答你的问题,但这里有一段代码,我几年前写的。
它计算多边形点列表中的点索引以及垂直于最近边缘的距离。

它使用一个Vector2结构定义矢量运算+-*(点积)和方法GetLengthGetSquaredLength。同样的代码也应该与Vector3(3D)一起运行。

请参阅备注以了解详情。

// Polygon points 
    List<Vector2> p; 

    /// <summary> Calculates the perpendicular vector from nearest point on polygon to given point. If no points available minIndex is -1. </summary> 
    public void GetPolygonDist(Vector2 point, out Vector2 minDist, out int minIndex) { 
     if (p.Count == 0) { minDist = Vector2.Null(); minIndex = -1; return; } 
     Vector2 dist; 
     minDist = point - p[0]; 
     minIndex = 0; 
     for (int i = 0; i < p.Count - 1; i++) { 
      Vector2 l = p[i + 1] - p[i];          // Edge 
      if (l.GetLength() < 1e-9) continue;         // Ignore edge of length almost zero 
      Vector2 d = point - p[i];           // Polygon point to point 
      Vector2 b = (l * d)/(l * l) * l;         // Projection to edge 
      double f = Math.Sign(b * l) * (b.GetLength()/l.GetLength());  // Where on the edge is the perpendicular? 
      if (f < 0.0) dist = point - p[i];         // Point before the edge 
      else if (f > 1.0) dist = point - p[i + 1];       // Point after the edge 
      else dist = d - b;             // Perpendicular 
      if (dist.GetSquaredLength() < minDist.GetSquaredLength()) { 
       minDist = dist; 
       minIndex = i; 
      } 
     } 
    } 
+0

thanx,但我需要一个js –

+1

你没有标记它js。它有点数学,所以把它转换成js应该不是很难。 – joe

相关问题