Create a gist now

Instantly share code, notes, and snippets.

(ns oo-circles.core
(:use quil.core)
(:import [toxi.geom Vec2D]))
(def WIDTH 800)
(def HEIGHT 600)
(def num-circles 30)
(def center-vec (Vec2D. (/ WIDTH 2) (/ HEIGHT 2)))
(defn mk-circle []
"Circle object constructor"
(let [position (new Vec2D 0 0)]
{:position position
:velocity (atom (random TWO-PI))
:center-radius (atom 80)
:noise-inc (atom (random 10000))
:radius (+ 5 (random 5))
:fill-col (color (random 55) (random 155) (random 180 255))
:alph (+ 55 (random 200))}))
(defn make-circles [n]
"Return a lazyseq of n circles created with mk-circle"
(repeatedly n mk-circle))
(defn update-circle [{:keys [position velocity center-radius noise-inc radius] :as circle}]
"Creates a new position variable from the result of adding current position and velocity"
(let [new-position-x (+ (.x center-vec) (* (cos @velocity) @center-radius))
new-position-y (+ (.y center-vec) (* (sin @velocity) @center-radius))]
(swap! noise-inc + 0.1)
(swap! center-radius + (* (noise @noise-inc) 3))
(swap! velocity + 0.01)
(assoc circle :noise-inc @noise-inc)
(assoc circle :center-radius @center-radius)
(assoc circle :velocity @velocity)
(assoc circle :position (new Vec2D new-position-x new-position-y))))
(defn update-circles [circles]
(map update-circle circles))
(defn draw-circle [{:keys [position radius fill-col alph]}]
(fill fill-col alph)
(ellipse (.x position) (.y position) radius radius))
(defn setup []
(background 0)
(set-state! :circles (atom (make-circles num-circles))))
(defn draw []
(fill 0 8)
(rect 0 0 WIDTH HEIGHT)
(let [circles (state :circles)]
(swap! circles update-circles)
(doseq [c @circles]
(draw-circle c))))
(defn mouse-released []
"Simply concat a new set of circles to the existing pool."
(swap! (state :circles) concat (make-circles num-circles)))
(defsketch oo-circles
:title "OO Circles Toxic"
:setup setup
:draw draw
:mouse-released mouse-released
(defproject oo-circles "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url ""
:license {:name "Eclipse Public License"
:url ""}
:dependencies [[org.clojure/clojure "1.4.0"]
[quil "1.6.0"]])

Open to suggestions for optimisation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment