const plottable = 0; //min=0 max=5 step=1 (No, Outline, Random Hatching, Regular Hatching, Outlined Random Hatching, Outlined Regular Hatching)
const depth = 7; //min=1 max=20 step=1
const leftAngle = 45; //min=1 max=89 step=1
const rightAngle = 45; //min=1 max=89 step=1
const polygonsOverlap = 0; //min=0 max=1 step=1 (No, Yes)
const drawOrder = 0; //min=0 max=2 step=1 (Trunk first, Leafs first, Randomly)
const startSize = 30; //min=10 max=50 step=1
const startOffsetX = 0; //min=-50 max=50 step=1
const startOffsetY = 42; //min=-50 max=50 step=1
const noise = 0; //min=0 max=10 step=.5
const trunkStretch = 1; //min=1 max=4 step=.05

// You can find the Turtle API reference here: https://turtletoy.net/syntax
Canvas.setpenopacity(plottable == 0? 1/depth: 1);

// Global code will be evaluated once.
const bales = Bales(depth, false);
const polygons = new Polygons();

// Global code will be evaluated once.
const turtle = new Turtle();

let squares = [[[startOffsetX, startOffsetY], Math.PI*1.5, startSize, 0]];
const angleLeft = Math.PI * leftAngle / 180;
const angleRight = Math.PI * rightAngle / 180;

// 
// Vector math
//

function rot2(a) { return [Math.cos(a), -Math.sin(a), Math.sin(a), Math.cos(a)]; }
function trans2(m, a) { return [m[0]*a[0]+m[2]*a[1], m[1]*a[0]+m[3]*a[1]]; }
function scale2(a,b) { return [a[0]*b,a[1]*b]; }
function sub2(a,b) { return [a[0]-b[0], a[1]-b[1]]; }
function add2(a,b) { return [a[0]+b[0],a[1]+b[1]]; }
function add3(a,b) { return [a[0]+b[0],a[1]+b[1],a[2]+b[2]]; }
function multi2(a, b) { return [a[0]*b[0], a[1]*b[1]]; }
function noise2(a, s) {
    let noise = trans2(rot2(Math.random() * Math.PI * 2), scale2([0, s], Math.random()));
    return add2(a, noise);
}

let calcDone = false;

// The walk function will be called until it returns false.
function walk(i) {
    if(!calcDone) {
        const square = squares[i];
        const points = getSquarePoints(square);
        
        const edgeUp = sub2(points[0], points[1]);
        const edgeLeftToRight = sub2(points[0], points[3]);
        
        const laRatio = square[2] / Math.sin(Math.PI - angleLeft - angleRight);
        const lengthRight = Math.sin(angleLeft) * laRatio;
        const lengthLeft = Math.sin(angleRight) * laRatio;

        const centerRight = add2(scale2(edgeUp, lengthRight / square[2] / 2), scale2(edgeLeftToRight, -lengthRight / square[2] / 2));
        const centerLeft = add2(scale2(edgeUp, lengthLeft / square[2] / 2), scale2(edgeLeftToRight, lengthLeft / square[2] / 2));

        if(square[3] < depth - 1) {
            squares.push([add2(points[0], trans2(rot2(-angleRight), centerRight)), square[1]- angleRight, lengthRight, square[3] + 1]);
            squares.push([add2(points[3], trans2(rot2(angleLeft), centerLeft)), square[1] + angleLeft, lengthLeft, square[3] + 1]);
        }
    
        calcDone = !(i < squares.length - 1);

        return true;
    }

    const square = (drawOrder == 0)? squares.shift(): (drawOrder == 1? squares.pop(): squares.splice((Math.random() * squares.length) | 0, 1)[0]);
    
    const points = getSquarePoints(square).map(i => noise2(i, noise));
    if(trunkStretch > 1 && square[3] == 0) {
        points[1] = add2(points[1], scale2(sub2(points[1], points[0]), trunkStretch - 1));
        points[2] = add2(points[2], scale2(sub2(points[2], points[3]), trunkStretch - 1));
    }
    drawPoly(points, square[3]);

    return squares.length > 0;
}

function getSquarePoints(square) {
    return [[-.5,-.5], [.5,-.5],[.5, .5], [-.5,.5]].map(i => scale2(i, square[2])).map(i => trans2(rot2(square[1]), i)).map(i => add2(i, square[0]));
}

function drawPoly(points, gen) {
    const p = polygons.create();
    p.addPoints(...points);
    switch(plottable) {
        case 0: //not
            p.addHatching(1, .15);
            break;
        case 1: //outline
            p.addOutline();
            break;
        case 2: //hatch
        case 3: //regular
            p.addHatching(Math.random()*Math.PI, plottable == 2? .3 + Math.random(): .3 + (.6*gen/depth));
            break;
        case 4: //both
        case 5: //both
        default:
            p.addOutline();
            p.addHatching(Math.random()*Math.PI, plottable == 4? .3 + Math.random(): .3 + (.6*gen/depth));
            break;
    }
    
    if(polygonsOverlap) {
        p.draw(plottable == 0? bales[gen]: turtle);
    } else {
        polygons.draw(plottable == 0? bales[gen]: turtle, p);
    }
    
    return;
}

