fractal
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
fractal [2011/04/12 23:28] – cedric | fractal [2011/04/12 23:46] – [Mandelbrot] cedric | ||
---|---|---|---|
Line 141: | Line 141: | ||
====== Mandelbrot ====== | ====== Mandelbrot ====== | ||
+ | |||
+ | {{: | ||
+ | |||
<code python> | <code python> | ||
try: | try: | ||
- | import Numeric as nm | + | |
except: | except: | ||
- | print " | + | |
- | print "from --> http:// | + | print "from --> http:// |
- | 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): | + | |
- | """ | + | """ |
- | Create window | + | Create window |
- | """ | + | """ |
- | self.root = tk.Tk() | + | self.root = tk.Tk() |
- | self.root.title(" | + | self.root.title(" |
- | 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): | + | |
- | """ | + | """ |
- | Draw the Mandelbrot set. | + | Draw the Mandelbrot set. |
- | """ | + | """ |
- | xx = nm.arange(x1, | + | xx = nm.arange(x1, |
- | yy = nm.arange(y2, | + | yy = nm.arange(y2, |
- | q = nm.ravel(xx+yy[:, | + | q = nm.ravel(xx+yy[:, |
- | z = nm.zeros(q.shape, | + | z = nm.zeros(q.shape, |
- | output = nm.resize(nm.array(0, | + | output = nm.resize(nm.array(0, |
- | for iter in range(maxiter): | + | for iter in range(maxiter): |
- | z = z*z + q | + | z = z*z + q |
- | done = nm.greater(abs(z), | + | done = nm.greater(abs(z), |
- | q = nm.where(done, | + | q = nm.where(done, |
- | z = nm.where(done, | + | z = nm.where(done, |
- | output = nm.where(done, | + | output = nm.where(done, |
- | 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): | + | |
- | """" | + | """" |
- | Create the image from the draw() string | + | Create the image from the draw() string |
- | """ | + | """ |
- | self.im = Image.new(" | + | self.im = Image.new(" |
- | # you can experiment with these x and y ranges | + | # you can experiment with these x and y ranges |
- | self.draw(-2.13, | + | self.draw(-2.13, |
- | self.im.fromstring(self.mandel, | + | self.im.fromstring(self.mandel, |
- | 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, | + | self.label = tk.Label(self.root, |
- | self.label.pack() | + | self.label.pack() |
if __name__ == ' | if __name__ == ' | ||
- | test = Mandelbrot() | + | |
- | </code | + | </code> |