I've gotten comfortable with programming the Mandelbrot (2D) after having played with it for a long time and want to generalize it to a Mandelbulb (3D). I have two different methods at the moment for making a Mandelbrot:
1. z = sq(z)+c method (where I test every point through this equation):
- I like this method and understand why it works
- Fast
- It is easy enough to set the equation to higher powers (multiply more binomials)
- I have no idea how to generalize it to 3D
2. Rotation method (based on following these instructions):
http://www.skytopia.com/project/fractal/mandelbrot.html#pandora- It does correctly make a Mandelbrot but I don't understand why
- Slow
- I don't know how to set higher powers
- Making it work in 3D seems to just require Spherical Positioning instead of Polar Positioning
If anyone reading this has Processing installed you can check out simplified versions of either code:
- Equation method:
float xmin = -2.5;
float ymin = -2.0;
float wh = 4;
int maxIterations = 255;
void setup() {
size(800, 800, P2D);
}
void draw() {
loadPixels();
float xmax = xmin+wh;
float ymax = ymin+wh;
float dx = (xmax-xmin)/width;
float dy = (ymax-ymin)/height;
float x = xmin;
for (int i = 0; i < width; i++) {
float y = ymin;
for (int j = 0; j < height; j++) {
float zr = x;
float zi = y;
int n = 0;
while (n < maxIterations) {
float zrr = zr*zr;
float zii = zi*zi;
float twori = 2*zr*zi;
zr = zrr-zii+x;
zi = twori+y;
if (zrr+zii > 16) break;
n++;
}
if (n == maxIterations) pixels[i+j*width] = 0;
else pixels[i+j*width] = color(n);
y += dy;
}
x += dx;
}
updatePixels();
noLoop();
}
- Rotation method:
float xmin = -2.5;
float ymin = -2;
float wh = 4;
int maxIterations = 255;
void setup() {
size(800, 800, P2D);
noLoop();
}
void draw() {
loadPixels();
float xmax = xmin+wh;
float ymax = ymin+wh;
float dx = (xmax-xmin)/width;
float dy = (ymax-ymin)/height;
float x = xmin;
for (int i = 0; i < width; i++) {
float y = ymin;
for (int j = 0; j < height; j++) {
float a = atan2(y, x);
float d = sqrt(x*x+y*y);
float nx = x;
float ny = y;
int n = 0;
while (n < maxIterations) {
float na = atan2(ny, nx);
float nd = sqrt(nx*nx+ny*ny);
nx = cos(na*2)*nd*nd+cos(a)*d;
ny = sin(na*2)*nd*nd+sin(a)*d;
if (nx*nx+ny*ny > 16) break;
n++;
}
if (n == maxIterations) pixels[i+j*width] = 0;
else pixels[i+j*width] = color(n);
y += dy;
}
x += dx;
}
updatePixels();
}
I would strongly prefer to use the equation method although I don't know how to test 3D space like this. In 2D I just test the x-axis for real numbers and the y-axis for imaginary numbers so it would be great if this could be generalized for 3D space. If not then any help with setting higher powers for the rotation method would be appreciated.