Mandelbrot through Curl Noise

A turtle is walking through a curl noise field to draw a Mandelbrot.
Forked from "Curl Noise" by reinder - Curl Noise

Log in to post a comment.

// Forked from "Curl Noise" by reinder
// https://turtletoy.net/turtle/740f09b88c

// Curl Noise. Created by Reinder Nijhoff 2021 - @reindernijhoff
//
// https://turtletoy.net/turtle/740f09b88c
//

const turtle = new Turtle();
turtle.degrees(Math.PI * 2);
turtle.traveled = 0;

const seed = 69; // min=1, max=100, step=1
const radius = 1.0; // min=0.1, max=5, step=0.01
const minRadius = 0.3; // min=0.01, max=1, step=0.01
const maxPathLength = 50;  // min=1, max=100, step=0.1
const frequency = 4; //min=.1, max=10, step=.01
const style = 2; // min=0 max=2 step=1 (Black,White,Invert)
const maxTries = 1000;

const noise = new SimplexNoise(seed);
const grid  = new PoissonDiscGrid(radius);

function fbm(x, y) {
    x *= frequency / 1000;
    y *= frequency / 1000;
    let f = 1., v = 0.;
    for (let i=0; i<3; i++) {
        v += noise.noise2D([x * f, y * f]) / f;
        f *= 2; x += 32;
    }
    return v;
}

function mandelbrot(x, y)
{
    var cr = x;
    var ci = y;
    var zr = 0;
    var zi = 0;
    var iterations = 0;
    
    while (zr * zr + zi * zi < 4)
    {
        const new_zr = zr * zr - zi * zi + cr;
        const new_zi = 2 * zr * zi + ci;
        zr = new_zr;
        zi = new_zi;
        iterations += 1;
        if (iterations >= max_iterations) break;
    }
    
    return iterations;
}

const max_iterations = 19   /// min = 1, max = 100, step = 1
const m_scale =  80         /// min = 10, max = 10000, step = 0.01
const offset_x = 0.7        /// min = -10, max = 10, step = 0.001
const offset_y = 0          /// min = -10, max = 10, step = 0.001

function get_image_intensity(x,y)
{
    var center_x = x;
    var center_y = y;

    const m_x = (center_x) / m_scale - offset_x;
    const m_y = (center_y) / m_scale - offset_y;

    const value = mandelbrot(m_x, m_y) / max_iterations;
    if (style == 1) return value * 2.5;
    if (style == 2) return 1 - value**3;
    return value;
}

function curlNoiseM(x, y) {
    const eps = 1;
    
    var dx = (fbm(x, y + eps) - fbm(x, y - eps))/(2 * eps);
    var dy = (fbm(x + eps, y) - fbm(x - eps, y))/(2 * eps);

    dx += (get_image_intensity(x, y + eps) - get_image_intensity(x, y - eps))/(2 * eps) * 1;
    dy += (get_image_intensity(x + eps, y) - get_image_intensity(x - eps, y))/(2 * eps) * 1;
    
    const l = Math.hypot(dx, dy) / radius * .99;
    const c = [dx / l, -dy / l];	
    
    return c;
}

function curlNoise(x, y) {
    const eps = 0.01;
    
    const dx = (fbm(x, y + eps) - fbm(x, y - eps))/(2 * eps);
    const dy = (fbm(x + eps, y) - fbm(x - eps, y))/(2 * eps);
    
    const l = Math.hypot(dx, dy) / radius * .99;
    return [dx / l, -dy / l];	
}

function getRadius(p2) {
    const l = get_image_intensity(p2[0], p2[1]);
    return (minRadius * l + radius * (1-l)) / 2;
}

function walk(i) {
    const p = turtle.pos();

    const curl = curlNoiseM(p[0], p[1]);
    const dest = [p[0]+curl[0], p[1]+curl[1]];
    dest[2] = getRadius(dest);
    
    if (turtle.traveled < maxPathLength && Math.abs(dest[0]) < 110 && Math.abs(dest[1]) < 110 && grid.insert(dest)) {
        turtle.goto(dest);
        turtle.traveled += Math.hypot(curl[0], curl[1]);
    } else {
        turtle.traveled = 0;
        let r, i = 0;
        do { 
            r =[Math.random()*200-100, Math.random()*200-100];
            r[2] = getRadius(r);
            i ++;
        } while(!grid.insert(r) && i < maxTries);
        if (i >= maxTries) {
            return false;
        }
        turtle.jump(r);
    }
    return true;
}

