]> git.itanic.dy.fi Git - mandelbrot/commitdiff
Factor out iteration calculation
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Sun, 1 May 2011 16:01:02 +0000 (19:01 +0300)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Sun, 1 May 2011 16:01:02 +0000 (19:01 +0300)
Signed-off-by: Timo Kokkonen <kaapeli@itanic.dy.fi>
mandelbrot.c

index ef17b9e621e814cf0223cacdab753afa4be0b49b..ae05fbd50f9591edb9d2cda85785b062adc6d66a 100644 (file)
@@ -17,28 +17,34 @@ static void putpixel(struct SDL_Surface *screen, const int x, const int y,
 
 #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);