Skip to content

Instantly share code, notes, and snippets.

@ooesili
Created November 28, 2018 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ooesili/ef785611b69031f625e7b6c9ce33ccd8 to your computer and use it in GitHub Desktop.
Save ooesili/ef785611b69031f625e7b6c9ce33ccd8 to your computer and use it in GitHub Desktop.
Circles
(ns quils.circles.core
(:require [quil.core :as q]))
(defmacro with-matrix [& body]
`(do
(q/push-matrix)
(try
~@body
(finally (q/pop-matrix)))))
(def TAU (* 2 q/PI))
(def max-depth 4)
(def max-iterations 100)
(def render false)
(defn setup []
(q/frame-rate 10)
(q/color-mode :hsb)
{:iteration 0})
(defn update-state [state]
(update state :iteration inc))
(defn state->iteration [state]
(mod (:iteration state) max-iterations))
(defn state->scale-factor [state]
(-> (state->iteration state)
(/ max-iterations)
(* TAU)
q/sin))
(defn draw-circles [state depth]
(let [radius 10
main-diameter (* 2 radius)
angle-step (/ TAU 6)
hues (for [hue [0 32 64]]
(let [hue-step (/ 255 max-iterations)]
(mod (+ hue (* (state->iteration state) hue-step)) 255)))
size-step (state->scale-factor state)
sizes (iterate (partial + size-step) 1)
circles (map vector sizes hues)]
(doseq [[size hue] circles]
(q/stroke hue 255 255 8)
(let [diameter (* main-diameter size)]
(q/ellipse 0 0 diameter diameter)))
(when-not (zero? depth)
(doseq [i (range 6)]
(with-matrix
(q/rotate (* i angle-step))
(q/translate 0 radius)
(draw-circles state (dec depth)))))))
(defn draw-state [state]
(q/background 0 0 0)
(q/no-fill)
(q/stroke-weight 0.5)
(with-matrix
(q/translate (/ (q/width) 2) (/ (q/height) 2))
(q/scale (/ (q/width) 100) (/ (q/height) 100))
(draw-circles state max-depth))
(when render
(let [{:keys [iteration]} state]
(if (> iteration max-iterations)
(q/exit)
(q/save (format "frame-%03d.png" iteration))))))
(ns quils.circles.sketch
(:require [quil.core :as q]
[quil.middleware :as m]
[quils.circles.core :as core]))
(q/defsketch circles
:title "Circles"
:size [500 500]
:setup core/setup
:update core/update-state
:draw core/draw-state
:features []
:middleware [m/fun-mode])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment