public
Created

  • Download Gist
oo-circles-toxic-03.clj
Clojure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
(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])
project.clj
Clojure
1 2 3 4 5 6 7
(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"]])

Open to suggestions for optimisation

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.