Exemple #1
0
void drawSet(double zoom_factor, double offset)
{
    clock_t start = clock();
    /* (x,y) is the pixel coordinate with respect to the center (x0,y0)
     (xp,yp) is the pixel coordinate with respect to the image corner. */
    double xp, yp, r, g, b;
    int iteration = 0;
    struct complex c;
    int color;

    /* Complex plane scaling */
    double xp_max = ( 1.25 * scaleX - offset ) / zoom_factor;
    double xp_min = ( -1.75 * scaleX - offset ) / zoom_factor;
    double yp_max = ( 1.5 * scaleY - offset ) / zoom_factor;
    double yp_min = ( -1.5 * scaleY - offset ) / zoom_factor;

    // Lock surface if needed
    if (SDL_MUSTLOCK(screen))
        if (SDL_LockSurface(screen) < 0)
            return;

    for (xp = 0; xp < sizeX; xp++)
    {
        for (yp = 0; yp < sizeY; yp++)
        {
            /* Scaling */
            c.real = ( (xp_max - xp_min) * xp ) / sizeX + xp_min;
            c.imag = ( (yp_max - yp_min) * yp ) / sizeY + yp_min;

            // printf("x is: %f, y is: %f\n", x, y);
            iteration = mandelbrotCalc(c);
            // printf("iteration: %d\n", iteration);

            /* Define the color sets that are going to be used. */
            if (iteration > max_iteration) {
                color = 0x000000;
            } else {
                double i = (iteration * 255 / max_iteration);
                r = round(sin(0.2 * i + 0) * 127 + 128);
                g = round(sin(0.2 * i + 2) * 127 + 128);
                b = round(sin(0.2 * i + 4) * 127 + 128);
                color = createRGB(r,g,b);
            }
            putpixel(xp,yp,color);
            iteration = 0;
        }
    }

    // Unlock if needed
    if (SDL_MUSTLOCK(screen))
        SDL_UnlockSurface(screen);

    // update the whole screen
    SDL_UpdateRect(screen, 0, 0, sizeX, sizeY);

    clock_t end = clock();
    clock_t millis = end - start;
    printf("Elapsed time: %lu\n", millis);
}
int escapeSteps (double x, double y) {
   Complex c = {x, y};

   Complex z = {0, 0};
   int steps = 0;
   while (magnitude (z) <= 4 && steps < MAX_STEPS) {
      z = mandelbrotCalc(z,c);
      steps ++;
   }
   return steps; 
}