xxxxxxxxxx
181
let drawCoordinates = true;
let s = 800;
let width = s;
let ratio = 1;
let height = width*ratio;
let sliderZoom;
let sliderXAxis;
let sliderYAxis;
let complexPlaneLeft = -2;
let complexPlaneRight = 0.6;
let complexPlaneBottom = -1.3*ratio;
let complexPlaneTop = 1.3*ratio;
function setup() {
createCanvas(width, height);
background(255);
pixelDensity(1);
sliderZoom = createSlider(1, 10, 1, 0.2);
sliderZoom.position(0, height + 30);
sliderZoom.style('width', width + 'px');
sliderXAxis = createSlider(complexPlaneLeft, complexPlaneRight, 0, 0.00001);
sliderXAxis.position(0, height);
sliderXAxis.style('width', width + 'px');
sliderYAxis = createSlider(complexPlaneBottom, complexPlaneTop, 0, 0.00001);
sliderYAxis.position(width + 20, 0);
sliderYAxis.style('width', height + 'px');
sliderYAxis.style('transform-origin', '0 0');
sliderYAxis.style('transform', 'rotate(90deg)');
button = createButton('Toggle Coordinates');
button.position(width/2 - 25, height + 60);
button.mousePressed(() => {
drawCoordinates = !drawCoordinates
});
doit();
}
function doit(){
resetMatrix();
loadPixels();
let zoom = sliderZoom.value();
let xOffset = sliderXAxis.value();
let yOffset = sliderYAxis.value();
for(let k = 0; k < width; k++) {
for(let l = 0; l < height; l++) {
let xMin = complexPlaneLeft / zoom + xOffset;
let xMax = complexPlaneRight / zoom + xOffset;
let x = map(k, 0, width, xMin, xMax);
let yMin = complexPlaneBottom / zoom + yOffset;
let yMax = complexPlaneTop / zoom + yOffset;
let y = map(l, 0, height, yMin, yMax);
fastMandelbrot(x, y, (l*width+k)*4);
//let c = new Complex(x, y);
//mandelbrot(c, k, l);
}
}
updatePixels();
if(drawCoordinates) {
drawPlane();
}
}
var iterations = 800;
var magic_num = 4;
function mandelbrot(c, k, l) {
let z = new Complex(0, 0);
let i;
for(i = 0; i < iterations; i++) {
z = z.multiply(z).add(c);
if(z.r > magic_num || z.i > magic_num || -z.r > magic_num || -z.i > magic_num) {
break;
}
}
let index = (l*width+k)*4;
if(i != iterations){
pixels[index + 0] = (i/iterations)*255*10;
pixels[index + 1] = pixels[index + 0];
pixels[index + 2] = pixels[index + 0];
}
pixels[index + 3] = 255;
}
function fastMandelbrot(cR, cI, index) {
let zR = 0;
let zI = 0;
let i;
for(i = 0; i < iterations; i++) {
let tempZr = zR;
zR = (zR * zR - zI * zI) + cR;
zI = 2*tempZr*zI + cI;
if(zR > magic_num || zI > magic_num || -zR > magic_num || -zI > magic_num) {
break;
}
}
if(i != iterations){
pixels[index + 0] = (i/iterations)*255*10;
pixels[index + 1] = pixels[index + 0];
pixels[index + 2] = pixels[index + 0];
}
pixels[index + 3] = 255;
}
function mouseClicked() {
background(0);
doit();
}
function drawPlane() {
stroke("red");
let xZero = map(0, complexPlaneLeft, complexPlaneRight, 0, width);
let yZero = map(0, complexPlaneBottom, complexPlaneTop, 0, width);
let zoom = sliderZoom.value();
let xOffset = sliderXAxis.value();
let yOffset = sliderYAxis.value();
let xMin = complexPlaneLeft / zoom + xOffset;
let xMax = complexPlaneRight / zoom + xOffset;
let yMin = complexPlaneBottom / zoom + yOffset;
let yMax = complexPlaneTop / zoom + yOffset;
let middleX = map(0, xMin, xMax, 0, height);
let zeroX = map(0, xMin, xMax, 0, width);
let oneX = map(1, xMin, xMax, 0, width);
let nOneX = map(-1, xMin, xMax, 0, width);
let nTwoX = map(-2, xMin, xMax, 0, width);
let middleY = map(0, yMin, yMax, 0, height);
let i = map(1, yMin, yMax, 0, height);
let nI = map(-1, yMin, yMax, 0, height);
strokeWeight(0.5);
noFill();
line(0, middleY, width, middleY);
line(middleX, 0, middleX, height);
strokeWeight(1);
textAlign(CENTER, CENTER);
fill("red");
text("1", oneX, middleY);
text("0", zeroX, middleY);
text("-1", nOneX, middleY);
text("-2", nTwoX, middleY);
text(" i", middleX, i);
text(" -i", middleX, nI);
}