Return to Snippet

Revision: 41836
at February 23, 2011 21:34 by ole_mn


Updated Code
def floodfill( x,y,array, pixels_x, pixels_y):
    if x < 0 or x > len(array)-1:
        return
    if y < 0 or y > len(array[x])-1:
        return
    if array[x][y] == 1:
        return
    for i in range(0,len(pixels_x)):
        if  pixels_x[i] == x and pixels_y[i] == y :
            return
    pixels_x.append(x)
    pixels_y.append(y)
    floodfill(x+1,y,array,pixels_x,pixels_y)
    floodfill(x-1,y,array,pixels_x,pixels_y)
    floodfill(x,y+1,array,pixels_x,pixels_y)
    floodfill(x,y-1,array,pixels_x,pixels_y)
    return

pixels_x = []
pixels_y = []

array = [[0,0,0,0,0,0,0],
         [0,0,0,1,0,0,0],
         [0,0,1,0,1,0,0],
         [0,1,0,0,0,1,0],
         [1,0,0,0,1,0,0],
         [0,1,0,0,0,1,0],
         [0,0,1,0,1,0,0],
         [0,0,0,1,0,0,0]]

floodfill( 3,3, array, pixels_x, pixels_y )
for i in range(0,len(pixels_x)):
    print "Pixel: " + str(pixels_x[i]) + "," + str(pixels_y[i])

Revision: 41835
at February 23, 2011 21:14 by ole_mn


Initial Code
def floodfill( x,y,array, pixels_x, pixels_y):
    if x < 0 or x > len(array)-1:
        return
    if y < 0 or y > len(array[x])-1:
        return
    if array[x][y] == 1:
        return
    if x in pixels_x and y in pixels_y:
        return
    pixels_x.append(x)
    pixels_y.append(y)
    floodfill(x+1,y,array,pixels_x,pixels_y)
    floodfill(x-1,y,array,pixels_x,pixels_y)
    floodfill(x,y+1,array,pixels_x,pixels_y)
    floodfill(x,y-1,array,pixels_x,pixels_y)
    return

pixels_x = []
pixels_y = []

array = [[0,0,0,0,0,0,0],
         [0,0,0,1,0,0,0],
         [0,0,1,0,1,0,0],
         [0,1,0,0,0,1,0],
         [1,0,0,0,1,0,0],
         [0,1,0,0,0,1,0],
         [0,0,1,0,1,0,0],
         [0,0,0,1,0,0,0]]

floodfill( 3,3, array, pixels_x, pixels_y )
for i in range(0,len(pixels_x)):
    print "Pixel: " + str(pixels_x[i]) + "," + str(pixels_y[i])

Initial URL

                                

Initial Description
A short snippet to demonstrate how to mark pixels inside e.g.a circle.
Note that checking if a pixel was already added or not would be done faster using as input to the floodfill function a duplicate array of the pixel data and simply setting any added pixels to one.

Initial Title
Flood Fill in Python

Initial Tags

                                

Initial Language
Python