Last active
September 30, 2015 23:18
-
-
Save martintrojer/1878258 to your computer and use it in GitHub Desktop.
Mandelbrot set
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
(defn c+ [[re1 im1] [re2 im2]] [(+ re1 re2) (+ im1 im2)]) | |
(defn c* [[re1 im1] [re2 im2]] | |
[(- (* re1 re2) (* im1 im2)) (+ (* re1 im2) (* im1 re2))]) | |
(defn c2 [c] (c* c c)) | |
(defn |c| [[re im]] (Math/sqrt (+ (* re re) (* im im)))) | |
(defn get-mandel-set [im-range re-range max-iter] | |
(for [im im-range | |
re re-range | |
:let [c [re im]]] | |
(loop [z [0 0], cnt -1] | |
(let [z (c+ (c2 z) c) | |
cnt (inc cnt)] | |
(if-not (and (< (|c| z) 4) (< cnt max-iter)) | |
cnt | |
(recur z cnt)))))) | |
(defn print-mandel [sz data] | |
(let [cs ["." "," "\"" "-" ":" "/" "(" "*" "|" "$" "#" "@" "%" "~"]] | |
(loop [ds data] | |
(when-not (empty? ds) | |
(let [cs (map #(nth cs (dec %)) (take sz ds))] | |
(doseq [c cs] (print c)) | |
(println "") | |
(recur (drop sz ds))))))) | |
(->> | |
(get-mandel-set (range -1.2 1.2 0.05) (range -2 1 0.04) 14) | |
(print-mandel 75)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment