Hello, my name is Stig, I`m from Oslo and I`m 16 in August. Fractals are very cool indeed, and I`ve kept a little eye on a 3D mandelbrot forum here before I decided to join.
And hey, I wrote this program in two days using C++ and Allegro. It`s the good old Mandelbrot set, which allows you to zoom with the mouse, reset with `R` if you get lost and even terminate the program with Esc! *proud* I will keep experimenting with the program, but I felt like displaying it right now to show off my 1337 fractal skills.
#include <allegro.h>
#include <math.h>
using namespace std;
int mandel(int iterations, double bailout, double cr, double ci);
int main() {
allegro_init();
set_color_depth(8);
if(set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0)) {
allegro_message(allegro_error);
return 1;
}
install_keyboard();
install_mouse();
int iterations = 600;
double bailout = 50.0;
double r1 = -2.5;
double i1 = -1.5;
double r2 = 1.5;
double i2 = 1.5;
double cr, ci = 0;
double width = r2 - r1;
double height = i2 - i1;
double rIncr = width / SCREEN_W;
double iIncr = height / SCREEN_H;
int x, y = 0;
int col = 0;
bool mainLoop = true;
bool inputLoop = true;
BITMAP *buffer = create_bitmap(SCREEN_W, SCREEN_H);
show_mouse(screen);
while(mainLoop) {
scare_mouse();
inputLoop = true;
ci = i1;
for(y = 0; y < SCREEN_H; y++) {
ci += iIncr;
cr = r1;
for(x = 0; x < SCREEN_W; x++) {
cr += rIncr;
col = mandel(iterations, bailout, cr, ci) % 256;
putpixel(buffer, x, y, col);
}
if(!(y % 48)) blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
}
blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
unscare_mouse();
while(inputLoop) {
if(key[KEY_ESC]) {
mainLoop = false;
inputLoop = false;
}
if(mouse_b & 1) {
cr = fabs(mouse_x) / SCREEN_W * width + r1;
ci = fabs(mouse_y) / SCREEN_H * height + i1;
r1 = cr - width / 8.0;
i1 = ci - height / 8.0;
r2 = cr + width / 8.0;
i2 = ci + height / 8.0;
width = r2 - r1;
height = i2 - i1;
rIncr = width / SCREEN_W;
iIncr = height / SCREEN_H;
inputLoop = false;
}
if(key[KEY_R]) {
r1 = -2.5;
i1 = -1.5;
r2 = 1.5;
i2 = 1.5;
width = r2 - r1;
height = i2 - i1;
rIncr = width / SCREEN_W;
iIncr = height / SCREEN_H;
inputLoop = false;
}
}
}
destroy_bitmap(buffer);
allegro_exit();
return 0;
}
END_OF_MAIN()
int mandel(int iterations, double bailout, double cr, double ci) {
double sqrBail = bailout * bailout;
double zr, zi = 0.0;
double zr_n, zi_n = 0.0;
int iterCounter = 0;
double dist = cr * cr + ci * ci;
while(dist < sqrBail && iterCounter < iterations) {
zr_n = (zr * zr) - (zi * zi);
zi_n = 2 * zr * zi;
zr = zr_n + cr;
zi = zi_n + ci;
dist = (zr * zr) + (zi * zi);
iterCounter++;
}
return iterCounter % iterations;
}
Well, sorry for not bothering to add comments to the code, but I _hope_ it's pretty self-documenting. This is actually a re-write, 'cause I first wrote it on another pc without having brought a memory stick
