Created
February 18, 2014 18:58
-
-
Save nsmaciej/9077457 to your computer and use it in GitHub Desktop.
Flood fill in Haskell! ^_^
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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