From 10394fae32c1a947bc2e000003c87b41599571ea Mon Sep 17 00:00:00 2001 From: Timo Kokkonen Date: Sun, 1 May 2011 19:01:02 +0300 Subject: [PATCH] Factor out iteration calculation Signed-off-by: Timo Kokkonen --- mandelbrot.c | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) 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); -- 2.45.0