Created
November 15, 2015 12:45
-
-
Save rinx/b472e5b8bc0d44a3b40a to your computer and use it in GitHub Desktop.
draw mandelbrot set as ascii art
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.Complex | |
maxIter :: Int | |
maxIter = 127 | |
magnification :: Double | |
magnification = 10.0 | |
xrange :: [Int] | |
xrange = [-30..30] | |
yrange :: [Int] | |
yrange = [-30..30] | |
main :: IO () | |
main = scrPlot $ mandelIter | |
mandelIter :: (Double, Double) -> Int | |
mandelIter (x,y) = toOneChar $ mandelbrot (x :+ y) (0 :+ 0) 0 | |
mandelbrot :: Complex Double -- Coordinate to test | |
-> Complex Double -- Iterating Z value | |
-> Int -- Current iteration | |
-> Int -- Iterations before diverging | |
mandelbrot c z iter | |
| iter > maxIter = 0 | |
| otherwise = let z' = z^2 + c in | |
if magnitude z' > 2 | |
then iter | |
else mandelbrot c z' (iter+1) | |
toOneChar :: Int -> Int | |
toOneChar x = if x > 9 | |
then 9 | |
else x | |
scrPlot :: ((Double, Double) -> Int) -> IO () | |
scrPlot f = mapM_ (\y -> do | |
mapM_ (flip printf y) xs | |
putStrLn "") ys | |
where | |
xs = map fromIntegral xrange | |
ys = map fromIntegral yrange | |
printf x y = putStr $ show $ | |
f (fromIntegral x / magnification,fromIntegral y / magnification) | |
-- 0000000000000000000000000000000000000000000000000000000000000 | |
-- 0000000000000000000000000000000000000000000000000000000000000 | |
-- 0000000000000000000000000000000000000000000000000000000000000 | |
-- 0000000000000000000000000000000000000000000000000000000000000 | |
-- 0000000000000000000000000000000000000000000000000000000000000 | |
-- 0000000000000000000000000000000000000000000000000000000000000 | |
-- 0000000000000000000000000000000000000000000000000000000000000 | |
-- 0000000000000000000000000000000000000000000000000000000000000 | |
-- 0000000000000000000000000000000000000000000000000000000000000 | |
-- 0000000000000000000000000000000000000000000000000000000000000 | |
-- 0000000000000000000000000000001000000000000000000000000000000 | |
-- 0000000000000000000000001111111111111000000000000000000000000 | |
-- 0000000000000000000000111111111111111110000000000000000000000 | |
-- 0000000000000000000011111111111111111111100000000000000000000 | |
-- 0000000000000000001111111111111111111111111000000000000000000 | |
-- 0000000000000000011111111111111111111111111100000000000000000 | |
-- 0000000000000000111111111111111111111111111110000000000000000 | |
-- 0000000000000001111111122222111111111111111111000000000000000 | |
-- 0000000000000011111222222222222211111111111111100000000000000 | |
-- 0000000000000011122222222333943322111111111111100000000000000 | |
-- 0000000000000111222222223334680332211111111111110000000000000 | |
-- 0000000000000122222222333345897433222111111111110000000000000 | |
-- 0000000000001222222223333457009544322211111111111000000000000 | |
-- 0000000000001222222233346779009855432211111111111000000000000 | |
-- 0000000000012222222334459090000099932221111111111100000000000 | |
-- 0000000000012222234444579000000000642221111111111100000000000 | |
-- 0000000000022223466666690000000000843222111111111100000000000 | |
-- 0000000000023334469999890000000000953222111111111100000000000 | |
-- 0000000000033344579000900000000000943222111111111100000000000 | |
-- 0000000000033556990000000000000000743222111111111100000000000 | |
-- 0000000000000000000000000000000009643222211111111110000000000 | |
-- 0000000000033556990000000000000000743222111111111100000000000 | |
-- 0000000000033344579000900000000000943222111111111100000000000 | |
-- 0000000000023334469999890000000000953222111111111100000000000 | |
-- 0000000000022223466666690000000000843222111111111100000000000 | |
-- 0000000000012222234444579000000000642221111111111100000000000 | |
-- 0000000000012222222334459090000099932221111111111100000000000 | |
-- 0000000000001222222233346779009855432211111111111000000000000 | |
-- 0000000000001222222223333457009544322211111111111000000000000 | |
-- 0000000000000122222222333345897433222111111111110000000000000 | |
-- 0000000000000111222222223334680332211111111111110000000000000 | |
-- 0000000000000011122222222333943322111111111111100000000000000 | |
-- 0000000000000011111222222222222211111111111111100000000000000 | |
-- 0000000000000001111111122222111111111111111111000000000000000 | |
-- 0000000000000000111111111111111111111111111110000000000000000 | |
-- 0000000000000000011111111111111111111111111100000000000000000 | |
-- 0000000000000000001111111111111111111111111000000000000000000 | |
-- 0000000000000000000011111111111111111111100000000000000000000 | |
-- 0000000000000000000000111111111111111110000000000000000000000 | |
-- 0000000000000000000000001111111111111000000000000000000000000 | |
-- 0000000000000000000000000000001000000000000000000000000000000 | |
-- 0000000000000000000000000000000000000000000000000000000000000 | |
-- 0000000000000000000000000000000000000000000000000000000000000 | |
-- 0000000000000000000000000000000000000000000000000000000000000 | |
-- 0000000000000000000000000000000000000000000000000000000000000 | |
-- 0000000000000000000000000000000000000000000000000000000000000 | |
-- 0000000000000000000000000000000000000000000000000000000000000 | |
-- 0000000000000000000000000000000000000000000000000000000000000 | |
-- 0000000000000000000000000000000000000000000000000000000000000 | |
-- 0000000000000000000000000000000000000000000000000000000000000 | |
-- 0000000000000000000000000000000000000000000000000000000000000 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment