mandelbrot generator messed up

Thread Solved

Join Date: Jul 2005
Posts: 8
Reputation: zauber is an unknown quantity at this point 
Solved Threads: 1
zauber zauber is offline Offline
Newbie Poster

mandelbrot generator messed up

 
0
  #1
Jul 30th, 2005
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]
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,959
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 918
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: mandelbrot generator messed up

 
0
  #2
Jul 31st, 2005
I have worked with modelbrot sets before, just recently with this example, maybe you can apply the algorithm ...
  1. ## example of how Numeric, Tkinter, and PIL can be used
  2. ## together to create all sorts of images, in this case the Mandelbrot set
  3. ## used the Numerical python text example, but modified it to work with PIL
  4.  
  5. try:
  6. import Numeric
  7. except:
  8. print "This demo requires the Numeric extension, sorry."
  9. print "Get it from http://numeric.scipy.org/"
  10. import sys
  11. sys.exit()
  12. import FFT
  13. import Tkinter
  14. import Image
  15. import ImageTk
  16. import sys
  17.  
  18. w = 256
  19. h = 256
  20.  
  21. class Test:
  22. def draw(self,LowX, HighX, LowY, HighY, maxiter=30):
  23. xx=Numeric.arange(LowX,HighX,(HighX-LowX)/w*2)
  24. yy=Numeric.arange(HighY,LowY,(LowY-HighY)/h*2)*1j
  25. c=Numeric.ravel(xx+yy[:,Numeric.NewAxis])
  26. z=Numeric.zeros(c.shape,Numeric.Complex)
  27. output=Numeric.resize(Numeric.array(0,),c.shape)
  28.  
  29. for iter in range(maxiter):
  30. #print "iter",iter
  31. z=z*z+c
  32. finished=Numeric.greater(abs(z),2.0)
  33. c=Numeric.where(finished,0+0j,c)
  34. z=Numeric.where(finished,0+0j,z)
  35. output=Numeric.where(finished,iter,output)
  36.  
  37. # scale output a bit to make it brighter
  38. # output * output * 1000
  39. output = (output + (256*output) + (256**2)*output)*8
  40. self.mandel = output.tostring() #"raw", "RGBX", 0, -1)
  41. # testing ...
  42. print "len(self.mandel) =", len(self.mandel)
  43. # this is a long bin string
  44. print type(self.mandel)
  45.  
  46. # extra stuff
  47. # open the file for writing
  48. fileHandle = open( 'mandel.bin', 'wb' )
  49. fileHandle.write( self.mandel )
  50. # close the file
  51. fileHandle.close()
  52.  
  53. def createImage(self):
  54. self.im = Image.new("RGB", (w/2,h/2))
  55. self.draw(-2.1, 0.7, -1.2, 1.2)
  56. # testing ...
  57. print 'len(self.im.tostring("raw", "RGBX", 0, -1)) =', len(self.im.tostring("raw", "RGBX", 0, -1))
  58. # from PIL
  59. self.im.fromstring(self.mandel, "raw", "RGBX", 0, -1)
  60.  
  61. def createLabel(self):
  62. self.image = ImageTk.PhotoImage(self.im)
  63. self.label = Tkinter.Label(self.root, image=self.image)
  64. self.label.pack()
  65.  
  66. def __init__(self):
  67. self.root = Tkinter.Tk()
  68. self.i = 0
  69. self.createImage()
  70. self.createLabel()
  71. self.root.mainloop()
  72.  
  73. demo = Test
  74.  
  75. if __name__ == '__main__':
  76. demo()
Last edited by vegaseat; Oct 1st, 2009 at 4:47 pm. Reason: fixed code tags
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC