Skip to content

Instantly share code, notes, and snippets.

@noprompt
Last active May 22, 2019 16:33
Show Gist options
  • Select an option

  • Save noprompt/3b17bc7a97e2369f27166e1e5a356e31 to your computer and use it in GitHub Desktop.

Select an option

Save noprompt/3b17bc7a97e2369f27166e1e5a356e31 to your computer and use it in GitHub Desktop.
Implementing L-System examples from the "Algorithmic Beauty of Plants" with Meander
{:paths ["src"]
:deps {org.clojure/clojure {:mvn/version "1.10.0"}
org.clojure/clojurescript {:mvn/version "1.10.439"}
org.clojure/test.check {:mvn/version "0.10.0-alpha3"}
com.google/clojure-turtle {:mvn/version "0.3.0"}
meander/delta {:mvn/version "0.0.65"}
quil/quil {:mvn/version "3.0.0"}}
:aliases {:test {:extra-paths ["test"]
:extra-deps {org.clojure/test.check {:mvn/version "0.10.0-alpha3"}
com.cognitect/test-runner {:git/url "https://github.com/healthfinch/test-runner"
:sha "1d0cb97a14152959cdb7c1e8539a1759a1663f5b"}}
:main-opts ["-m" "cognitect.test-runner"]}}}
(ns topiary.core
(:require [meander.match.delta :as r.match]
[meander.strategy.delta :as r]
[clojure-turtle.core :as turtle]))
(turtle/new-window {:size [1024 768]})
;; F Move forward drawing a line.
;; f Move forward without drawing a line.
;; + Turn left by angle δ.
;; - Turn right by angle δ.
(defn interpret [d δ instruction]
(r.match/find instruction
F
(turtle/forward d)
f
(do (turtle/penup) (turtle/forward d)
(turtle/pendown))
+
(turtle/left δ)
-
(turtle/right δ)
[_ ..1 :as ?instructions]
(run!
(fn [?instruction]
(interpret d δ ?instruction))
?instructions)))
(defn l-system [s n]
(if (= n -1)
identity
(fn [t]
((r/all (l-system s (dec n))) (s t)))))
(def koch-island
{:axiom '[F - F - F - F]
:productions
(r/rewrite
F [F - F + F + F F - F - F + F]
?X ?X)})
(def example-a
{:axiom '[F - F - F - F]
:productions (r/rewrite
F [F F - F - F - F - F - F + F]
?X ?X)})
(def example-b
{:axiom '[F - F - F - F]
:productions (r/rewrite
F [F F - F - F - F - F F]
?X ?X)})
(def example-c
{:axiom '[F - F - F - F]
:productions (r/rewrite
F [F F - F + F - F - F F]
?X ?X)})
(def example-d
{:axiom '[F - F - F - F]
:productions (r/rewrite
F [F F - F - - F - F]
?X ?X)})
(def example-f
{:axiom '[F - F - F - F]
:productions (r/rewrite
F [F - F + F - F - F]
?X ?X)})
(let [X example-a
n 3
δ 90
system (l-system (:productions X) n)]
(turtle/clean)
(turtle/setxy 0 0)
(interpret 10 δ (system (:axiom X))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment