Log in to post a comment.

// Using base L-System implementation from reinder
// Global code will be evaluated once.
Canvas.setpenopacity(1);
const turtle = new Turtle(-60,50);

// l-system
function createLSystem(numIters, axiom) {
    let s = axiom;
    for (let i=0; i<numIters; i++) {
        s = processString(s);
    }
    return s;
}

function processString(oldStr) {
    let newstr = "";
    for (let i=0; i<oldStr.length; i++) {
        newstr += applyRules(oldStr[i]);
    }
    return newstr;
}

function applyRules(ch) {
    switch (ch) {
        case "X": return "YF+XF+Y"; break;
        case "Y": return "XF-YF-X"; break;
        default: return ch;
    }
}

const inst = createLSystem(7, "YF"); // number of iterations and axiom
const distance = 1;
const angle = 60;

// The walk function will be called until it returns false.
function walk(i) {
    const cmd = inst[i];
    
    switch (cmd) {
        case "F":   turtle.forward(distance);
                    break;
        case "+":   turtle.right(angle);
                    break;
        case "-":   turtle.left(angle);
                    break;
        default:
    }
      
    return i < inst.length - 1;
}