From: Timo Kokkonen Date: Sun, 1 May 2011 16:01:02 +0000 (+0300) Subject: Factor out iteration calculation X-Git-Url: http://git.itanic.dy.fi/?p=mandelbrot;a=commitdiff_plain;h=10394fae32c1a947bc2e000003c87b41599571ea Factor out iteration calculation Signed-off-by: Timo Kokkonen --- diff --git a/mandelbrot.c b/mandelbrot.c index ef17b9e..ae05fbd 100644 --- a/mandelbrot.c +++ b/mandelbrot.c @@ -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);