Search code examples
arrayspseudocode

Array that contains points of polygon. Can we iterate through it's border?


Let's say we have an array filled with points(x,y)(tile), all in pseudocode, that form a closed and filled 2d polygon. How can we iterate only through the points that form the polygon's border?


Solution

  • Apply the gift-wrapping algorithm also known as the Jarvis march, which will output only those points that form the border.

    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