Blurred fractal

Visualization of Mandelbrot set

Log in to post a comment.

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

// Global code will be evaluated once.
const turtle = new Turtle();
turtle.penup();
turtle.goto(-50, -50);
turtle.pendown();

// complex number z = a + bi

function complex (a, b) {
    return {
        a: a,
        b: b
    };
}

function cMult(c1, c2) {
    return complex(
        c1.a * c2.a - c1.b * c2.b,
        c1.a * c2.b + c1.b * c2.a
    );
}

function cAdd(c1, c2) {
    return complex(c1.a + c2.a, c1.b + c2.b);
}

function abss(c) {
    return c.a*c.a+c.b*c.b;
}

function mandelbrot(z, maxIter) {
    let c = z;
    for (let i = 0; i < maxIter; i++) {
        if (abss(z) > 4) {
            return i;
        }
        z = cAdd(cMult(z, z), c);
    }
    return maxIter;
}

let resolution = 300;
let maxIter = 50;

function walk(i) {
    let fx = (i % resolution) / resolution;
    let fy = Math.floor(i/resolution) / resolution;
    
    let x = -2 + 4 * fx;
    let y = -2 + 4 * fy;
    let m = mandelbrot(complex(x, y), maxIter);
    if (m < 10) {
        turtle.penup();
        turtle.goto(fx * 190 - 95, fy * 190 - 95);
        turtle.pendown();
        turtle.forward(5);
        turtle.right(m/1000);
    }
    return i < 90000;
}