Guest User

Untitled

a guest
Jul 3rd, 2014
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. #include <GL/glut.h>
  2. #include <complex>
  3.  
  4. const char * title = "Mandelbrot set";
  5. const double maxMod = 2.0;
  6. const size_t maxIt = 1000;
  7. const size_t maxX = 400;
  8. const size_t maxY = 400;
  9.  
  10. int points[maxX][maxY];
  11.  
  12. std::complex<double> sqr(const std::complex<double> &v)
  13. {
  14.     return pow(v, 2);
  15. }
  16.  
  17. void makeMandelbrotSet(void)
  18. {
  19.     for (int y = 0; y < maxY; ++y)
  20.         for (int x = 0; x < maxX; ++x)
  21.         {
  22.             std::complex<double> z(0.0, 0.0);
  23.             std::complex<double> c(1.0*(x-100) / 100, 1.0*(y-100) / 100);
  24.             size_t i = 0;
  25.             while (i < maxIt && abs(z) < maxMod)
  26.             {
  27.                 z = sqr(z) + c;
  28.                 ++i;
  29.             }
  30.             if (abs(z) >= 2) points[y][x] = i;
  31.             else points[y][x] = -1;
  32.         }
  33. }
  34.  
  35. void Resize(int width, int height)
  36. {
  37.     glutReshapeWindow(maxX, maxY);
  38. }
  39.  
  40. void Draw(void)
  41. {
  42.     glClear(GL_COLOR_BUFFER_BIT);
  43.    
  44.     glBegin(GL_POINTS);
  45.     for (int y = 0; y < maxY; ++y)
  46.         for (int x = 0; x < maxX; ++x)
  47.             if (points[y][x] != -1)
  48.             {
  49.                 float col = (50.0 - points[y][x]) / 50.0;
  50.                 glColor3f(col, col, col);
  51.                 glVertex2f(x, y);
  52.             }
  53.     glEnd();
  54.     glutSwapBuffers();
  55. }
  56.  
  57. int main(int argc, char ** argv)
  58. {
  59.     glutInit(&argc, argv);
  60.  
  61.     makeMandelbrotSet();
  62.  
  63.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  64.     glutInitWindowSize(maxX, maxY);
  65.     glutInitWindowPosition(200, 200);
  66.     glutCreateWindow(title);
  67.  
  68.     glClearColor(0, 0, 0, 1.0);
  69.     glPointSize(2);
  70.  
  71.     glMatrixMode(GL_PROJECTION);
  72.     glLoadIdentity();
  73.     glOrtho(0, maxX/2, maxY/2, 0, -1, 1);
  74.     glMatrixMode(GL_MODELVIEW);
  75.     glLoadIdentity();
  76.  
  77.     glutReshapeFunc(Resize);
  78.     glutDisplayFunc(Draw);
  79.  
  80.     glutMainLoop();
  81.     return EXIT_SUCCESS;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment