Last active
August 29, 2015 14:07
-
-
Save DataKinds/3a13b8418b98d6eb05f1 to your computer and use it in GitHub Desktop.
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
{-# LANGUAGE BangPatterns #-} | |
--Enables strict arguments, the X in mandelbrot | |
import Data.Complex | |
import Data.List | |
maxIter :: Int | |
maxIter = 50 | |
xMin, xMax, xPrecision, yMin, yMax, yPrecision :: Double | |
xMin = -2 -- -2 | |
xMax = 1 -- 1 | |
xPrecision = 0.001 -- 0.05 | |
yMin = -1.25 -- -2 | |
yMax = 1.25 -- 2 | |
yPrecision = 0.001 -- 0.05 | |
mandelbrot :: Complex Double -> Complex Double -> Int -> Int | |
mandelbrot !x !c !iter | |
| magnitude f >= 2 = iter | |
| iter >= maxIter = -1 | |
| otherwise = mandelbrot f c (iter + 1) | |
where f = x ^ 2 + c | |
applyColormap :: [[Int]] -> [[[Int]]] | |
applyColormap = (map . map) color | |
where color n | |
| n == (-1) = [0] | |
| otherwise = [n] | |
mandelColormapString :: String | |
mandelColormapString = (unlines . map (unwords . map (unwords . map show))) $ applyColormap mandelSet | |
where mandelSet = (map . map) (\c -> mandelbrot 0 c 0) [[a :+ b | a <- [xMin, (xMin+xPrecision) .. xMax]] | b <- [yMax, (yMax-yPrecision) .. yMin]] -- for whatever reason the y's are backwards. it works, don't change it | |
header :: String | |
header = "P2\n"++(show $ length [xMin, (xMin+xPrecision) .. xMax])++" "++(show $ length [yMax, (yMax-yPrecision) .. yMin])++"\n"++(show maxIter)++"\n" | |
file :: String | |
file = header ++ mandelColormapString | |
main :: IO () | |
main = writeFile "mandelbrot.pgm" file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment