2011-07-10 11:06:37 +00:00
-----
isHidden: false
menupriority: 1
kind: article
created_at: 2011-07-10T12:41:26+02:00
en: title: ASCII Haskell Mandelbrot
fr: title: Mandelbrot avec haskell
author_name: Yann Esposito
author_uri: yannesposito.com
2011-10-26 08:30:53 +00:00
tags:
- mandelbrot
- haskell
- ASCII
- golfed
2011-07-10 11:06:37 +00:00
-----
en: Here is the obfuscated code:
fr: Voici le code "obfusqué" :
2011-07-30 23:44:55 +00:00
< code class = "haskell" file = "animandel.hs" >
2011-07-10 11:06:37 +00:00
a=27;b=79;c=C(-2.0,-1.0);d=C(1.0,1.0);e=C(-2.501,-1.003)
newtype C = C (Double,Double) deriving (Show,Eq)
instance Num C where C(x,y)*C(z,t)=C(z*x-y*t,y*z+x*t);C(x,y)+C(z,t)=C(x+z,y+t);abs(C(x,y))=C(sqrt(x*x+y*y),0.0)
r(C(x,y))=x;i(C(x,y))=y
f c z 0=0;f c z n=if(r(abs(z))>2)then n else f c ((z*z)+c) (n-1)
h j k = map (\z->(f (C z) (C(0,0)) 32,(fst z>l - q/2))) [(x,y)|y< - [ p ,( p +(( o-p )/ a )).. o ], x < - [ m ,( m + q ).. l ]] where o = i k ; p = i j ; m = r j ; l = r k ; q = (l-m)/b
u j k = concat $ map v $ h j k where v (i,p)=(" .,`'°\":;-+oO0123456789=!%*§& $@#"!!i):rst p;rst True="\n";rst False=""
main = putStrLn $ im 0 where cl n (C (x,y))=let cs=(1.1**n-1) in C ((x+cs*(r e))/cs+1,(y+cs*(i e))/cs+1);bl n=cl n c;tr n=cl n d;im n=u (bl n) (tr n)++"\x1b[H\x1b[25A"++im (n+1)
< / code >
en: To launch it, you'll need to have [haskell ](http://haskell.org ) installed and to run:
fr: Pour le lancer, [haskell ](http://haskell.org ) doit être installé. Puis vous devez écrire dans un terminal :
< code class = "zsh" > ghc --make animandel.hs & & animandel< / code >
en: Here is some image after 50 iterations:
fr: Voici le résultat après 50 itérations.
2011-07-30 23:44:55 +00:00
< pre >
2011-07-12 14:52:43 +00:00
###@@@@@@@$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$&&&&&WWOOClbUOWW&&$$$$$$$$$$$$$$
##@@@@@@$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$&&&&&WWUCUb; ,jUOWW&&&$$$$$$$$$$$$
#@@@@@$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$&&&WWWWWUb ooCWW&&&&&&$$$$$$$$
@@@@$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$& & WWWWWWWWOU uUOWWWW& & & & & & $$$$$
@@@$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$& & & WOUObUOOOUUUCbi rbCUUUOWWWWWOUW& $$$
@$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$& & & & & & WWWUcr,iiCb o wUUUUUC;OW& $$
$$$$$$$$$$$$$$$$$$$$$$$$$$& & & & & & & & & & WWWWOUC, j llW& & $
$$$$$$$$$$$$$$$$$$$$$& & & & & & & & & & & & WWWWWWOCCbi bWWW& &
$$$$$$$$$$$$$$$$$& & WWWWWWW& & & WWWWWWWWOUo jUOWW& &
$$$$$$$$$$$$$$& & & WWOwOOWWWOUUOWWWWWOOUbw j.blW&
$$$$$$$$$$$& & & & & WWWObiijbUCl bCiUUUUUCj, bOW&
$$$$$$$$$& & & & & & & WWWOUbw ; oobCbl jUWW&
$$$$$$$& & & & & & & WWWWOcbi ij jUW& &
$$$$$& & WWWWWWWOwUUCbw WW& &
WWWOWWWWWWWWWUUbo UWWW& &
: wbUOWW& & &
WWWOWWWWWWWWWUUbo UWWW& &
$$$$$& & WWWWWWWOwUUCbw WW& &
$$$$$$$& & & & & & & WWWWOcbi ij jUW& &
$$$$$$$$$& & & & & & & WWWOUbw ; oobCbl jUWW&
$$$$$$$$$$$& & & & & WWWObiijbUCl bCiUUUUUCj, bOW&
$$$$$$$$$$$$$$& & & WWOwOOWWWOUUOWWWWWOOUbw j.blW&
$$$$$$$$$$$$$$$$$& & WWWWWWW& & & WWWWWWWWOUo jUOWW& &
$$$$$$$$$$$$$$$$$$$$$& & & & & & & & & & & & WWWWWWOCCbi bWWW& &
$$$$$$$$$$$$$$$$$$$$$$$$$$& & & & & & & & & & WWWWOUC, j llW& & $
@$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$& & & & & & WWWUcr,iiCb o wUUUUUC;OW& $$
2011-07-30 23:44:55 +00:00
< / pre >
2011-07-10 11:06:37 +00:00
Here is the more readable version. I believe with this far more readable version, no more explanation is needed.
2011-07-30 23:44:55 +00:00
< code class = "haskell" >
2011-07-11 15:33:38 +00:00
nbvert = 30
2011-07-10 11:06:37 +00:00
nbhor = 79
2011-07-11 15:33:38 +00:00
zoomfactor = 1.01
init_bottom_left = C (-2.0,-2.0)
init_top_right = C (3.0,2.0)
interrest = C (-1.713,-0.000)
2011-07-10 11:06:37 +00:00
newtype Complex = C (Float,Float) deriving (Show,Eq)
instance Num Complex where
fromInteger n = C (fromIntegral n,0.0)
C (x,y) * C (z,t) = C (z*x - y*t, y*z + x*t)
C (x,y) + C (z,t) = C (x+z, y+t)
abs (C (x,y)) = C (sqrt (x*x + y*y),0.0)
signum (C (x,y)) = C (signum x , 0.0)
real :: Complex -> Float
real (C (x,y)) = x
im :: Complex -> Float
im (C (x,y)) = y
cabs :: Complex -> Float
cabs = real.abs
2011-07-11 15:33:38 +00:00
f :: Complex -> Complex -> Int -> Int
f c z 0 = 0
f c z n = if (cabs z > 2) then n else f c ((z*z)+c) (n-1)
2011-07-10 11:06:37 +00:00
2011-07-11 15:33:38 +00:00
bmandel bottomleft topright = map (\z -> (f (C z) (C(0,0)) 32, (fst z > right - hstep/2 ))) [(x,y) | y < - [ bottom ,( bottom + vstep ).. top ], x < - [ left ,( left + hstep ).. right ]]
2011-07-10 11:06:37 +00:00
where
top = im topright
bottom = im bottomleft
left = real bottomleft
right = real topright
vstep=(top-bottom)/nbvert
hstep=(right-left)/nbhor
2011-07-11 15:33:38 +00:00
mandel :: (Complex,Complex) -> String
mandel (bottomleft,topright) = concat $ map treat $ bmandel bottomleft topright
2011-07-10 11:06:37 +00:00
where
2011-07-11 15:33:38 +00:00
treat (i,jump) = " .,:;rcuowijlbCUOW& $@#" !! (div (i*22) 32):rst jump
2011-07-10 11:06:37 +00:00
rst True = "\n"
rst False = ""
2011-07-11 15:33:38 +00:00
cdiv :: Complex -> Float -> Complex
cdiv (C(x,y)) r = C(x/r, y/r)
cmul :: Complex -> Float -> Complex
cmul (C(x,y)) r = C(x*r, y*r)
zoom :: Complex -> Complex -> Complex -> Float -> (Complex,Complex)
zoom bl tr center magn = (f bl, f tr)
where
f point = ((center `cmul` magn) + point ) `cdiv` (magn + 1)
2011-07-10 11:06:37 +00:00
main = do
x < - getContents
putStrLn $ infinitemandel 0
where
2011-07-11 15:33:38 +00:00
window n = zoom init_bottom_left init_top_right interrest (zoomfactor**n)
infinitemandel n = mandel (window n) ++ "\x1b[H\x1b[25A" ++ infinitemandel (n+1)
2011-07-10 11:06:37 +00:00
< / code >