Last active
August 29, 2015 14:04
-
-
Save rm-hull/b971d77087ff873ed7c2 to your computer and use it in GitHub Desktop.
The Barnsley Fern is a fractal named after the British mathematician Michael Barnsley who first described it in his book _Fractals Everywhere_. The fern is one of the basic examples of self-similar sets, i.e. it is a mathematically generated pattern that can be reproducible at any magnification or reduction. Like the Sierpinski triangle, the Bar…
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 big-bang.examples.barnsley-fern | |
(:require | |
[jayq.core :refer [show]] | |
[big-bang.core :refer [big-bang]] | |
[enchilada :refer [ctx canvas canvas-size]] | |
[monet.canvas :refer [fill-rect fill-style]])) | |
(def width (first (canvas-size))) | |
(def height (second (canvas-size))) | |
(def scale (/ height 10)) | |
(def offset (/ width 2)) | |
(def black-spleenwort [ | |
;Cumulative | |
;Probability Co-efficients | |
[0.01 [ 0.00 0.00 0.00 0.16 0.00 0.00]] | |
[0.86 [ 0.85 0.04 -0.04 0.85 0.00 1.60]] | |
[0.93 [ 0.20 -0.26 0.23 0.22 0.00 1.60]] | |
[1.00 [-0.15 0.28 0.26 0.24 0.00 0.44]]]) | |
(def initial-state [0 0]) | |
(defn pick-coeffs [prob affine-transforms] | |
(first | |
(for [[cumulative-prob coeffs] affine-transforms | |
:when (<= prob cumulative-prob)] | |
coeffs))) | |
(defn apply-transform [[x y] [a b c d e f]] | |
[(+ (* a x) (* b y) e) | |
(+ (* c x) (* d y) f)]) | |
(defn update-state [event world-state] | |
(->> | |
black-spleenwort | |
(pick-coeffs (rand)) | |
(apply-transform world-state))) | |
(defn render-fern [[x y]] | |
(fill-rect ctx { | |
:x (Math/floor (+ offset (* scale x))) | |
:y (Math/floor (+ height (* scale y -1))) | |
:w 1 :h 1})) | |
(show canvas) | |
(fill-style ctx "green") | |
(big-bang | |
:initial-state initial-state | |
:on-tick update-state | |
:tick-rate 1 | |
:to-draw render-fern) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment