Created
June 24, 2012 08:40
-
-
Save msgodf/2982481 to your computer and use it in GitHub Desktop.
Functions to generate and print the Mandelbrot set
This file contains 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
(defn vec-abs | |
[[x y]] | |
(+ (* x x) | |
(* y y))) | |
(defn vec-add | |
[x y] | |
(map + x y)) | |
(defn vec-sq | |
[[x y]] | |
(vector (- (* x x) | |
(* y y)) | |
(* 2 x y))) | |
(defn mandel | |
[coord max-iterations] | |
(count | |
(filter #(> (vec-abs %) 4) | |
(take max-iterations | |
(iterate #(vec (vec-add (vec-sq %) | |
coord)) | |
[0 0]))))) | |
(defn build-square | |
[x] | |
(map #(vector (int (/ % x)) (mod % x)) | |
(range (* x x)))) | |
(defn mand-set | |
[size] | |
(map #(cond (= 0 (mandel % 250)) "#" | |
:else " ") | |
(map (fn [x] | |
(map #(/ (- % | |
(/ size 2.0)) | |
(/ size 3.0)) | |
x)) | |
(build-square size)))) | |
(defn print-square | |
[size square] | |
(pprint (map #(apply str | |
(apply vector %)) | |
(partition size square)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment