Created
September 11, 2012 20:32
-
-
Save threedaymonk/3701781 to your computer and use it in GitHub Desktop.
UTF-8 Mandelbrot in Clojure
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
(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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wow. Very impressed. :-D