xxxxxxxxxx
78
//variables: A B
//axiom: A
//rules: (A -> AB), (B -> A)
//variables: F+-[]
//axiom: F
//rules: FF+[+F-F-F]-[-F+F+F]
let angle;
let axiom = "F";
let sentence = axiom;
let len = 100;
let rules = [];
rules[0] = {
a: "F",
b: "FF+[+F-F-F]-[-F+F+F]"
};
function generate() {
len *= 0.5;
let nextSentence = "";
for (let i = 0; i < sentence.length; i++) {
let current = sentence.charAt(i);
let found = false;
for (let j = 0; j < rules.length; j++) {
if (current == rules[j].a) {
found = true;
nextSentence += rules[j].b
break;
}
}
if (!found) {
nextSentence += current;
}
}
sentence = nextSentence;
createP(sentence);
turtle();
}
function turtle() {
background(220);
resetMatrix();
translate(width/2, height);
for (let i = 0; i < sentence.length; i++) {
let current = sentence.charAt(i);
if(current == "F"){
stroke(0, 100);
line(0,0,0,-len);
translate(0, -len);
}else if(current == "+"){
rotate(angle);
}else if(current == "-"){
rotate(-angle);
}else if(current == "["){
push();
}else if(current == "]"){
pop();
}
}
}
function setup() {
createCanvas(400, 400);
angle = radians(25);
background(220);
createP(axiom);
turtle();
let button = createButton("generate");
button.mousePressed(generate);
}
function draw() {
}