Skip to content

Instantly share code, notes, and snippets.

@charlespunk
Created February 22, 2013 19:56
Show Gist options
  • Save charlespunk/5016116 to your computer and use it in GitHub Desktop.
Save charlespunk/5016116 to your computer and use it in GitHub Desktop.
Implement the "paint fill" function that one might see on many image editing programs. That is, Given a screen(represented by a two-dimensional
array of colors), a point, and a new color, fill in the surrounding area until the color changes from the original color.
enum Color{
Black, White, Red, Yellow, Creen
}
public static void paint(int[][] screen, int x, int y, Color originalColor, Color updatedColor){
if(x < 0 || y < 0 || x >= screen[0].length || y >= screen.length) return;
if(screen[x][y] == originalColor){
screen[x][y] = updatedColor;
paint(screen, x + 1, y, originalColor, updatedColor);
paint(screen, x - 1, y, originalColor, updatedColor);
paint(screen, x, y - 1, originalColor, updatedColor);
paint(screen, x, y + 1, originalColor, updatedColor);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment