// Forked from "Fork: Von Koch snowflake" by rupertxrussell
// https://turtletoy.net/turtle/25d01b4b1e

// Forked from "Von Koch snowflake" by 4rknova
// https://turtletoy.net/turtle/4934db2221

// Using base L-System implementation from reinder
// Global code will be evaluated once.
Canvas.setpenopacity(1);

const xOffset = 0; //min=-40, max=80, step=0.5
const yOffset = 0; //min=-40, max=80, step=0.5


const turtle = new Turtle(xOffset,yOffset);

// 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 "F": return "F+F-F+F"; break;
        default: return ch;
    }
}

const inst = createLSystem(7, "F+F-F+F"); // number of iterations and axiom
const distance = 30; //min=.1, max=30, step=1
const angle = 18; //min=10, max=90, step=0.5

// 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 "B":   turtle.backward(distance);
                    break;
        case "+":   turtle.right(angle);
                    break;
        case "-":   turtle.left(angle);
                    break;
        default:
    }
      
    return i < inst.length - 1;
}