Created
October 12, 2010 04:35
-
-
Save anonymous/621663 to your computer and use it in GitHub Desktop.
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
(ns mandel | |
(:require [clojure.contrib.math :as math]) | |
(:gen-class)) | |
(defn complex [r i] | |
[(double r) (double i)]) | |
(defn add [[r1 i1] [r2 i2]] | |
[(+ r1 r2) (+ i1 i2)]) | |
(defn mult [[r1 i1] [r2 i2]] | |
[(- (* r1 r2) (* i1 i2)) | |
(+ (* r1 i2) (* i1 r2))]) | |
(defn square [x] | |
(math/expt x 2)) | |
(defn norm-sq [[r i]] | |
(+ (square r) (square i))) | |
(defn mk-iter [c] | |
(fn [z] (add (mult z z) c))) | |
(def max-steps 25) | |
(defn steps [c] | |
(let [iter (mk-iter c)] | |
(loop [n 0 | |
z (complex 0 0)] | |
(cond | |
(>= n max-steps) max-steps | |
(> (norm-sq z) 4) n | |
:else (recur (+ n 1) (iter z)))))) | |
(def gamma (/ 1 2.0)) | |
(defn step-grey [n] | |
(int (* 255 (math/expt (/ n (double max-steps)) gamma)))) | |
(def img-w 300) | |
(def img-h 200) | |
(defn px-to-complex [[xp yp]] | |
(complex | |
(- (/ (* xp 3) (- img-w 1)) 2) | |
(- (/ (* yp 2) (- img-h 1)) 1))) | |
(def points | |
(for [y (range img-h) x (range img-w)] [x y])) | |
(defn out-header [] | |
(print "P2\n" img-w " " img-h "\n" 255 "\n")) | |
(defn compute-pt [pt] | |
(step-grey (steps (px-to-complex pt)))) | |
(defn out-val [v] | |
(print v " ")) | |
(defn pmap-batched [n f xs] | |
(let [k (int (/ (count xs) n))] | |
(apply concat (pmap #(doall (map f %)) (partition-all k xs))))) | |
(defn -main [] | |
(out-header) | |
(let [vs (doall (pmap-batched 2 compute-pt points))] | |
(dorun (map out-val vs))) | |
(print "\n") | |
(flush) | |
(shutdown-agents)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment