Mandelbrot Set
The Mandelbrot set is one of the most famous fractal.
The mandelbrot set is defined by the set of complex numbers c for which the complex numbers of the sequence zn remain bounded in absolute value. The sequence zn is defined by:
$$ z_0 = 0 $$
$$ z_n+1 = z_n^2 + c $$
where c is a complex number and zn is the nth complex number in the sequence.
Code
elem=document.querySelector(".example-container");
setCanvas(elem);
function draw() {
clearCanvas();
w=WIDTH;
h=HEIGHT;
for(i=0;i<w;i+=1){
for(j=0;j<h;j+=1){
var a= mapRange(i,0,w,-2,1);
var b= mapRange(j,0,h,-1.5,1.5);
var ca=a;
var cb=b;
var n=0;
while(n<100){
var aa=a*a-b*b;
var bb=2*a*b;
a=aa+ca;
b=bb+cb;
if(a+b>4){
break;
}
n++;
}
var c=mapRange(n,0,100,0,360);
if(c>20){
new rect(i,j,2.5,2.5,`hsl(${c},100%,50%)`,1,`hsl(${c},100%,50%)`,0,'top-left',0);
}
}
};
// requestAnimationFrame(draw);
}
draw();