Chouser (owner)

Forks

Revisions

  • 7f8f97 Thu Dec 25 21:27:58 -0800 2008
gist: 40012 Download_button fork
public
Public Clone URL: git://gist.github.com/40012.git
Embed All Files: show embed
fractal.clj #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
(import '(java.awt Point Graphics Frame) '(java.awt.geom AffineTransform))
(def minpoint (doto (Point.) (.setLocation 0 0)))
(def maxpoint (doto (Point.) (.setLocation 1000 1000)))
 
(defmacro branch [t & choices]
  `(let [maxp# (.transform ~t maxpoint (Point.))
         minp# (.transform ~t minpoint (Point.))]
     (if (zero? (.distance maxp# minp#))
       maxp#
       (condp = (rand-int ~(count choices))
         ~@(mapcat list (range (count choices)) choices)))))
 
(defn transform [#^AffineTransform t]
  (branch t
    (recur (doto t (.scale 0.5 0.5) (.translate 0 500) (.rotate 2)))
    (recur (doto t (.scale 0.5 0.5) (.translate 0 500) (.rotate -2)))
    (recur (doto t (.scale 0.8 0.8) (.translate 0 300)))
    (recur (doto t (.scale 0.8 0.8) (.translate 0 300)))
    (recur (doto t (.scale 0.8 0.8) (.translate 0 300)))))
 
(defn draw [_ #^Frame w]
  (let [t (AffineTransform.)]
    (.translate t 300 650)
    (.scale t 0.5 -0.5)
    (let [#^Point p (transform t)]
      (.fillRect (.getGraphics w) (.x p) (.y p) 1 1))
    (send *agent* draw w)))
 
(let [w (doto (Frame. "Merry Christmas") (.setSize 600 700) (.setVisible true))]
  (dotimes [_ 5] (send (agent nil) draw w)))