Mandelbrot Microscope

Nick J Chackowsky mediocre_person at hotmail.com
Thu Jul 8 19:34:01 EDT 2004


Okay, here's my naive solution to coding a Mandelbrot microscope in 
Python... but it's so slow! Would anyone care to comment on how to speed 
this up?

Nick.

#
#This uses the graphics.py module included with John Zelle's
#textbook. File available at http://mcsp.wartburg.edu/zelle/python/
#

from graphics import *

DEPTH = 200

def f(z):
     C = z
     count = 0
     while count < DEPTH and abs(z) < 3.0:
         z = z*z + C
         count += 1
     if count == DEPTH:
         count -= 1
     return count

def rangecolors(n):
     """ Return a list of nicely blended colors """
     clist = []
     r, g, b = 13, 101, 137
     for c in range(n):
         clist.append(color_rgb(r,g,b))
         r += 5
         g += 7
         b += 11
         if r > 255: r = 0
         if g > 255: g = 0
         if b > 255: b = 0
     #end for
     return clist

def main():
     llx, lly = -3.0, 3.0  #lower left
     urx, ury = 3.0, -3.0  #upper right
     WINDOWSZ = 200
     clist = rangecolors(DEPTH)
     keepgoing = True
     while keepgoing:
         w = GraphWin("Mandelbrot", WINDOWSZ, WINDOWSZ)
         w.setCoords(llx, lly, urx, ury)
         delta = abs(llx-urx)/float(WINDOWSZ)
         re = llx
         while re < urx:
             im = ury
             while im < lly:
                 w.plot(re, im, clist[f(complex(re, im))])
                 im += delta
             #end while
             re += delta
         #end while
         p1 = w.getMouse()
         p2 = w.getMouse()

         #for now, assume that the user input a square area correctly
         llx = p1.getX()
         lly = p1.getY()
         urx = p2.getX()
         ury = p2.getY()

         w.close()
     #end while

main()



More information about the Python-list mailing list