Skip to content

Instantly share code, notes, and snippets.

@nasser
Created March 11, 2018 17:34
Show Gist options
  • Save nasser/4fae1e960ad6d017b6165e398df61d5f to your computer and use it in GitHub Desktop.
Save nasser/4fae1e960ad6d017b6165e398df61d5f to your computer and use it in GitHub Desktop.
particles
(ns nasser.particles)
(defn new-particle
([x y vx vy] {::x x ::y y ::vx vx ::vy vy})
([x y] (new-particle x y 0 0)))
(defn particles-at-origin [n]
(repeat n (new-particle 0 0)))
(defn random-particle []
(new-particle (rand) (rand) (rand) (rand)))
(defn particles-around-origin [n]
(repeatedly n random-particle))
(defn particle-grid [n m distance]
(for [x (range n)
y (range m)]
(new-particle (* x distance) (* y distance))))
(defn update-particles [sys dt]
(map
(fn [{:keys [::x ::y ::vx ::vy]}]
(new-particle
(+ x (* dt vx))
(+ y (* dt vy))
vx
vy))
sys))
(defn new-system [ps]
(atom ps))
(defn update-all-particles! [sys dt]
(swap! sys update-particles dt))
(defn all-steps [n]
(->> (particles-around-origin n)
(iterate #(update-particles % 1))))
(comment
(-> test-particles ; (particles-around-origin 2)
(update-particles 1)
(update-particles 1)
(update-particles 1)
(update-particles 1)
(update-particles 1)
; new-system
; (update-all-particles! 1)
pprint
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment