Skip to content

Instantly share code, notes, and snippets.

@msgodf
Created June 24, 2012 08:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msgodf/2982481 to your computer and use it in GitHub Desktop.
Save msgodf/2982481 to your computer and use it in GitHub Desktop.
Functions to generate and print the Mandelbrot set
(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