Skip to content

Instantly share code, notes, and snippets.

@nardove
Created March 29, 2013 11:24
Show Gist options
  • Save nardove/5270297 to your computer and use it in GitHub Desktop.
Save nardove/5270297 to your computer and use it in GitHub Desktop.
(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]}]
(no-stroke)
(fill fill-col alph)
(ellipse (.x position) (.y position) radius radius))
(defn setup []
(background 0)
(smooth)
(set-state! :circles (atom (make-circles num-circles))))
(defn draw []
(no-stroke)
(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
:size [WIDTH HEIGHT])
(defproject oo-circles "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.4.0"]
[quil "1.6.0"]])
@nardove
Copy link
Author

nardove commented Mar 30, 2013

Open to suggestions for optimisation

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