Public
Edited
Jul 13, 2023
Importers
Insert cell
Insert cell
{
const tree = new LSystem("0", { "1": "11", "0": "1[0]0" });
return tree.iteration(3);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
sierpinsky = new LSystem("F-G-G", {
F: "F-G+F+G-F",
G: "GG"
})
Insert cell
Insert cell
Insert cell
Insert cell
koch = new LSystem("F", { F: "F+F-F-F+F" })
Insert cell
Insert cell
Insert cell
cicero = new LSystem("F", {
F: "F+F--F+F"
})
Insert cell
{
// Parameters
const n = 7;

const angle = 86;
const dist = 3;
const width = 640;
const height = 400;

const t = new Turtle({
width,
height,
x: 10,
y: height - 65,
backgroundColor: "#BDF6FE"
});

t.penColor("#222").penSize(0.5);
const sequence = cicero.iteration(n);
sequence.split("").forEach((c) => {
switch (c) {
case "F":
t.segment(dist);
break;
case "+":
t.left(angle);
break;
case "-":
t.right(angle);
break;
}
});

return t.draw();
}
Insert cell
Insert cell
fern = new LSystem("X", { X: "F+[[X]-X]-F[-FX]+X", F: "FF" })
Insert cell
Insert cell
Insert cell
plant = new LSystem("F", { F: "F[+F]F[-F]F" })
Insert cell
Insert cell
Insert cell
plant2 = new LSystem("F", { F: "FF-[-F+F+F]+[+F-F-F]" })
Insert cell
Insert cell
Insert cell
algae = new LSystem("A", { A: "ABA", B: "BBB" })
Insert cell
Insert cell
Insert cell
class LSystem {
constructor(start, rules) {
this.start = start;
this.rules = rules;
}

step(state) {
return state
.split("")
.flatMap((s) => this.rules.hasOwnProperty(s) ? this.rules[s] : s)
.join("");
}

iteration(n) {
let state = this.start;
for (let k = 0; k < n; k = k + 1) {
state = this.step(state);
}
return state;
}
}
Insert cell
import {Turtle} from "@pnavarrc/turtle-graphics"
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more