Skip to content

Instantly share code, notes, and snippets.

@wbadart
Last active October 4, 2016 03:51
Show Gist options
  • Save wbadart/d2b5c1cddc7f51b40dabbdbf34a57c96 to your computer and use it in GitHub Desktop.
Save wbadart/d2b5c1cddc7f51b40dabbdbf34a57c96 to your computer and use it in GitHub Desktop.
Simulate the "fill color" operation of programs like MS with lists.
'''
' paint.py
'
' Simulate the "fill color" operation of
' programs like MS with lists.
'
' Will Badart
' SEP 2016
'
'''
def paint(matrix, color, x, y):
current_color = matrix[x][y]
matrix[x][y] = color
if x - 1 >= 0 and matrix[x - 1][y] == current_color:
paint(matrix, color, x - 1, y)
if x + 1 < len(matrix) and matrix[x + 1][y] == current_color:
paint(matrix, color, x + 1, y)
if y - 1 >= 0 and matrix[x][y - 1] == current_color:
paint(matrix, color, x, y - 1)
if y + 1 < len(matrix[x]) and matrix[x][y + 1] == current_color:
paint(matrix, color, x, y + 1)
canvas = [[1, 1, 1, 1, 1],
[1, 2, 1, 1, 2],
[1, 2, 2, 1, 1],
[1, 1, 1, 1, 1]]
def print2d(matrix):
for row in matrix:
print row
print 'Original canvas:'
print2d(canvas)
paint(canvas, 3, 1, 1)
print 'Painted canvas:'
print2d(canvas)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment