Skip to content

Instantly share code, notes, and snippets.

@viebel
Last active August 18, 2021 01:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viebel/5af8070145d1c1c738f9ace114683cc2 to your computer and use it in GitHub Desktop.
Save viebel/5af8070145d1c1c738f9ace114683cc2 to your computer and use it in GitHub Desktop.
Godel Escher Bach - G fractal from Chapter 5
;; 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)
@viebel
Copy link
Author

viebel commented May 22, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment