Last active
August 29, 2015 14:07
-
-
Save rm-hull/0c6393d5092cd656f801 to your computer and use it in GitHub Desktop.
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…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns enchilada.four-bugs | |
(:require | |
[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] | |
(-> | |
ctx | |
(fill-style :white) | |
(stroke-style :black) | |
(begin-path) | |
(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))))) | |
(-> | |
ctx | |
(close-path) | |
(fill) | |
(stroke))) | |
(defn draw-shapes [ctx n r decr angle] | |
(if (pos? r) | |
(recur | |
(-> ctx (draw-polygon n r) (rotate angle)) | |
n | |
(- r decr) | |
decr | |
angle) | |
ctx)) | |
(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}] | |
(-> | |
ctx | |
(save) | |
(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) | |
(restore))) | |
(show canvas) | |
(big-bang | |
: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