I am trying to create this mandelbrot fractal with a UI but as you can see the UI has a weird visual glitch where it flickers the colors of the Mandelbrot fractal i can’t find the cause of this
// MandelbrotStandalone.pde
// Mandelbrot specific parameters
int mandelbrotIterations = 10; // Default to 10 iterations
float mandelbrotZoom = 1;
float mandelbrotOffsetX = 0;
float mandelbrotOffsetY = 0;
PGraphics pg;
boolean rendering = false;
int activeSlider = -1;
void setup() {
size(1920, 1080);
pg = createGraphics(width, height);
}
void draw() {
background(255);
if (!rendering) {
rendering = true;
thread("renderMandelbrot");
}
image(pg, 0, 0);
drawUI();
}
void renderMandelbrot() {
pg.beginDraw();
pg.loadPixels();
for (int x = 0; x < pg.width; x++) {
for (int y = 0; y < pg.height; y++) {
float a = map(x, 0, pg.width, -2.5 / mandelbrotZoom + mandelbrotOffsetX, 1 / mandelbrotZoom + mandelbrotOffsetX);
float b = map(y, 0, pg.height, -1 / mandelbrotZoom + mandelbrotOffsetY, 1 / mandelbrotZoom + mandelbrotOffsetY);
float ca = a;
float cb = b;
int n = 0;
while (n < mandelbrotIterations) {
float aa = a * a - b * b;
float bb = 2 * a * b;
a = aa + ca;
b = bb + cb;
if (abs(a + b) > 16) {
break;
}
n++;
}
float bright = map(n, 0, mandelbrotIterations, 0, 255);
if (n == mandelbrotIterations) {
bright = 0;
}
pg.pixels[x + y * pg.width] = color(bright);
}
}
pg.updatePixels();
pg.endDraw();
rendering = false;
}
void drawUI() {
fill(40);
noStroke();
rect(0, 0, 300, height);
fill(255);
textAlign(LEFT, CENTER);
textSize(14);
// Iterations slider
fill(255);
text("Iterations: " + mandelbrotIterations, 20, 30);
fill(200);
rect(20, 40, 260, 10, 5);
fill(100);
float iterKnobX = map(mandelbrotIterations, 1, 100, 20, 280);
rect(iterKnobX - 5, 35, 10, 20, 5);
// Removed the zoom slider
}
void mousePressed() {
// Handling mouse interaction for Mandelbrot-specific sliders
if (mouseX < 300) {
if (mouseY > 25 && mouseY < 45) {
activeSlider = 0; // Iterations slider
}
}
}
void mouseDragged() {
// Update slider values based on dragging
if (activeSlider == 0) {
mandelbrotIterations = int(map(constrain(mouseX, 20, 280), 20, 280, 1, 100));
}
}
void mouseReleased() {
activeSlider = -1;
}
void mouseWheel(MouseEvent event) {
// Handle zooming with the mouse wheel
float e = event.getCount();
float zoomFactor = 1.1;
// Calculate the mouse position in world coordinates
float mouseXWorld = map(mouseX, 0, width, -2.5 / mandelbrotZoom + mandelbrotOffsetX, 1 / mandelbrotZoom + mandelbrotOffsetX);
float mouseYWorld = map(mouseY, 0, height, -1 / mandelbrotZoom + mandelbrotOffsetY, 1 / mandelbrotZoom + mandelbrotOffsetY);
if (e < 0) {
mandelbrotZoom *= zoomFactor;
} else {
mandelbrotZoom /= zoomFactor;
}
// Calculate the new mouse position in world coordinates after zooming
float newMouseXWorld = map(mouseX, 0, width, -2.5 / mandelbrotZoom + mandelbrotOffsetX, 1 / mandelbrotZoom + mandelbrotOffsetX);
float newMouseYWorld = map(mouseY, 0, height, -1 / mandelbrotZoom + mandelbrotOffsetY, 1 / mandelbrotZoom + mandelbrotOffsetY);
// Adjust offsets to keep the zoom centered around the mouse
mandelbrotOffsetX += mouseXWorld - newMouseXWorld;
mandelbrotOffsetY += mouseYWorld - newMouseYWorld;
}