Hello,
I started coding a little app for exploring the mandelbrot fractal. Hopefully someone here is familiar with it, and perhaps tried coding it in python.

My problem is, as you will see if you try running my code, is that the generated image is NOT the mandelbrot (although it is fractal and similar in a sense). And I just cant figure out why this is. So my hopes are that this is due to some oddity of python I am hitherto unaware of - I can't see any misstakes in the algorithm itself (although it might be there anyway of course :) )

heres the code (uses pygame and numeric, and is in no way optimized - I wanted it that way to make sure no mistakes were made)

import pygame
from Numeric import *
from pygame import *
pygame.init()
WHITE=(255,255,255,255)
BLACK=(0,0,0,255)
WIDTH=200
HEIGHT=200
ITERATIONS = 20
rmin=-1.0
rmax=2.0
imin=-2.0
imax=2.0
canvas=pygame.display.set_mode((WIDTH,HEIGHT))
canvas.fill(BLACK)
pygame.display.update()
px=-1
for a in arange(rmin,rmax,(rmax-rmin)/WIDTH):
    px+=1
    py=-1
    for b in arange(imax,imin,(imin-imax)/HEIGHT):    
        py+=1
        x = 0
        y = 0
        i=0
        while i<ITERATIONS and (x*x+y*y)<4:
            x=x*x-y*y+a
            y=2*x*y +b
            i=i+1
        if i==ITERATIONS: canvas.set_at((px,py),WHITE)
        else: canvas.set_at((px,py),BLACK)
        pygame.display.update()
pygame.time.wait(2000)

I have worked with modelbrot sets before, just recently with this example, maybe you can apply the algorithm ...

## example of how Numeric, Tkinter, and PIL can be used 
## together to create all sorts of images, in this case the Mandelbrot set
## used the Numerical python text example, but modified it to work with PIL

try:
    import Numeric
except:
    print "This demo requires the Numeric extension, sorry."
    print "Get it from http://numeric.scipy.org/"
    import sys
    sys.exit()
import FFT
import Tkinter
import Image
import ImageTk
import sys

w = 256
h = 256

class Test:
    def draw(self,LowX, HighX, LowY, HighY, maxiter=30):
        xx=Numeric.arange(LowX,HighX,(HighX-LowX)/w*2)
        yy=Numeric.arange(HighY,LowY,(LowY-HighY)/h*2)*1j
        c=Numeric.ravel(xx+yy[:,Numeric.NewAxis])
        z=Numeric.zeros(c.shape,Numeric.Complex)
        output=Numeric.resize(Numeric.array(0,),c.shape)

        for iter in range(maxiter):
            #print "iter",iter
            z=z*z+c
            finished=Numeric.greater(abs(z),2.0)
            c=Numeric.where(finished,0+0j,c)
            z=Numeric.where(finished,0+0j,z)
            output=Numeric.where(finished,iter,output)

        # scale output a bit to make it brighter
        # output * output * 1000
        output = (output + (256*output) + (256**2)*output)*8
        self.mandel = output.tostring() #"raw", "RGBX", 0, -1)
        # testing ...
        print "len(self.mandel) =", len(self.mandel)
        # this is a long bin string
        print type(self.mandel)

        # extra stuff
        # open the file for writing
        fileHandle = open( 'mandel.bin', 'wb' )
        fileHandle.write( self.mandel )
        # close the file
        fileHandle.close()

    def createImage(self):
        self.im = Image.new("RGB", (w/2,h/2))
        self.draw(-2.1, 0.7, -1.2, 1.2)
        # testing ...
        print 'len(self.im.tostring("raw", "RGBX", 0, -1)) =', len(self.im.tostring("raw", "RGBX", 0, -1))
        # from PIL
        self.im.fromstring(self.mandel, "raw", "RGBX", 0, -1)

    def createLabel(self):
        self.image = ImageTk.PhotoImage(self.im)
        self.label = Tkinter.Label(self.root, image=self.image)
        self.label.pack()
        
    def __init__(self):
        self.root = Tkinter.Tk()
        self.i = 0
        self.createImage()
        self.createLabel()
        self.root.mainloop()

demo = Test

if __name__ == '__main__':
    demo()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.