Skip to content

Instantly share code, notes, and snippets.

@threedaymonk
Created September 11, 2012 20:32
Show Gist options
  • Save threedaymonk/3701781 to your computer and use it in GitHub Desktop.
Save threedaymonk/3701781 to your computer and use it in GitHub Desktop.
UTF-8 Mandelbrot in Clojure
(def screen-width 100)
(def screen-height 30)
(defn scaled-x [x]
(- (* (/ x screen-width) 3.5) 2.5))
(defn scaled-y [y]
(- (* (/ y screen-height) 2) 1))
(defn mandel [x y iteration x0 y0 max-iteration]
(if (and
(< (+ (* x x) (* y y)) 4)
(< iteration max-iteration))
(let [x' (+ (- (* x x) (* y y)) x0)
y' (+ (* 2 x y) y0)]
(recur x' y' (inc iteration) x0 y0 max-iteration))
iteration))
(defn char-for-c [c]
(nth " ░▒▓█" (/ c 8) "█"))
(defn draw-pixel [pixel-x pixel-y]
(let [x0 (scaled-x pixel-x)
y0 (scaled-y pixel-y)
c (mandel 0 0 0 x0 y0 1000)]
(print (char-for-c c))))
(defn draw-fractal []
(dotimes [y screen-height]
(dotimes [x screen-width]
(draw-pixel x y))
(println)))
(draw-fractal)
@CodeFarmer
Copy link

Paul... Clojure eh?

@krisajenkins
Copy link

Wow. Very impressed. :-D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment