Skip to content

Instantly share code, notes, and snippets.

@Chouser
Created January 23, 2009 17:57
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 Chouser/51110 to your computer and use it in GitHub Desktop.
Save Chouser/51110 to your computer and use it in GitHub Desktop.
(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 [opt (gensym)]
`(let [maxp# (.transform ~t maxpoint (Point.))
minp# (.transform ~t minpoint (Point.))]
(if (zero? (.distance maxp# minp#))
maxp#
(let [~opt (int (rand-int ~(count choices)))]
~(reduce (fn [acc [i choice]] `(if (== ~opt (int ~i)) ~choice ~acc))
nil
(map vector (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)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment