I'm trying to create a simple Mandelbrot Set renderer, and it won't work- it compiles, and the rendering and zoom work, but they're not rendering the set. They're rendering something else:
I can't figure out what's wrong with my code. It all seems to check out. Help?
//Mandelbrot Set
float rR = 10; //real axis radius
float iR = 10; //imaginary axis radius
float rC = 0; //center of render on real axis
float iC = 0; //center of render on imaginary axis
int iterations = 20;
float breakOff = 2;
float zoomFactor = 2;
void setup(){
size(500,500);
render();
}
void draw(){}
void render(){
for(int r = 0; r < width; r++){
for(int i = 0; i < height; i++){
//Implement the equation for each pixel
Complex Z = new Complex(0,0);
Complex C = new Complex(map(r,0,width,rC-(rR/2),rC+(rR/2)),map(i,0,height,iC-(iR/2),iC+(iR/2)));
set(r,i,color(255));
for(int n = 0; n <= iterations; n++){
//Just like the equation
Z.mult(Z);
Z.add(C);
if(Z.norm()>breakOff){
set(r,i,color(map(n,0,iterations,0,255)));
break;
}
}
}
}
}
void mousePressed(){
rC = map(mouseX, 0, width, rC-(rR/2), rC+rR/2);
iC = map(mouseY, 0, height, iC-(iR/2), iC+iR/2);
if(mouseButton == RIGHT){
rR *= zoomFactor;
iR *= zoomFactor;
}else if(mouseButton == LEFT){
rR /= zoomFactor;
iR /= zoomFactor;
}
render();
}
//Complex number class
public class Complex{
float re = 0;
float im = 0;
public Complex () {
this.re = 0;
this.im = 0;
}
public Complex(float rE, float iM) {
this.re = rE;
this.im = iM;
}
public void add(Complex op) {
this.re = (this.re + op.re);
this.im = (this.im + op.im);
}
//I think my multiplication function might be off...