import sys, random from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * def mandelbrot(c, maxIter): iter = 0 z = c while iter < maxIter: z = z*z + c if abs(z) > 2: return iter iter = iter + 1 return -1 width = 400 height = 400 maxIter = 32 colormap = [] for i in range(0,256): colormap.append(((i%4)*75,(i%4)*75,(i%4)*75)) center = (-0.75, 0.25) size = 1 def draw(): # glClear(GL_COLOR_BUFFER_BIT) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(-0.5, width+0.5, -0.5, height+0.5) glMatrixMode(GL_MODELVIEW) for i in range(0,width): x = (i/(width-1.0)*size) - size/2.0 + center[0] glBegin(GL_POINTS) for j in range(0,height): y = (j/(height-1.0)*size) - size/2.0 + center[1] m = mandelbrot(x+y*1j, maxIter) if m == -1: glColor3ub(255,255,255) else: glColor3ubv(colormap[m]) glVertex2i(i,j) glEnd() glFlush() def keyboard(key, x, y): global size, maxIter if key == chr(27): sys.exit(0) elif key == '+': size = size / 2.0 elif key == '-': size = size * 2.0 elif key == 'a': maxIter += 16 elif key == 's': maxIter -= 16 glutPostRedisplay() glutInit([]) glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE) glutInitWindowSize(width,height) glutCreateWindow(sys.argv[0]) glutDisplayFunc(draw) glutKeyboardFunc(keyboard) glutMainLoop()