mandelbrot


Graphs a Mandelbrot fractal. Coded August 2010.

from PIL import Image

def mandelbrot(x0, y0, maximum):
    Z = complex()
    C = complex(x0,y0)
    i = 0
    while (Z.real**2 + Z.imag**2 <= 4.0 and i < maximum):
        Z = Z**2 + C
        i+=1
    if i == maximum: #it is in the set
        return 255, 255, 255
    else: #even if we lose, we can still win...
        return (i/float(maximum))*255,0,0

def make_mandelbrot():
    scale_factor = 300.0
    max_iterations = 100
    size = (int(3.5*scale_factor), int(2*scale_factor)) #make it easy for us
    im = Image.new("RGB",size)

    data = im.load()
    for y in range(size[1]):
        for x in range(size[0]):
           v = mandelbrot((x-2.5*scale_factor)/scale_factor, (y-scale_factor)/scale_factor, max_iterations)
           data[x, y] = (int(v[0]),int(v[1]),int(v[2]))
    return im


make_mandelbrot().save("out.bmp")

Posted in: Code by nsundin
Copyright © 2011-2025 Programmatic Verse · RSS Feed
Built on Skeleton
Powerered by Wordpress