xxxxxxxxxx
86
function setup() {
createCanvas(200, 100);
}
function fac(n){
if (n != 1 && n!=0){
n = n*fac(n-1)
}else{
return 1;
}
return n;
}
function sum(x, y, a, b, ord){
let pi=3.14159265359;
let s = {
re:0,
im:0
};
for (let k = 0;k<=ord;k++){
if (k%2===0){
s.re+=(fac(ord)/(fac(k)*fac(ord-k)))*(x**(ord-k))*(y**k)*(Math.cos(k*pi/2));
}
if (k%2!==0){
s.im+=(fac(ord)/(fac(k)*fac(ord-k)))*(x**(ord-k))*(y**k)*(Math.sin(k*pi/2));
}
}
return s;
}
function isInMandelbrotSet(x, y){
pixelDensity(1);
let z = {
a:0,
b:0
}
let c = {
a:x,
b:y
}
for (let n=1;n<50;n++){
let s = sum(z.a, z.b, c.a, c.b, 2);
let z1 = {
a:s.re+c.a,
b:s.im+c.b
}
z.a = z1.a; z.b = z1.b;
}
if (z.a**2 + z.b**2>4){
return false;
}else{
return true;
}
}
function draw() {
background(220);
loadPixels(); // Load pixel data into the pixels[] array
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
let index = (x + y * width) * 4; // Calculate the index of the pixel
let a = map(x, 0, width, -2, 2);
let b = map(y, 0, height, -2, 2);
if (isInMandelbrotSet(a, b) == true){
// Modify pixel color values
//pixels[index] = 255; // Red component
//pixels[index + 1] = 0; // Green component
//pixels[index + 2] = 0; // Blue component
//pixels[index + 3] = 255; // Alpha component
}
}
}
updatePixels(); // Apply the changes to the canvas
}