This commit is contained in:
Anish Lakhwara 2022-05-07 18:56:55 +10:00
parent fa330a3e2d
commit ac1bb39302

View file

@ -10,41 +10,60 @@
; setup function returns initial state. It contains ; setup function returns initial state. It contains
; circle color and position. ; circle color and position.
{:color 0 {:color 0
:angle 0}) :angle 0
:counter 0
:range 50})
(defn update-state [state] (defn update-state [state]
; Update sketch state by changing circle color and position. ; Update sketch state by changing circle color and position.
{:color (mod (+ (:color state) 0.7) 255) {:color (mod (+ (:color state) 0.7) 255)
:angle (+ (:angle state) 0.1)}) :angle (+ (:angle state) 0.1)
:counter (+ (:counter state) 1)
:range (if (= 4 (mod (:counter state) 20)) (q/random 20 50) (:range state))})
(defn tri (defn tri
[x y delta] [x y delta r]
(q/triangle x y x (- y delta) (- x delta) y) (q/triangle x y (* r x) (- y delta) (- x delta) y)
(q/triangle x y x (+ y delta) (- x delta) y) (q/triangle x (* r y) x (+ y delta) (- x delta) y)
(q/triangle x y x (- y delta) (- x delta) y) #_(q/triangle x y x (- y delta) (- x delta) y)
(q/triangle x y x (+ y delta) (+ x delta) y)) #_(q/triangle x y x (+ y delta) (+ x delta) y))
(defn draw-state [state] (defn draw-state [state]
; Clear the sketch by filling it with light-grey color. ; Clear the sketch by filling it with light-grey color.
(q/background 0) (q/background 0)
; Set circle color. ; Set circle color.
(q/fill (:color state) 255 255)
(q/stroke 55 100) (q/stroke 55 100)
(let [angle (:angle state) (let [angle (:angle state)
r (:range state)
size 50 size 50
x1 (* size (q/cos angle)) x1 (* 0.7 size (q/cos angle))
y1 -50 y1 (* 0.3 size (q/sin angle))
x2 (+ x1 size) x2 (+ x1 size)
y2 (+ y1 size) y2 (+ y1 size)
delta (/ size 2)] delta (/ size 2)]
(q/with-translation [(/ (q/width) 2) (q/with-translation [(/ (q/width) 2)
(/ (q/height) 2)] (/ (q/height) 2)]
#_(q/rect x1 y1 size size) (doseq [i (range -300 300 r)]
(tri x1 y1 (* delta (q/cos angle))) (q/push-matrix)
(tri x2 y1 delta) (q/fill (* r (:color state)) (* 0.5 (:color state)) (+ 100 (:color state)))
(tri x1 y2 delta) (q/rotate (q/radians (* delta (q/cos (+ i angle)))))
(tri x2 y2 (* delta (q/sin angle))) (tri (+ i x1) y1 (* delta (q/cos angle)) r)
) (tri x1 (+ y1 i) (* delta (q/sin angle)) r)
(q/pop-matrix))
(doseq [i (range -400 400 (* 0.6 r))]
(q/push-matrix)
(q/fill (* 0.6 r (:color state)) (* 0.5 (:color state)) (+ 100 (:color state)))
(q/rotate (q/radians (* delta (q/sin (+ i angle)))))
(tri (+ i x2) y2 (* delta (q/cos angle)) r)
(tri x2 (+ y2 i) (* delta (q/sin angle)) r)
(q/pop-matrix))
#_(q/push-matrix)
#_(q/rotate (q/radians (* delta (q/sin angle))))
#_(tri x2 y1 delta)
#_(tri x1 y2 delta)
#_(q/pop-matrix)
#_(tri x2 y2 (* delta (q/sin angle))))
; Calculate x and y coordinates of the circle. ; Calculate x and y coordinates of the circle.
#_(let [angle (:angle state) #_(let [angle (:angle state)
@ -56,7 +75,7 @@
(q/defsketch shunic (q/defsketch shunic
:title "You spin my circle right round" :title "You spin my circle right round"
:size [500 500] :size [800 800]
; setup function called only once, during sketch initialization. ; setup function called only once, during sketch initialization.
:setup setup :setup setup
; update-state is called on each iteration before draw-state. ; update-state is called on each iteration before draw-state.