Created
October 22, 2013 14:12
-
-
Save davepape/7101458 to your computer and use it in GitHub Desktop.
compute a Mandelbrot set and draw it using OpenGL points
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys, random | |
| from pyglet.gl import * | |
| width = 400 | |
| height = 400 | |
| singlebuffered = pyglet.gl.Config(double_buffer=False) | |
| window = pyglet.window.Window(width,height,config=singlebuffered) | |
| maxIter = 32 | |
| 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 | |
| def GreyscaleColormap(): | |
| map = [] | |
| for i in range(0,256): | |
| map.append((i,i,i)) | |
| return map | |
| def BlueGreenRedColormap(): | |
| map = [] | |
| for i in range(0,85): | |
| map.append((0, 0, int(i/85.0 * 255))) | |
| for i in range(0,85): | |
| map.append((0, int(i/85.0 * 255), 0)) | |
| for i in range(0,86): | |
| map.append((int(i/85.0 * 255), 0, 0)) | |
| return map | |
| colormap = BlueGreenRedColormap() | |
| center = (-0.75, 0.25) | |
| size = 1.0 | |
| @window.event | |
| def on_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: | |
| m = m * 255 / maxIter | |
| glColor3ub(colormap[m][0], colormap[m][1], colormap[m][2]) | |
| glVertex2i(i,j) | |
| glEnd() | |
| glFlush() | |
| @window.event | |
| def on_key_press(key, modifiers): | |
| global size, maxIter | |
| if key == pyglet.window.key.EQUAL or key == pyglet.window.key.PLUS: | |
| size = size / 2.0 | |
| elif key == pyglet.window.key.MINUS: | |
| size = size * 2.0 | |
| elif key == pyglet.window.key.A: | |
| maxIter += 16 | |
| elif key == pyglet.window.key.S: | |
| maxIter -= 16 | |
| pyglet.app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment