Welcome, guest | Sign In | My Account | Store | Cart
# Recursively draw the Mandelbrot set
# Dependencies: Python 2.7.5, PyGame 1.9.1

import pygame
from pygame.locals import QUIT
from sys import exit

# size must be a power of 2 or you will get rounding errors in the image
# E.g. 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, ...
size = 512

pygame.init()
surface = pygame.display.set_mode((size, size), 0, 32)

# Mandelbrot drawing area
xa = -2.0
xb = 1.0
ya = -1.5
yb = 1.5

# maximum iterations
maxIt = 256

def pump():
    # pump the event queue so the window is responsive, exit if signaled
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            exit()

def point(x, y):
    # get the escape value of a specific coordinate in the Mandelbrot set
    zy = y * (yb - ya) / size  + ya
    zx = x * (xb - xa) / size  + xa
    z = zx + zy * 1j
    c = z
    for i in range(maxIt):
        if abs(z) > 2.0: break
        z = z * z + c
    return i

def col(i):
    # return a color variable computed from a escape value
    return (i % 4 * 64, i % 8 * 32, i % 16 * 16)

def mandel(x, y, i_size):

    p1 = point(x, y)

    # test if the square needs to be further sub-divided
    # test every pixel around the square, if they are all the same color fill
    test = False
    for i in range(i_size):
        t1 = point(x, y + i)
        t2 = point(x + i, y)
        t3 = point(x + i_size, y + i)
        t4 = point(x + i, y + i_size)
        if (p1 != t1 or p1 != t2 or p1 !=t3 or p1 != t4):
            test = True
            break

    if test:
        # plot corner points and sub-divide area
        p2 = point(x + i_size, y)
        p3 = point(x + i_size, y + i_size)
        p4 = point(x, y + i_size)

        surface.lock()
        surface.set_at((x, y), col(p1))
        surface.set_at((x + i_size, y), col(p2))
        surface.set_at((x + i_size, y + i_size), col(p3))
        surface.set_at((x, y + i_size), col(p4))
        surface.unlock()

        # base case, dividing by 2: when 1 / 2 = 0.5 is floored to 0
        half = i_size / 2
        if half != 0:
            mandel(x, y, half)
            mandel(x + half, y, half)
            mandel(x + half, y + half, half)
            mandel(x, y + half, half)
        else:
            return
    else:
        # all square border points are same color, fill area
        surface.fill(col(p1), (x, y, i_size, i_size))

    pygame.display.update()
    pump()

# calculate the image
mandel(0, 0, size)

# Wait for user to click close widget on window
while True:
    pump()

Diff to Previous Revision

--- revision 4 2013-08-10 06:14:53
+++ revision 5 2013-08-10 06:16:55
@@ -61,8 +61,6 @@
 
     if test:
         # plot corner points and sub-divide area
-        half = i_size / 2
-
         p2 = point(x + i_size, y)
         p3 = point(x + i_size, y + i_size)
         p4 = point(x, y + i_size)
@@ -75,6 +73,7 @@
         surface.unlock()
 
         # base case, dividing by 2: when 1 / 2 = 0.5 is floored to 0
+        half = i_size / 2
         if half != 0:
             mandel(x, y, half)
             mandel(x + half, y, half)

History