Last active
August 18, 2021 01:48
-
-
Save viebel/5af8070145d1c1c738f9ace114683cc2 to your computer and use it in GitHub Desktop.
Godel Escher Bach - G fractal from Chapter 5
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
;; Godel Escher Bach - G fractal from Chapter 5 | |
(set! | |
(.-innerHTML js/klipse-container) | |
"<canvas style='width:100%; height:100%'></canvas>") | |
(def canvas (aget (js/document.getElementsByTagName "canvas") 0)) | |
(set! (.-height canvas) (.-innerHeight js/window)) | |
(def ctx (.getContext canvas "2d")) | |
(defn circle [ctx x y] | |
(.beginPath ctx) | |
(set! (.-fillStyle ctx) "black") | |
(.arc ctx x y 2.5 0 (* 2 js/Math.PI)) | |
(.stroke ctx) | |
(.fill ctx)) | |
(defn connect [ctx side x y width] | |
(let [x-end (if (= side :left) | |
(- x width) (+ x width))] | |
(.beginPath ctx) | |
(.moveTo ctx x y) | |
(.lineTo ctx x-end y) | |
(.moveTo ctx x-end y) | |
(.lineTo ctx x-end (- y 10)) | |
(.stroke ctx) | |
(.moveTo ctx x y))) | |
(defn connect-up [ctx x y] | |
(.beginPath ctx) | |
(.moveTo ctx x y) | |
(.lineTo ctx x (- y 10)) | |
(.stroke ctx) | |
(.moveTo ctx x (- y 10))) | |
(defn draw-right [x y n width] | |
(connect ctx :right x y width) | |
(circle ctx (+ x width) (- y 10)) | |
(draw (+ x width) (- y 10) (dec n) (/ width 2))) | |
(defn draw-left [x y n width] | |
(connect ctx :left x y width) | |
(draw (- x width) (- y 10) (dec n) (/ width 2))) | |
(defn draw [x y n width] | |
(when (> n 0) | |
(connect-up ctx x y) | |
(circle ctx x (- y 10)) | |
(draw-left x (- y 10) n width) | |
(draw-right x (- y 10) n width))) | |
(draw (/ (.-width canvas) 2) 200 5 75) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Edit live with http://app.klipse.tech/?cljs_in.gist=viebel/5af8070145d1c1c738f9ace114683cc2&container=1