Mandelbrot

I reused my complex class from shadergif.com/gifs/378

Log in to post a comment.

Canvas.setpenopacity(1);

const turtle = new Turtle();

turtle.penup();
        
class ComplexNumber{
    constructor(real, im){
    	this.r = real || 0.0;
        this.i = im || 0.0;
    }
    
    pow2(){
        let a = this.r;
        let b = this.i;
        this.r = a * a - b * b;
        this.i = 2 * (a * b);
    	return this;
    }
    
    len(){
    	return Math.sqrt(this.r * this.r + this.i * this.i);
    }
    
    times(k){
        this.r *= k;
        this.i *= k;
        return this;
    }
    
    add(complex){
        this.r += complex.r;
        this.i += complex.i;
        
    	return this;
    }
}

function pixel(i,j){
    let k;
    
    let x = i / 100;
    let y = j / 100;
    
    let z = new ComplexNumber(0,0);
    let c = new ComplexNumber(x,y);
    c.times(1.4);
    c.r -= 0.6;
            
    for(k = 0; k < 25; k++){
    	z = z.pow2().add(c);
        if(z.len() > 4.0){
        	break;
        }
	}
    
    turtle.goto(i * 1.0, j * 0.98);
    
    if(z.len() < 3.0 && z.len()){
        turtle.pendown();
        turtle.goto(i,j);
    } else {
        turtle.penup();        
    }

}

function walk(i) {
    let ii = i * 4;
    
    pixel(ii % 200 - 100, (ii - ii % 200) / 100 - 100);
    
    return i < 100 * 100;
}