User Tools

Site Tools


fractal

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
fractal [2011/04/12 23:28] cedricfractal [2011/04/12 23:47] cedric
Line 8: Line 8:
  
 ====== Iterated function system ====== ====== Iterated function system ======
 +
 +In mathematics, [[http://en.wikipedia.org/wiki/Iterated_function_system | iterated function systems]] or IFSs are a method of constructing fractals; the resulting constructions are always self-similar.
  
 {{:barnsley-1.png|}} {{:barnsley-1.png|}}
Line 141: Line 143:
  
 ====== Mandelbrot ====== ====== Mandelbrot ======
 +
 +{{:mandelbrot-python.png|}}
 +
 <code python> <code python>
 try: try:
- import Numeric as nm+    import Numeric as nm
 except: except:
- print "program requires the Numeric module" +    print "program requires the Numeric module" 
- print "from --> http://numeric.scipy.org/" +    print "from --> http://numeric.scipy.org/" 
- raise SystemExit+    raise SystemExit
 import Tkinter as tk import Tkinter as tk
 import Image # PIL import Image # PIL
 import ImageTk # PIL import ImageTk # PIL
 # set width and height of window # set width and height of window
-w ,h = 640, 600+#w ,h = 640, 600
 #w ,h = 1280, 1200 #w ,h = 1280, 1200
 +w ,h = 2200, 2000
 #w ,h = 2560, 2400 #w ,h = 2560, 2400
  
 class Mandelbrot(object): class Mandelbrot(object):
- def __init__(self): +    def __init__(self): 
- """ +        """ 
- Create window +        Create window 
- """ +        """ 
- self.root = tk.Tk() +        self.root = tk.Tk() 
- self.root.title("Mandelbrot Set"+        self.root.title("Mandelbrot Set"
- self.create_image() +        self.create_image() 
- self.create_label() +        self.create_label() 
- # start event loop +        # start event loop 
- self.root.mainloop()+        self.root.mainloop()
  
- def draw(self, x1, x2, y1, y2, maxiter=30): +    def draw(self, x1, x2, y1, y2, maxiter=30): 
- """ +        """ 
- Draw the Mandelbrot set. +        Draw the Mandelbrot set. 
- """ +        """ 
- xx = nm.arange(x1, x2, (x2-x1)/w*2) +        xx = nm.arange(x1, x2, (x2-x1)/w*2) 
- yy = nm.arange(y2, y1, (y1-y2)/h*2) * 1j +        yy = nm.arange(y2, y1, (y1-y2)/h*2) * 1j 
- q = nm.ravel(xx+yy[:, nm.NewAxis]) +        q = nm.ravel(xx+yy[:, nm.NewAxis]) 
- z = nm.zeros(q.shape, nm.Complex) +        z = nm.zeros(q.shape, nm.Complex) 
- output = nm.resize(nm.array(0,), q.shape) +        output = nm.resize(nm.array(0,), q.shape) 
- for iter in range(maxiter): +        for iter in range(maxiter): 
- z = z*z + q +            z = z*z + q 
- done = nm.greater(abs(z), 2.0) +            done = nm.greater(abs(z), 2.0) 
- q = nm.where(done,0+0j, q) +            q = nm.where(done,0+0j, q) 
- z = nm.where(done,0+0j, z) +            z = nm.where(done,0+0j, z) 
- output = nm.where(done, iter, output) +            output = nm.where(done, iter, output) 
- output = (output + (256*output) + (256**2)*output) * 8 +        output = (output + (256*output) + (256**2)*output) * 8 
- # convert output to a string +        # convert output to a string 
- self.mandel = output.tostring()+        self.mandel = output.tostring()
  
- def create_image(self): +    def create_image(self): 
- """" +        """" 
- Create the image from the draw() string +        Create the image from the draw() string 
- """ +        """ 
- self.im = Image.new("RGB", (w/2, h/2)) +        self.im = Image.new("RGB", (w/2, h/2)) 
- # you can experiment with these x and y ranges +        # you can experiment with these x and y ranges 
- self.draw(-2.13, 0.77, -1.3, 1.3) +        self.draw(-2.13, 0.77, -1.3, 1.3) 
- self.im.fromstring(self.mandel, "raw", "RGBX", 0, -1)+        self.im.fromstring(self.mandel, "raw", "RGBX", 0, -1)
  
- def create_label(self): +    def create_label(self): 
- """ +        """ 
- Put the image on a label widget +        Put the image on a label widget 
- """ +        """ 
- self.image = ImageTk.PhotoImage(self.im) +        self.image = ImageTk.PhotoImage(self.im) 
- self.label = tk.Label(self.root, image=self.image) +        self.label = tk.Label(self.root, image=self.image) 
- self.label.pack()+        self.label.pack()
  
  
 if __name__ == '__main__': if __name__ == '__main__':
- test = Mandelbrot() +    test = Mandelbrot() 
-</code+</code>