Mandelbrot Fractal

Mandelbrot Fractal

Log in to post a comment.

const turtle = new Turtle();
turtle.penup();

const max_iterations = 50;
const zoom = 1.0;
const pan_x = -0.5;
const pan_y = 0;

const resolution = 150; 

const scale = 200 / resolution;

function walk(i) {
    
    let screen_y = -100 + (i * scale);
    
    let pen_is_down = false;
    turtle.penup();
    turtle.goto(-100, screen_y);

    for (let j = 0; j <= resolution; j++) {
        let screen_x = -100 + (j * scale);
        
        let cx = (screen_x / 100) * 1.5 * (1/zoom) + pan_x;
        let cy = (screen_y / 100) * 1.5 * (1/zoom) + pan_y;
        
        let zx = 0;
        let zy = 0;
        let is_in_set = true;
        
        for (let iter = 0; iter < max_iterations; iter++) {
            let zx_temp = zx * zx - zy * zy + cx;
            zy = 2 * zx * zy + cy;
            zx = zx_temp;
            
            if (zx * zx + zy * zy > 4) {
                is_in_set = false;
                break;
            }
        }
        
        if (is_in_set) {
            if (!pen_is_down) {
                turtle.goto(screen_x, screen_y);
                turtle.pendown();
                pen_is_down = true;
            }
            turtle.goto(screen_x, screen_y);
        } else {
            if (pen_is_down) {
                turtle.goto(screen_x, screen_y); 
                turtle.penup();
                pen_is_down = false;
            }
        }
    }
    
    turtle.penup();

    return i < resolution;
}