////////////////////////////////////////////////////////////////
// Bale utility code - Created by Jurgen Westerhof 2022
// https://turtletoy.net/turtle/beb59d67ae
// Abusing the opacity, usage:
//      Canvas.setpenopacity(1/paletteSize);
//      const bales = Bales(paletteSize); // Bales(count, includeFullTransparent = true, turtleClass = null)
// Then use bales[x] wherever you would use a turtle object to 'draw'
// in 'color' x (i.e Polygon hatching with a bale object and .15 interspacing)
//      bales[x].jump(0,0);
//      bales[x].goto(40,0);
////////////////////////////////////////////////////////////////
function Bale(n, turtleClass = null) {class Bale {constructor(n, turtleClass = null) { this.turtles = Array.apply(null,{length: n}).map(i => turtleClass == null? new Turtle(): new turtleClass()); }back(e)         { this.turtles.forEach(t => t.back(e)); return this; }backward(e)     { this.turtles.forEach(t => t.backward(e)); return this; }bk(e)           { this.turtles.forEach(t => t.bk(e)); return this; }fd(e)           { this.turtles.forEach(t => t.fd(e)); return this; }forward(e)      { this.turtles.forEach(t => t.forward(e)); return this; }left(e)         { this.turtles.forEach(t => t.left(e)); return this; }lt(e)           { this.turtles.forEach(t => t.lt(e)); return this; }right(e)        { this.turtles.forEach(t => t.right(e)); return this; }rt(e)           { this.turtles.forEach(t => t.rt(e)); return this; }seth(e)         { this.turtles.forEach(t => t.seth(e)); return this; }setheading(e)   { this.turtles.forEach(t => t.setheading(e)); return this; }setx(e)         { this.turtles.forEach(t => t.setx(e)); return this; }sety(e)         { this.turtles.forEach(t => t.sety(e)); return this; }setpos(x, y)        { this.turtles.forEach(t => t.setpos(x, y)); return this; }setposition(x, y)   { this.turtles.forEach(t => t.setposition(x, y)); return this; }toradians(e)    { this.turtles.forEach(t => t.toradians(e)); return this; }degrees(e)      { this.turtles.forEach(t => t.degrees(e)); return this; }goto(x, y)      { this.turtles.forEach(t => t.goto(x, y)); return this; }jmp(x, y)       { this.turtles.forEach(t => t.jmp(x, y)); return this; }jump(x, y)      { this.turtles.forEach(t => t.jump(x, y)); return this; }circle(radius, extent, steps) { this.turtles.map(t => t.circle(radius, extent, steps)); return this; }clone()         { let b = new Bale(this.turtles.length); this.turtles.forEach((t, k) => b.turtles[k] = t.clone()); return b; }h()             { return this.turtles.length == 0? null: this.turtles[0].h(); }heading()       { return this.turtles.length == 0? null: this.turtles[0].heading(); }home()          { this.turtles.forEach(t => t.home()); return this; }isdown()        { return this.turtles.length == 0? null: this.turtles[0].isdown(); }pos()           { return this.turtles.length == 0? null: this.turtles[0].pos(); }position()      { return this.turtles.length == 0? null: this.turtles[0].position(); }pd()            { this.turtles.forEach(t => t.pd()); return this; }pendown()       { this.turtles.forEach(t => t.pendown()); return this; }penup()         { this.turtles.forEach(t => t.penup()); return this; }pu()            { this.turtles.forEach(t => t.pu()); return this; }down()          { this.turtles.forEach(t => t.down()); return this; }up()            { this.turtles.forEach(t => t.up()); return this; }radians()       { this.turtles.forEach(t => t.radians()); return this; }x()             { return this.turtles.length == 0? null: this.turtles[0].x(); }xcor()          { return this.turtles.length == 0? null: this.turtles[0].xcor(); }y()             { return this.turtles.length == 0? null: this.turtles[0].y(); }ycor()          { return this.turtles.length == 0? null: this.turtles[0].ycor(); }set(key, value) { this.turtles.forEach(i => i[key] = value); return this; }get(key) { return this.turtles.length == 0? null: this.turtles[0][key]; }}return new Bale(n, turtleClass);}
function Bales(count, includeFullTransparent = true, turtleClass = null) { if(count == 1) return [new Bale(1, turtleClass)]; const getExponent = (base, target) => Math.log(target) / Math.log(base); const baleSize = count - (includeFullTransparent?1:0); const n = Array.apply(null,{length: baleSize}).map((v,k) => Math.round(getExponent(1 - 1/count, 1 - (count - k == count?.99:(baleSize - k)/baleSize)))); if(includeFullTransparent) n.push(0); return n.map(i => new Bale(i, turtleClass));}

