Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Although this gist is not exactly the four bug problem (where bugs/mice/insects are placed at the corners of a regular polygon, and each bug then begins to move towards its immediate neighbour in an counter-clockwise direction), it started out with that intention. Instead, because the angles are quite shallow and the lines close together, an int…
(ns enchilada.four-bugs
[jayq.core :refer [show]]
[big-bang.core :refer [big-bang]]
[enchilada :refer [canvas ctx value-of]]
[monet.canvas :refer [translate scale rotate
save restore fill-rect
stroke-style stroke fill-style fill
begin-path close-path move-to line-to]]))
(defn draw-polygon [ctx n r]
(fill-style :white)
(stroke-style :black)
(move-to r 0))
(dotimes [i n]
(let [theta (/ (* i 2 Math/PI) n)]
(line-to ctx (* r (Math/cos theta)) (* r (Math/sin theta)))))
(defn draw-shapes [ctx n r decr angle]
(if (pos? r)
(-> ctx (draw-polygon n r) (rotate angle))
(- r decr)
(def initial-state
{:t 0
:radius 290
:angle 0.01
:sides 4
:decrement 3.61})
(defn update-state [event world-state]
(update-in world-state [:t] inc))
(defn render [{:keys [t angle radius sides decrement] :as world-state}]
(fill-style :lightgreen)
(fill-rect {:x 0 :y 0 :w 800 :h 600})
(translate 400 300)
(rotate (* t 0.005))
(draw-shapes sides radius decrement angle)
(show canvas)
:initial-state initial-state
:on-tick update-state
:to-draw render)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment