Guest User

Mandelbrot thingy with Allegro

a guest
May 4th, 2012
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <complex>
  3. #include <vector>
  4. #include <allegro5/allegro.h>
  5. #include <allegro5/allegro_image.h>
  6.  
  7. const int MANDELBROT_RESOLUTION_W = 2000;
  8. const int MANDELBROT_RESOLUTION_H = 2000;
  9. const unsigned int ITERATIONS = 100;
  10.  
  11. inline float complex_length_squared(const std::complex<float>& cmplx) {
  12.     return cmplx.real()*cmplx.real() + cmplx.imag()*cmplx.imag();
  13. }
  14.  
  15. inline float lin_interpol(float p1, float p2, float rate) {
  16.     return p1 + (p2-p1)*rate;
  17. }
  18.  
  19. int main()
  20. {
  21.    al_init();
  22.    al_init_image_addon();
  23.    ALLEGRO_BITMAP* bitmap = al_create_bitmap(MANDELBROT_RESOLUTION_W, MANDELBROT_RESOLUTION_H);
  24.  
  25.    std::vector<std::complex<float>> setCandidates;
  26.    setCandidates.reserve(MANDELBROT_RESOLUTION_W * MANDELBROT_RESOLUTION_H);
  27.    for(int i = 0; i<MANDELBROT_RESOLUTION_W; ++i) {
  28.        for(int j = 0; j<MANDELBROT_RESOLUTION_H; ++j) {
  29.            setCandidates.push_back(std::complex<float>(lin_interpol(-2,2,i/static_cast<float>(MANDELBROT_RESOLUTION_W)),
  30.                                                        lin_interpol(-2,2,j/static_cast<float>(MANDELBROT_RESOLUTION_H))));
  31.        }
  32.    }
  33.    std::vector<std::complex<float>> discriminants(MANDELBROT_RESOLUTION_W*MANDELBROT_RESOLUTION_H,1);
  34.    std::vector<int> escapeChecker(MANDELBROT_RESOLUTION_W * MANDELBROT_RESOLUTION_H, 0);
  35.    for(int currentIteration = 1; currentIteration <= ITERATIONS; ++currentIteration) {
  36.        for(int i = 0; i< MANDELBROT_RESOLUTION_W; ++i) {
  37.             for(int j=0; j<MANDELBROT_RESOLUTION_H; ++j) {
  38.                 if(escapeChecker[i]) continue;
  39.                 discriminants[i*MANDELBROT_RESOLUTION_H+j] *= discriminants[i*MANDELBROT_RESOLUTION_H+j];
  40.                 discriminants[i*MANDELBROT_RESOLUTION_H+j] += setCandidates[i*MANDELBROT_RESOLUTION_H+j];
  41.                 float length = complex_length_squared(discriminants[i*MANDELBROT_RESOLUTION_H+j]);
  42.                 if(length > 2) {
  43.                    escapeChecker[i*MANDELBROT_RESOLUTION_H+j] = currentIteration;
  44.                 }
  45.             }
  46.        }
  47.  
  48.    }
  49.    al_set_target_bitmap(bitmap);
  50.    al_clear_to_color(al_map_rgb(0,0,0));
  51.    for(int i=0; i<MANDELBROT_RESOLUTION_W; ++i) {
  52.        for(int j=0; j<MANDELBROT_RESOLUTION_H; ++j) {
  53.            int escaped = escapeChecker[i*MANDELBROT_RESOLUTION_H+j];
  54.            if(escaped) {
  55.                float rate = static_cast<float>(escaped)/ITERATIONS;
  56.                al_draw_pixel(i,j,al_map_rgb(static_cast<unsigned char>(lin_interpol(0,255,rate)),
  57.                                             0,
  58.                                             static_cast<unsigned char>(lin_interpol(255,0,rate))));
  59.            }
  60.        }
  61.    }
  62.  
  63.    al_save_bitmap("mandelbrot.png",bitmap);
  64.  
  65.    al_destroy_bitmap(bitmap);
  66. }
Advertisement
Add Comment
Please, Sign In to add comment