Peaks

Filling the screen upwards with triangles.

Log in to post a comment.

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

// Global code will be evaluated once.
const turtle = new Turtle();
const startingPoints = 14; // min=2, max=150, step=1
const layers = 126; // min=1, max=150, step=1
const layerMax = 2; // min=1, max=30, step=1
const layerMin = 1; // min=1, max=30, step=1

function isEven(n) {
   return n % 2 == 0;
}

function randomNumber(min, max) {
    return Math.floor(Math.random() * (max - min) + min);
}

//let boundary = [[-100,84],[-93,95],[-80,88],[-73,97],[-62,85],[-53,92],[-33,84],[11,93],[33,78],[39,93],[45,84],[54,89],[69,83],[84,89],[100,78]];

let boundary = [[-100,84]];
for (let i=1; i<startingPoints; i++) {
    boundary.push([200*i/startingPoints+randomNumber(0,5)-100,randomNumber(85,99)]);
}
boundary.push([100,randomNumber(85,99)]);

function drawBoundary() {
    turtle.jump(boundary[0]);
    for (let i=0; i < boundary.length; i++) {
        turtle.goto(boundary[i]);    
    }
}


function linkPeaks() {
    let newBoundary = boundary;

    for (let i=0; i < boundary.length-1; i++) {
        if (boundary[i][1]<boundary[i+1][1]) {
                newBoundary.splice(i+1,1);
//                i++;
        }
    }
    boundary=newBoundary;       
}

function newPeaks(){
    let newBoundary = boundary;

    for (let j=0; j < boundary.length-1; j++) {
            if (Math.abs(boundary[j][0]-boundary[j+1][0]) > 1) {
                newBoundaryPoint = [randomNumber(boundary[j][0],boundary[j+1][0]),Math.min(boundary[j][1],boundary[j][1])-randomNumber(layerMin,layerMax)];
                newBoundary.splice(j+1,0,newBoundaryPoint); 
                j++;
            }
    }
    newBoundary.splice(0,1,[-100,newBoundary[0][1]-randomNumber(layerMin,layerMax)]);
    newBoundary.splice(-1,1);
    newBoundary.push([100,newBoundary[newBoundary.length-1][1]-randomNumber(-5,5)]);
    boundary=newBoundary;    
}

drawBoundary();


    
// The walk function will be called until it returns false.
function walk(j) {
    linkPeaks();
    drawBoundary();
    newPeaks();
    drawBoundary();
    return j < layers;
}