Skip to content

Instantly share code, notes, and snippets.

@clyce
Last active December 21, 2015 05:18
Show Gist options
  • Save clyce/6255625 to your computer and use it in GitHub Desktop.
Save clyce/6255625 to your computer and use it in GitHub Desktop.
(def ^:dynamic *current-canvas* nil)
(def key-pressed-count (atom 0))
(defn tick-and-bloom
[interval
[init-bloom-value f-on-bloom-in-every-tick]
[reset-pred & t-args]]
(fn [f]
(loop [to-bloom init-value]
(Thread/sleep interval)
(f to-bloom)
(recur ((if-not (apply terminate-pred t-args)
f-on-bloom-in-every-tick
(constantly init-value)) to-bloom)))))
;;matrix-multiply is not a function in clojure.core,
;;but a function pretend to have been defined in this program, sin & cos as well
;;point is in the structure of [x y]
(defn spinned-point [angle point]
(pop
(multiply (conj point 1)
[[(cos angle) -(sin angle) 0]
[(sin angle) (cos angle) 0]
[0 0 1]])))
(defn gen-rectangle-points-according-to-size-and-angle
[size angle]
(map (partial spinned-point angle)
(map (partial map (partial * size))
[[1 1] [1 -1] [-1 -1] [-1 1]])))
;;clear-and-redraw is a function pretended to be exist, again, not in clojure.core.
;;create-canvasand key-pressed? as well
;;but there's similar functions in see-saw library, see http://daveray.github.io/seesaw/seesaw.graphics-api.html
(defn draw-spinned-square [size angle]
(clear-and-redraw :polygon *current-canvas*
(gen-rectangle-points-according-to-size-and-angle size angle)))
(defn -main []
(binding [[width height] [640 480]
*current-canvas* (create-canvas width height)]
(future
((tick-and-bloom
1 [1 inc]
[key-pressed? :space])
(fn [x] (draw-spinned-square x x))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment