I am creating a simple game in Pygame for my kids. It involves intersecting random complex curves. Some of the intersections can be coloured in different colours. I have implemented a flood fill algorithm which works well for handling areas created by intersecting complex curves, but is very slow.
Couple of questions:
Is there a built-in flood fill algorithm I should be using (I could not find one) that can handle the above use case?
If not, then outside of algorithm improvements, how could I dramatically speed up a flood fill to make it near instantaneous to the human eye? (I am thinking about going low level but not sure how to go about this)
Flood Fill code:
# replaces all points of same starting colour,
# with a new colour, up to a border with
# different starting colour
def do_flood_fill(surface, x, y, newColor):
theStack = [(x, y)]
oldColor = surface.get_at((x,y)) # Get starting colour
while len(theStack) > 0:
x, y = theStack.pop()
if surface.get_at((x,y)) != oldColor:
continue
surface.set_at((x,y),newColor)
# pygame.display.update() # Show fill - very slow
theStack.append( (x + 1, y) ) # right
theStack.append( (x - 1, y) ) # left
theStack.append( (x, y + 1) ) # down
theStack.append( (x, y - 1) ) # up