Skip to content

Instantly share code, notes, and snippets.

@nsmaciej
Created February 18, 2014 18:58
Show Gist options
  • Save nsmaciej/9077457 to your computer and use it in GitHub Desktop.
Save nsmaciej/9077457 to your computer and use it in GitHub Desktop.
Flood fill in Haskell! ^_^
import Data.Array
type Point = (Int, Int)
lo, hi :: Array Point Int -> Point
lo a = fst $ bounds a
hi a = snd $ bounds a
outOfBounds, emptyPoint, takenPoint :: Array Point Int -> Point -> Bool
outOfBounds a (x, y)
| x < 0 || x > fst (hi a) = True
| y < 0 || y > snd (hi a) = True
| otherwise = False
emptyPoint a p = a ! p < 0
takenPoint a p = a ! p > 0
floodFill :: Int -> Point -> Array Point Int -> Array Point Int
floodFill r i a
| outOfBounds a i = a
| emptyPoint a i = a
| takenPoint a i = a
| a ! i == 0 = floodFill r (x+1, y) .
floodFill r (x-1, y) .
floodFill r (x, y+1) $
floodFill r (x, y-1) ua
where (x, y) = i
ua = a // [(i, r)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment