| | |
mandelbrot generator messed up
Thread Solved |
•
•
Join Date: Jul 2005
Posts: 8
Reputation:
Solved Threads: 1
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)
[PHP]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)[/PHP]
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)
[PHP]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)[/PHP]
I have worked with modelbrot sets before, just recently with this example, maybe you can apply the algorithm ...
python Syntax (Toggle Plain Text)
## 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()
Last edited by vegaseat; Oct 1st, 2009 at 4:47 pm. Reason: fixed code tags
May 'the Google' be with you!
![]() |
Similar Threads
- Random number generator's (C++)
- Problems using a php generator (PHP)
- messed up windows... again! (Windows NT / 2000 / XP)
- Messed up with hijack this (Viruses, Spyware and other Nasties)
- XHTML Complient parser? (HTML and CSS)
- pixles and colors messed up (Windows 95 / 98 / Me)
- Msn buddy icons messed up (Windows Software)
Other Threads in the Python Forum
- Previous Thread: pygame full screen help
- Next Thread: Python can handle Very Large Numbers
| Thread Tools | Search this Thread |
alarm ansi app assignment avogadro backend beginner binary bluetooth character cmd customdialog cx-freeze data decimals dictionary directory dynamic error exe file float format function generator getvalue gnu graphics halp heads homework http ideas import input ip itunes java leftmouse line linux list lists loop maintain maze millimeter module mouse number numbers output parsing path pointer port prime programming progressbar push py2exe pygame python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation sqlite ssh statistics string strings sudokusolver sum text thread threading time tlapse tuple tutorial ubuntu unicode url urllib urllib2 variable variables ventrilo vigenere web webservice wikipedia write wxpython xlib






