Last active
August 1, 2017 17:36
-
-
Save craftybones/32f40903e4d618909ff499f5e1b7d009 to your computer and use it in GitHub Desktop.
L-system implementations
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
(def koch-curve-right-angles | |
{:productions {\F "F+F-F-F+F"}, | |
:axiom "F", | |
:iterations 3, | |
:actions {\F '(FD 20) | |
\+ '(LT 90) | |
\- '(RT 90)}}) | |
(def sierpenski-triangle | |
{:productions {\F "F-G+F+G-F" | |
\G "GG"}, | |
:axiom "F-G-G", | |
:iterations 6, | |
:actions {\F '(FD 4) | |
\G '(FD 4)' | |
\+ '(LT 120) | |
\- '(RT 120)}}) | |
(def sierpenski-arrowhead | |
{:productions {\A "+B-A-B+" | |
\B "-A+B+A-"}, | |
:axiom "A", | |
:iterations 8, | |
:actions {\A '(FD 3) | |
\B '(FD 3)' | |
\+ '(LT 60) | |
\- '(RT 60)}}) | |
(defn value-or-itself [hashmap] | |
(fn [x] | |
(hashmap x x))) | |
(defn generate-iterator [productions] | |
(let [prod (value-or-itself productions)] | |
(comp clojure.string/join (partial map prod)))) | |
(defn nth-iteration [n axiom iterator] | |
(->> axiom | |
(iterate iterator) | |
(take n) | |
last)) | |
(defn render | |
[{:keys [productions iterations axiom actions]}] | |
(->> productions | |
generate-iterator | |
(nth-iteration iterations axiom) | |
(mapcat actions))) |
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
var generateLookup=function(production) { | |
return function(rule) { | |
return production[rule]||rule; | |
} | |
} | |
var substituion=function(productions,string) { | |
var lookup=generateLookup(productions); | |
return string.split("").map(lookup).join(""); | |
} | |
var iterate=function(productions,string,times) { | |
var initString=string; | |
for (var i = 0; i < times; i++) { | |
initString=substituion(productions,initString); | |
} | |
return initString | |
} |
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
def substitutions productions, string | |
string.split("").map{|c| productions.fetch(c,c)}.join("") | |
end | |
def iterate_substitutions productions, axiom, times | |
(0..times).inject(axiom) {|s| substitutions(productions,s)} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment