Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glut.h>
- #include <complex>
- const char * title = "Mandelbrot set";
- const double maxMod = 2.0;
- const size_t maxIt = 1000;
- const size_t maxX = 400;
- const size_t maxY = 400;
- int points[maxX][maxY];
- std::complex<double> sqr(const std::complex<double> &v)
- {
- return pow(v, 2);
- }
- void makeMandelbrotSet(void)
- {
- for (int y = 0; y < maxY; ++y)
- for (int x = 0; x < maxX; ++x)
- {
- std::complex<double> z(0.0, 0.0);
- std::complex<double> c(1.0*(x-100) / 100, 1.0*(y-100) / 100);
- size_t i = 0;
- while (i < maxIt && abs(z) < maxMod)
- {
- z = sqr(z) + c;
- ++i;
- }
- if (abs(z) >= 2) points[y][x] = i;
- else points[y][x] = -1;
- }
- }
- void Resize(int width, int height)
- {
- glutReshapeWindow(maxX, maxY);
- }
- void Draw(void)
- {
- glClear(GL_COLOR_BUFFER_BIT);
- glBegin(GL_POINTS);
- for (int y = 0; y < maxY; ++y)
- for (int x = 0; x < maxX; ++x)
- if (points[y][x] != -1)
- {
- float col = (50.0 - points[y][x]) / 50.0;
- glColor3f(col, col, col);
- glVertex2f(x, y);
- }
- glEnd();
- glutSwapBuffers();
- }
- int main(int argc, char ** argv)
- {
- glutInit(&argc, argv);
- makeMandelbrotSet();
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
- glutInitWindowSize(maxX, maxY);
- glutInitWindowPosition(200, 200);
- glutCreateWindow(title);
- glClearColor(0, 0, 0, 1.0);
- glPointSize(2);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0, maxX/2, maxY/2, 0, -1, 1);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glutReshapeFunc(Resize);
- glutDisplayFunc(Draw);
- glutMainLoop();
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment