Skip to content

Instantly share code, notes, and snippets.

@LauJensen
Created March 24, 2010 16:38
Show Gist options
  • Save LauJensen/342473 to your computer and use it in GitHub Desktop.
Save LauJensen/342473 to your computer and use it in GitHub Desktop.
(defmacro do-board [[w h] & body]
`(let [w# (int ~w) h# (int ~h)]
(loop [~'i (int 1)]
(when (< ~'i h#)
(loop [~'j (int 1)]
(when (< ~'j w#)
(do ~@body)
(recur (unchecked-inc ~'j))))
(recur (unchecked-inc ~'i))))))
(defmacro map! [f lookup collection ]
`(let [[w# h#] [(count ~lookup) (-> ~lookup first count)]]
(let [board# (make-array Double/TYPE h# w#)]
; (dotimes [k# 20] o
(do-board [w# h#]
(let [~'above (deep-aget ~'doubles ~lookup (unchecked-dec ~'i) ~'j)
; (aget ~lookup (dec ~'i) ~'j)
~'below (deep-aget ~'doubles ~lookup (unchecked-inc ~'i) ~'j)
~'left (deep-aget ~'doubles ~lookup ~'i (unchecked-dec ~'j))
~'right (deep-aget ~'doubles ~lookup ~'i (unchecked-inc ~'j))
~'self (deep-aget ~'doubles ~lookup ~'i ~'j)]
(deep-aset ~'doubles board# (int ~'i) (int ~'j) ~f)))
board#)))
(defn diffuse-board
[board diffusion]
(let [sz (count board)
dr (* *dt* diffusion sz sz)]
(map! (min 255.0 (/ (+ self (* dr (+ (+ above below) (+ left right))))
(inc (* 4 dr))))
board (make-array Double/TYPE sz sz))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment