/* Sequential Mandelbrot program */ #include #include #include #include #include #define X_RESN 800 /* x resolution */ #define Y_RESN 600 /* y resolution */ typedef struct complextype { float real, imag; } Compl; void init() { // Clear the window /* select clearing (background) color */ glClearColor(0.5, 0.5, 0.5, 0.0); // These RGB values make gray /* initialize viewing values */ glMatrixMode(GL_PROJECTION); // Select Matrix Mode glLoadIdentity(); // Provide Base Matrix glOrtho(-50.0, 1000.0, -50.0, 500.0, -1.0, 1.0); // Set window dimensions // Parameters: glOrtho(Left, Right, Bottom, Top, Front, Back) } void reshape(int width, int height) { // Set the new viewport size glViewport(0, 0, (GLint)width, (GLint)height); // Clear the window glClear(GL_COLOR_BUFFER_BIT); } void draw(void) { int k; float i,j; Compl z, c; float lengthsq, temp; float max=0.0,min=4.0, maxK=0.0; glClear(GL_COLOR_BUFFER_BIT); // Set the drawing color glColor3f(1.0, 1.0, 1.0); /* Calculate and draw points */ for(i=0; i < X_RESN; i+=0.5) for(j=0; j < Y_RESN; j+=0.5) { z.real = z.imag = 0.0; c.real = ((float) j - 400.0)/200.0; /* scale factors for 800 x 600 window */ c.imag = ((float) i - 400.0)/200.0; k = 0; do { /* iterate for pixel color */ temp = z.real*z.real - z.imag*z.imag + c.real; z.imag = 2.0*z.real*z.imag + c.imag; z.real = temp; lengthsq = z.real*z.real+z.imag*z.imag; k++; } while (lengthsq < 4.0 && k < 100); if (k==100) { if (lengthsq > max) max = lengthsq; if (lengthsq < min) min = lengthsq; glColor3f(0.5,0.0,0.5); glBegin(GL_POINTS); glVertex3f((float)j, (float)i-100,0.0); glEnd(); } else { if (k > maxK) maxK = k; glColor3f(k/99.0,0.0,0.0); glBegin(GL_POINTS); glVertex3f((float)j, (float)i-100,0.0); glEnd(); } } // Flush the buffer to force drawing of all objects thus far glFlush(); printf("MaxK=%f\n", maxK); } int main(int argc, char **argv) { /* Mandlebrot variables */ int i, j, k; float lengthsq, temp; // Open a window, name it "Hello World" glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(X_RESN, Y_RESN); glutInitWindowPosition(10,10); glutCreateWindow("Mandelbrot Set"); // Set the clear color to black glClearColor(0.0, 0.0, 0.0, 0.0); // Call an initialization function init(); // Assign reshape() to be the function called whenever // a reshape event occurs glutReshapeFunc(reshape); // Assign draw() to be the function called whenever a display // event occurs, generally after a resize or expose event glutDisplayFunc(draw); // Pass program control to tk's event handling code // In other words, loop forever glutMainLoop(); return 0; }