#define MAX_ITERATION 1000
+int get_mandelbrot_iterations(double x0, double y0)
+{
+ double x = 0, y = 0, xtemp;
+ int iteration = 0;
+
+ while ((x * x + y * y < 2 * 2) && iteration < MAX_ITERATION) {
+
+ xtemp = x * x - y * y + x0;
+ y = 2 * x * y + y0;
+
+ x = xtemp;
+ iteration++;
+ }
+
+ return iteration;
+}
+
int draw_mandelbrot(struct SDL_Surface *screen)
{
- double x, y, x0, y0, xtemp;
+ double x0, y0;
int iteration, xs,ys;
for (ys = 0, y0 = -1; ys < screen->h;
y0 += 2 / (double)screen->h, ys++) {
for (xs = 0, x0 = -2.5; xs < screen->w;
x0 += 3 / (double)screen->w, xs++) {
- iteration = 0;
- x = 0;
- y = 0;
-
- while (x * x + y * y < 2 * 2 &&
- iteration < MAX_ITERATION) {
-
- xtemp = x*x - y*y + x0;
- y = 2*x*y + y0;
- x = xtemp;
- iteration++;
- }
+ iteration = get_mandelbrot_iterations(x0, y0);
if (iteration == MAX_ITERATION)
putpixel(screen, xs, ys, 255, 255, 255);