Skip to content

Instantly share code, notes, and snippets.

@mayakraft
Created September 18, 2019 16:21
Show Gist options
  • Save mayakraft/d4f4a1110e995935c8cd653927528249 to your computer and use it in GitHub Desktop.
Save mayakraft/d4f4a1110e995935c8cd653927528249 to your computer and use it in GitHub Desktop.
a concise way of writing L-systems in Javascript. this rule set is the dragon curve
var rules = {
X: "X+YF+",
Y: "-FX-Y"
};
function rewrite(str) {
var s = "";
for (var i = 0; i < str.length; i++) {
if (rules[str[i]] !== undefined) {
s += rules[str[i]];
} else {
s += str[i];
}
}
return s;
}
// call n times
rewrite(rewrite(rewrite("FX")));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment