包含多边形点的数组。我们可以遍历边界吗?

时间:2018-11-14 13:14:59

标签: arrays pseudocode

比方说,我们有一个由点(x,y)(tile)填充的数组,所有这些都是伪代码,它们形成一个封闭且填充的2d多边形。我们如何仅通过形成多边形边界的点进行迭代?

1 个答案:

答案 0 :(得分:1)

应用gift-wrapping algorithm(也称为Jarvis行军),它将仅输出形成边界的那些点。

jarvis(S)

    // S is the set of points
    pointOnHull = leftmost point in S // which is guaranteed to be part of the CH(S)

   i = 0
   repeat
      P[i] = pointOnHull
      endpoint = S[0]      // initial endpoint for a candidate edge on the hull
      for j from 1 to |S|
         if (endpoint == pointOnHull) or (S[j] is on left of line from P[i] to endpoint)
            endpoint = S[j]   // found greater left turn, update endpoint
      i = i+1
      pointOnHull = endpoint
   until endpoint == P[0]      // wrapped around to first hull point

enter image description here