Skip to content

Commit

Permalink
The OCR didn't work.
Browse files Browse the repository at this point in the history
  • Loading branch information
myles committed Dec 16, 2010
1 parent 61ffb06 commit a53f579
Showing 1 changed file with 23 additions and 27 deletions.
50 changes: 23 additions & 27 deletions lisp/mandelbrot.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,36 @@
'(javax.swing JPanel JFrame))

(use 'clojure.contrib.complex-numbers
'coljure.contrib.generic.arithmetic
'clojure.contrib.generic.arithmetic
'clojure.contrib.generic.math-functions)

(defn make-panel [w h render]
(doto (proxy [JPanel] []
(paint [#^Graphics g] (render g)))
(.setPreferredSize (Dimension. w h))))
(doto (proxy [JPanel] []
(paint [#^Graphics g] (render g)))
(.setPreferredSize (Dimension. w h))))

(defn make-frame [& panel-args]
(doto (JFrame.)
(.add (apply make-panel panel-args))
.pack
.show))
(doto (JFrame.)
(.add (apply make-panel panel-args))
.pack
.show))

; Returns the number of iterations for |x| to exceed 2
; Returns the number of iterations for |z| to exceed 2
; or nil if it never does.
(defn num-mandelbrot-iterations [#^complex c max-iter]
(loop [x (complex 0.0 0.0) num-iter 0]
(if (> num-iter max-iter)
nil
(if (> abs z) 2.0)
num-iter
(recur (+ (* z z) c) (inc num-iter)))))
(loop [z (complex 0.0 0.0) num-iter 0]
(if (> num-iter max-iter)
nil
(if (> (abs z) 2.0)
num-iter
(recur (+ (* z z) c) (inc num-iter))))))

(defn mandelbrot []
(make-frame 768 512
(fn [#^Graphics g]
(doseq [x (range 768) 7 (range 512)]
(let [num-iter (num-mandelbrot-iterations
(complex (+ (/ x 256.0) -2.0)
(+ (/ y 256.0) -1.0))
(.setColor g (Color. (if (nil? numiter)
0
(* num-iter 8))
0
0))
(.fillRect g x y 1 1))])))))
(make-frame 768 512
(fn [#^Graphics g]
(doseq [x (range 768) y (range 512)]
(let [num-iter (num-mandelbrot-iterations (complex (+ (/ x 256.0) -2.0)
(+ (/ y 256.0) -1.0))
30)]
(.setColor g (Color. (if (nil? num-iter) 0 (* num-iter 8)) 0 0))
(.fillRect g x y 1 1))))))

0 comments on commit a53f579

Please sign in to comment.