Lines_01

its some lines. trying to recreate something from Frank Albenesius

instagram.com/frankalbenesius/

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();


class line {
    constructor (slope) {
        this.array =[]
        for (let i = -50; i < 250; i += 1) {
            var x = i
            var y = (x-200) * slope + Math.cos(x*0.3)
            this.array.push([x,y])
        }
    }
}

function distort(array,angle,rand_amount,shift_amount) {
    var new_array = []
    for (let i = 0; i < array.length; i += 1) {
        x = array[i][0];
        y = array[i][1];
        new_x = x + rand_amount*Math.random()*Math.cos(angle) + shift_amount * Math.cos(angle);
        new_y = y + rand_amount * Math.random() * Math.sin(angle)+ shift_amount * Math.sin(angle);
        new_array.push([new_x,new_y]);
    }
    
    return new_array
}

const line_array = []
var _array = new line(1)
line_array.push(_array.array)
for (let i = 0; i < 90; i += 1) {
        distorted_line = distort(line_array[i],2+Math.random(),(4*Math.random()*Math.random()*Math.random()),3)
        line_array.push(distorted_line);
    }

turtle.drawSpline = (pts, tension = 0.5, isClosed = false, numOfSegments = 5) => {
    const res = []; // clone array
    const _pts = pts.slice(0);
    
    turtle.up();

    // The algorithm require a previous and next point to the actual point array.
    // Check if we will draw closed or open curve.
    // If closed, copy end points to beginning and first points to end
    // If open, duplicate first points to befinning, end points to end
    if (isClosed) {
        _pts.unshift(pts[pts.length - 1]);
        _pts.unshift(pts[pts.length - 2]);
        _pts.unshift(pts[pts.length - 1]);
        _pts.unshift(pts[pts.length - 2]);
        _pts.push(pts[0]);
        _pts.push(pts[1]);

    }
    else {
        _pts.unshift(pts[1]);   //copy 1. point and insert at beginning
        _pts.unshift(pts[0]);
        _pts.push(pts[pts.length - 2]); //copy last point and append
        _pts.push(pts[pts.length - 1]);

    }
    // ok, lets start..
    // 1. loop goes through point array
    // 2. loop goes through each segment between the 2 pts + 1e point before and after
    for (let i = 2; i < (_pts.length - 4); i += 2) {
        for (let t = 0; t <= numOfSegments; t++) {
            // calc tension vectors
            const t1x = (_pts[i+2][0] - _pts[i-2][0]) * tension;
            const t2x = (_pts[i+4][0] - _pts[i][0]) * tension;
            const t1y = (_pts[i+3][1] - _pts[i-1][1]) * tension;
            const t2y = (_pts[i+5][1] - _pts[i+1][1]) * tension;
            // calc step
            const st = t / numOfSegments;
            // calc cardinals
            const c1 =   2 * Math.pow(st, 3)  - 3 * Math.pow(st, 2) + 1; 
            const c2 = -(2 * Math.pow(st, 3)) + 3 * Math.pow(st, 2); 
            const c3 =       Math.pow(st, 3)  - 2 * Math.pow(st, 2) + st; 
            const c4 =       Math.pow(st, 3)  -     Math.pow(st, 2);
            // calc x and y cords with common control vectors
            const x = c1 * _pts[i][0]    + c2 * _pts[i+2][0] + c3 * t1x + c4 * t2x;
            const y = c1 * _pts[i+1][1]  + c2 * _pts[i+3][1] + c3 * t1y + c4 * t2y;
            //store points in array
            res.push(x);
            res.push(y);
            }
        }
    // draw
    turtle.goto(res[0], res[1]);
    turtle.position();
    for(let i = 2; i < res.length - 1; i += 2) {
        turtle.down();
        turtle.goto(res[i], res[i+ 1]);
        turtle.up();
    }
    return res;
}



// The walk function will be called until it returns false.

var j = 0;

function walk(i) {
    turtle.drawSpline(line_array[j])
    j += 1;
    return i < line_array.length-1
}