////////////////////////////////////////////////////////////////
// Simplex Noise utility code. Created by Reinder Nijhoff 2020
// https://turtletoy.net/turtle/6e4e06d42e
// Based on: http://webstaff.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf
////////////////////////////////////////////////////////////////
function SimplexNoise(seed = 1) {
	const grad = [  [1, 1, 0], [-1, 1, 0], [1, -1, 0], [-1, -1, 0],
	            	[1, 0, 1], [-1, 0, 1], [1, 0, -1], [-1, 0, -1],
            		[0, 1, 1], [0, -1, 1], [0, 1, -1], [0, -1, -1] ];
	const perm = new Uint8Array(512);
            		
	const F2 = (Math.sqrt(3) - 1) / 2, F3 = 1/3;
	const G2 = (3 - Math.sqrt(3)) / 6, G3 = 1/6;

	const dot2 = (a, b) => a[0] * b[0] + a[1] * b[1];
	const sub2 = (a, b) => [a[0] - b[0], a[1] - b[1]];
	const dot3 = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
	const sub3 = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];

	class SimplexNoise {
		constructor(seed = 1) {
			for (let i = 0; i < 512; i++) {
				perm[i] = i & 255;
			}
			for (let i = 0; i < 255; i++) {
				const r = (seed = this.hash(i+seed)) % (256 - i)  + i;
				const swp = perm[i];
				perm[i + 256] = perm[i] = perm[r];
				perm[r + 256] = perm[r] = swp;
			}
		}
		noise2D(p) {
			const s = dot2(p, [F2, F2]);
			const c = [Math.floor(p[0] + s), Math.floor(p[1] + s)];
			const i = c[0] & 255, j = c[1] & 255;
			const t = dot2(c, [G2, G2]);

			const p0 = sub2(p, sub2(c, [t, t]));
			const o  = p0[0] > p0[1] ? [1, 0] : [0, 1];
			const p1 = sub2(sub2(p0, o), [-G2, -G2]);
			const p2 = sub2(p0, [1-2*G2, 1-2*G2]);
			
			let n =  Math.max(0, 0.5-dot2(p0, p0))**4 * dot2(grad[perm[i+perm[j]] % 12], p0);
			    n += Math.max(0, 0.5-dot2(p1, p1))**4 * dot2(grad[perm[i+o[0]+perm[j+o[1]]] % 12], p1);
		    	n += Math.max(0, 0.5-dot2(p2, p2))**4 * dot2(grad[perm[i+1+perm[j+1]] % 12], p2);
			
			return 70 * n;
		}
		hash(i) {
            i = 1103515245 * ((i >> 1) ^ i);
            const h32 = 1103515245 * (i ^ (i>>3));
            return h32 ^ (h32 >> 16);
		}
	}
	return new SimplexNoise(seed);
}


////////////////////////////////////////////////////////////////
// Poisson-Disc utility code. Created by Reinder Nijhoff 2019
// https://turtletoy.net/turtle/b5510898dc
////////////////////////////////////////////////////////////////
function PoissonDiscGrid(radius) {
    class PoissonDiscGrid {
        constructor(radius) {
            this.cellSize = 1/Math.sqrt(2)/radius;
            this.cells = [];
        }
        insert(p) {
            const x = p[0]*this.cellSize|0, y=p[1]*this.cellSize|0;
            for (let xi = x-1; xi<=x+1; xi++) {
                for (let yi = y-1; yi<=y+1; yi++) {
                    const ps = this.cell(xi,yi);
                    for (let i=0; i<ps.length; i++) {
                        if ((ps[i][0]-p[0])**2 + (ps[i][1]-p[1])**2 < (ps[i][2]+p[2])**2) {
                            return false;
                        }
                    }
                }       
            }
            this.cell(x, y).push(p);
            return true;
        }
        cell(x,y) {
            const c = this.cells;
            return (c[x]?c[x]:c[x]=[])[y]?c[x][y]:c[x][y]=[];
        }
    }
    return new PoissonDiscGrid(radius);
}