Crown_01

its a crown. Used @zoso95's interpolation (modified so it accepted [x,y]

imgur.com/xiw9k0v

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 num_circles = 8;
const amount_randomizer = 1/10;
const amount_oval = 1.25;
const x_randomness = 0;
const y_randomness = 1;
const circle_rad = 60;

const rand_array = [];
for (let i = 0; i<100; i+= 1){
    rand_array.push(Math.random());
}


class circlething {
    constructor (r,rand) {
        this.array = [];
        for (let i = 1; i < 65; i += 1) {
            var x = amount_oval*(Math.cos(i*0.1)*r-rand_array[i]*(rand*r*x_randomness));
            var y = Math.sin(i*0.1)*r-rand_array[i]*(rand*r*y_randomness);
            this.array.push([x,y]);
        }
        //this.array.push(this.array[this.array.length-1]);
    }
};

const circle_array = [];

for (let i = 0; i<num_circles; i+=1) {
    circle_array.push(new circlething(circle_rad,i*amount_randomizer));
}


turtle.drawSpline = (pts, tension = 0.5, isClosed = true, 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(circle_array[j].array);
    j += 1;
    return i < circle_array.length-1
}