Skip to content

Instantly share code, notes, and snippets.

@t-sin
Created August 27, 2015 16:35
Show Gist options
  • Save t-sin/648aeec57dc2f18afaa4 to your computer and use it in GitHub Desktop.
Save t-sin/648aeec57dc2f18afaa4 to your computer and use it in GitHub Desktop.
powapowa...
(ns powapowa.core
(:require [quil.core :as q]
[quil.middleware :as m]))
(def +width+ 640)
(def +height+ 480)
(def +point-num+ 10)
(defn rand-point [w h]
[(rand-int w) (rand-int h)])
(defn rand-acc []
(letfn [(rand- [] (- (rand 0.04) 0.02))]
[(rand-) (rand-)]))
(defn rand-interval []
(+ 60 (rand-int 30)))
(defn move [[x y] [vx vy]]
[(+ x vx) (+ y vy)])
(defn add [[x1 y1] [x2 y2]]
[(+ x1 x2) (+ y1 y2)])
(defn multiple [n [x y]]
[(* n x) (* n y)])
(defn to-center [[x y :as p]]
(letfn ((tocenter [x l] (- l x)))
(multiple 0.0001 [(tocenter x (/ +width+ 2))
(tocenter y (/ +height+ 2))])))
(defn accelerate [[x y :as p] [vx vy :as v] [ax ay :as a]]
(add a (add v (to-center p))))
(defn setup []
; Set frame rate to 30 frames per second.
(q/frame-rate 30)
; Set color mode to HSB (HSV) instead of default RGB.
(q/color-mode :hsb)
{:points (take +point-num+ (repeatedly #(rand-point +width+ +height+)))
:velocities (take +point-num+ (repeat [0 0]))
:dvdt (take +point-num+ (repeatedly rand-acc))
:interval (take +point-num+ (repeatedly rand-interval))
:colors (take +point-num+ (repeatedly #(rand-int 256)))
:radius (take +point-num+ (repeatedly #(+ (rand-int 50) 70)))
:angles (take +point-num+ (repeat 0))
:angle-delta (take +point-num+ (repeatedly #(+ 0.5 (rand))))})
(defn update-state [state]
; Update sketch state by changing circle color and position.
{:points (map move (:points state) (:velocities state))
:velocities (map accelerate
(:points state)
(:velocities state)
(:dvdt state))
:dvdt (for [i (range +point-num+)]
(if (zero? (nth (:interval state) i))
(rand-acc)
(nth (:dvdt state) i)))
:interval (for [i (:interval state)]
(if (zero? i) (rand-interval) (dec i)))
:colors (:colors state)
:radius (:radius state)
:angles (map + (:angles state) (:angle-delta state))
:angle-delta (:angle-delta state)})
(defn draw-state [state]
(q/background 30)
(q/no-stroke)
(loop [n 0]
(let [x (first (nth (:points state) n))
y (second (nth (:points state) n))
c (nth (:colors state) n)
a (nth (:angles state) n)
r (+ (nth (:radius state) n) (* 10 (q/sin a)))]
(q/fill c 255 255)
(q/ellipse x y r r))
(when (< n (dec +point-num+))
(recur (inc n)))))
(q/defsketch powapowa
:title "powapowa"
:size [+width+ +height+]
; setup function called only once, during sketch initialization.
:setup setup
; update-state is called on each iteration before draw-state.
:update update-state
:draw draw-state
:features [:keep-on-top]
; This sketch uses functional-mode middleware.
; Check quil wiki for more info about middlewares and particularly
; fun-mode.
:middleware [m/fun-mode])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment