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.
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:
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?
Current algorithm:
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)
I've left out the details because I'm looking for big picture ideas of how to do this better (i.e. better than O(num_pixels*num_edges)
). All ideas welcome.
Thanks!