Fuzzy Monster

Grid of ellipses that are rotated by a Perlin noise field

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();
let seed = 1000; // min=1 max=1000000 step=1
const bits = 20; 
const samples = 150000; 
const mod = 1<<bits;
const h = 1.9 // min = 1 max = 100 step = 0.1
const w = 4 // min = 1 max = 100 step = 0.1
const rot = 0 // min = 0 max = 6.282 step = 0.001
const perlin_size = 3// min = 1 max = 10 step = 1
const perlin_magnitude = 6.4 // min = 0 max = 20 step = 0.1
const spacing = 5 // min = 2 max = 15 step = 1

function ellipse(steps, a, b, theta, x_center, y_center) {
    t = Math.PI * 2 / steps;
    turtle.penup();
    for(i = 0; i <= steps; i++) {
        if(i == 1) {
            turtle.pendown();
        }
        x = x_center + a*Math.cos(theta)*Math.cos(t*i) - b*Math.sin(theta)*Math.sin(t*i);
        y = y_center + a*Math.sin(theta)*Math.cos(t*i) + b*Math.cos(theta)*Math.sin(t*i);
        turtle.goto(x,y);
    }
}

////////////////////////////////////////////////////////////////
// Pseudorandom number generator. Created by Reinder Nijhoff 2024
// https://turtletoy.net/turtle/a2274fd1fe
////////////////////////////////////////////////////////////////
function random() { // returns a number [0, 1[
    let r = 1103515245 * (((seed+=12345) >> 1) ^ seed);
    r = 1103515245 * (r ^ (r >> 3));
    r = r ^ (r >> 16);
    return (r % mod) / mod;
}

////////////////////////////////////////////////////////////////
// Perlin noise field generator. Taken from turtle below
// https://turtletoy.net/turtle/04036becc3
////////////////////////////////////////////////////////////////
class Perlin {
    constructor(size, gridSize) {
        this.size = size;
        this.gridSize = gridSize;
        this.grid = [];

        for (let i = 0; i <= gridSize; i++) {
            let table = [];
            for (let j = 0; j <= gridSize; j++) {
                let angle = random() * 2 * Math.PI;
                let x = Math.cos(angle);
                let y = Math.sin(angle);
                table.push([x, y]);
            }
            this.grid.push(table);
        }
    }

    get(x, y) {
        x = x / 2 + this.size / 2;
        y = y / 2 + this.size / 2;
        if (x < 0) x = 0;
        if (x >= this.size) x = this.size - 0.01;
        if (y < 0) y = 0;
        if (y >= this.size) y = this.size - 0.01;

        let posx = x * this.gridSize / this.size;
        let posy = y * this.gridSize / this.size;

        let x1 = Math.floor(posx);
        let x2 = x1 + 1;
        let y1 = Math.floor(posy);
        let y2 = y1 + 1;

        let scal = [];
        scal.push(this.scalar(posx, posy, x1, y1));
        scal.push(this.scalar(posx, posy, x2, y1));
        scal.push(this.scalar(posx, posy, x1, y2));
        scal.push(this.scalar(posx, posy, x2, y2));

        let int1 = this.interpolate(posx - x1, scal[0], scal[1]);
        let int2 = this.interpolate(posx - x1, scal[2], scal[3]);

        return this.interpolate(posy - y1, int1, int2);
    }

    scalar(x, y, vx, vy) {
        x -= vx;
        y -= vy;
        return x * this.grid[vx][vy][0] + y * this.grid[vx][vy][1];
    }

    smooth(v) {
        if (v < 0) v = 0;
        if (v > 1) v = 1;
        return v ** 2 * (3 - 2 * v);
    }

    interpolate(x, a, b) {
        return a + (b - a) * this.smooth(x);
    }
}

let perlin_1 = new Perlin(100, perlin_size);

for(let i = -100; i <= 100; i+=spacing){
    for(let j = -100; j <= 100; j+=spacing){
        ellipse(40, w, h, rot + perlin_1.get(i,j) * perlin_magnitude, i, j);
    }
}