title: Polygon edge gradient algorithm
body: <p>I'm trying to create an application that uses an uncommon polygon gradient fill. The idea is that each edge of the polygon has a single colour, and these edge colours are used to fill the rest of the polygon's pixels in a smooth gradient. </p>
  
  <p>I have a working program that performs this gradient by determining, for each pixel, how far it is from all the polygon's edges and doing a weighted average of all the edges' colours based on the distances to them. Here's a sample output:</p>
  
  <p><img src="https://i.stack.imgur.com/8mB7q.png" alt="Polygon edge gradient sample (click link to view)"></p>
  
  <p>The problem is that the algorithm is horribly slow when the polygon has a lot of edges, namely because for every single pixel, it has to calculate distances to every edge. Any ideas for how this could be sped up? </p>
  
  <p>Current algorithm:</p>
  
  <pre><code>for pixel in polygon: # predetermined using a basic scan-line polygon-fill algorithm
      edge_distances = {}
      for edge in polygon:
          distance = calc_point_to_line_distance(pixel, edge) # simple formula, constant time            
          edge_distances[edge] = distance
      colour = calc_pixel_colour(edge_distances)  # runs in O(num_edges) time
      set_pixel_colour(pixel, colour)
  </code></pre>
  
  <p>I've left out the details because I'm looking for big picture ideas of how to do this better (i.e. better than <code>O(num_pixels*num_edges)</code>). All ideas welcome.</p>
  
  <p>Thanks!</p>