////////////////////////////////////////////////////////////////
// Polygon Clipping utility code - Created by Reinder Nijhoff 2019
// (Polygon binning by Lionel Lemarie 2021)
// https://turtletoy.net/turtle/a5befa1f8d
////////////////////////////////////////////////////////////////
function Polygons(){const t=[],s=25,e=Array.from({length:s**2},t=>[]),n=class{constructor(){this.cp=[],this.dp=[],this.aabb=[]}addPoints(...t){let s=1e5,e=-1e5,n=1e5,h=-1e5;(this.cp=[...this.cp,...t]).forEach(t=>{s=Math.min(s,t[0]),e=Math.max(e,t[0]),n=Math.min(n,t[1]),h=Math.max(h,t[1])}),this.aabb=[s,n,e,h]}addSegments(...t){t.forEach(t=>this.dp.push(t))}addOutline(){for(let t=0,s=this.cp.length;t<s;t++)this.dp.push(this.cp[t],this.cp[(t+1)%s])}draw(t){for(let s=0,e=this.dp.length;s<e;s+=2)t.jump(this.dp[s]),t.goto(this.dp[s+1])}addHatching(t,s){const e=new n;e.cp.push([-1e5,-1e5],[1e5,-1e5],[1e5,1e5],[-1e5,1e5]);const h=Math.sin(t)*s,o=Math.cos(t)*s,a=200*Math.sin(t),i=200*Math.cos(t);for(let t=.5;t<150/s;t++)e.dp.push([h*t+i,o*t-a],[h*t-i,o*t+a]),e.dp.push([-h*t+i,-o*t-a],[-h*t-i,-o*t+a]);e.boolean(this,!1),this.dp=[...this.dp,...e.dp]}inside(t){let s=0;for(let e=0,n=this.cp.length;e<n;e++)this.segment_intersect(t,[.1,-1e3],this.cp[e],this.cp[(e+1)%n])&&s++;return 1&s}boolean(t,s=!0){const e=[];for(let n=0,h=this.dp.length;n<h;n+=2){const h=this.dp[n],o=this.dp[n+1],a=[];for(let s=0,e=t.cp.length;s<e;s++){const n=this.segment_intersect(h,o,t.cp[s],t.cp[(s+1)%e]);!1!==n&&a.push(n)}if(0===a.length)s===!t.inside(h)&&e.push(h,o);else{a.push(h,o);const n=o[0]-h[0],i=o[1]-h[1];a.sort((t,s)=>(t[0]-h[0])*n+(t[1]-h[1])*i-(s[0]-h[0])*n-(s[1]-h[1])*i);for(let n=0;n<a.length-1;n++)(a[n][0]-a[n+1][0])**2+(a[n][1]-a[n+1][1])**2>=.001&&s===!t.inside([(a[n][0]+a[n+1][0])/2,(a[n][1]+a[n+1][1])/2])&&e.push(a[n],a[n+1])}}return(this.dp=e).length>0}segment_intersect(t,s,e,n){const h=(n[1]-e[1])*(s[0]-t[0])-(n[0]-e[0])*(s[1]-t[1]);if(0===h)return!1;const o=((n[0]-e[0])*(t[1]-e[1])-(n[1]-e[1])*(t[0]-e[0]))/h,a=((s[0]-t[0])*(t[1]-e[1])-(s[1]-t[1])*(t[0]-e[0]))/h;return o>=0&&o<=1&&a>=0&&a<=1&&[t[0]+o*(s[0]-t[0]),t[1]+o*(s[1]-t[1])]}};return{list:()=>t,create:()=>new n,draw:(n,h,o=!0)=>{reducedPolygonList=function(n){const h={},o=200/s;for(var a=0;a<s;a++){const c=a*o-100,r=[0,c,200,c+o];if(!(n[3]<r[1]||n[1]>r[3]))for(var i=0;i<s;i++){const c=i*o-100;r[0]=c,r[2]=c+o,n[0]>r[2]||n[2]<r[0]||e[i+a*s].forEach(s=>{const e=t[s];n[3]<e.aabb[1]||n[1]>e.aabb[3]||n[0]>e.aabb[2]||n[2]<e.aabb[0]||(h[s]=1)})}}return Array.from(Object.keys(h),s=>t[s])}(h.aabb);for(let t=0;t<reducedPolygonList.length&&h.boolean(reducedPolygonList[t]);t++);h.draw(n),o&&function(n){t.push(n);const h=t.length-1,o=200/s;e.forEach((t,e)=>{const a=e%s*o-100,i=(e/s|0)*o-100,c=[a,i,a+o,i+o];c[3]<n.aabb[1]||c[1]>n.aabb[3]||c[0]>n.aabb[2]||c[2]<n.aabb[0]||t.push(h)})}(h)}}}