Skip to content

Instantly share code, notes, and snippets.

@swannodette
Created January 16, 2009 17:06
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 swannodette/48011 to your computer and use it in GitHub Desktop.
Save swannodette/48011 to your computer and use it in GitHub Desktop.
(defn diffuse [grid diff-ratio dt]
(let [a (float (* dt diff-ratio grid-size grid-size))
a4-1 (float (+ 1 (* 4 a)))
grid #^floats (deref grid)
diffused-grid #^floats (make-grid)
grid-size (int grid-size)
line-length (int (+ grid-size 2))]
(dotimes [n 20]
(dotimes [y grid-size]
(let [line-offset (* (inc y) line-length)]
(loop [x (int 1)
c (int (+ x line-offset))]
(aset diffused-grid c
(/ (+ (aget grid c)
(* a
(+ (+ (aget diffused-grid
(unchecked-dec c))
(aget diffused-grid
(unchecked-inc c)))
(+ (aget diffused-grid
(unchecked-subtract c line-length))
(aget diffused-grid
(unchecked-add c line-length))))))
a4-1))
(when (< x grid-size)
(recur (unchecked-inc x)
(unchecked-inc c)))))))
diffused-grid))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment