Main Page | Namespace List | Class List | File List | Namespace Members | Class Members | Related Pages | Examples

mandelbrot.py

This example generates a mandelbrot fractal.

How To Run

Run the command...

mandelbrot.py master
once to start the master process.

Everywhere else run ...

mandelbrot.py
#!/usr/bin/python # Copyright 2004 Andrew Wilkinson <aw@cs.york.ac.uk>. # # This file is part of PyLinda (http://www-users.cs.york.ac.uk/~aw/pylinda) # # PyLinda is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2.1 of the License, or # (at your option) any later version. # # PyLinda is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with PyLinda; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import linda linda.connect() import sys MaxIters = 256*256-1 if len(sys.argv) > 1 and sys.argv[1] == "master": out = linda.TupleSpace() pix = linda.TupleSpace() linda.universe._out(("out", out)) linda.universe._out(("pix", pix)) width, height = 80, 80 left, right, top, bottom = -2.0, 1.0, -1.0, 1.0 rls = (right - left) / width bts = (bottom - top) / height ylist = range(height) * width xlist = range(width) * height xlist.sort() map(lambda (x,y): out._out((x,y, left + x * rls, top + y * bts)), zip(xlist, ylist)) import Image im = Image.new("RGB", (width, height)) for i in range(height * width): x, y, r, g, b = pix._in((int, int, int, int, int)) print "%s\r" % i, im.putpixel((x, y), (r, g, b)) im.save("fractal.png") out = linda.universe._in(("out", linda.TupleSpace))[1] pix = linda.universe._in(("pix", linda.TupleSpace))[1] linda.kernel.message_noreturn("kill_server") else: out = linda.universe._rd(("out", linda.TupleSpace))[1] pix = linda.universe._rd(("pix", linda.TupleSpace))[1] col_mul = (2**24-1)/float(MaxIters) while True: t = out._inp((int, int, float, float)) if t is None: break (x, y, cr, ci) = t print "in", x, y #Z = 0 + 0j #C = cr + ci*1j #count = -1 #while abs(Z) <= 2.0 and count < MaxIters: # count += 1 #Z = Z * Z + C zr, zi = 0.0, 0.0 rsquared = zr * zr isquared = zi * zi count = -1 while rsquared + isquared <= 4.0 and count < MaxIters: count += 1 zi = zr * zi * 2 + ci zr = rsquared - isquared + cr rsquared = zr * zr isquared = zi * zi count = int(count * col_mul) pix._out((x, y, count%256, (count>>8)%256, (count>>16)%256))
00001 #!/usr/bin/python 00002 00003 # Copyright 2004 Andrew Wilkinson <aw@cs.york.ac.uk>. 00004 # 00005 # This file is part of PyLinda (http://www-users.cs.york.ac.uk/~aw/pylinda) 00006 # 00007 # PyLinda is free software; you can redistribute it and/or modify 00008 # it under the terms of the GNU Lesser General Public License as published by 00009 # the Free Software Foundation; either version 2.1 of the License, or 00010 # (at your option) any later version. 00011 # 00012 # PyLinda is distributed in the hope that it will be useful, 00013 # but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 # GNU Lesser General Public License for more details. 00016 # 00017 # You should have received a copy of the GNU Lesser General Public License 00018 # along with PyLinda; if not, write to the Free Software 00019 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 00021 import linda 00022 linda.connect() 00023 00024 import sys 00025 00026 MaxIters = 256*256-1 00027 00028 if len(sys.argv) > 1 and sys.argv[1] == "master": 00029 out = linda.TupleSpace() 00030 pix = linda.TupleSpace() 00031 00032 linda.universe._out(("out", out)) 00033 linda.universe._out(("pix", pix)) 00034 00035 width, height = 80, 80 00036 left, right, top, bottom = -2.0, 1.0, -1.0, 1.0 00037 00038 rls = (right - left) / width 00039 bts = (bottom - top) / height 00040 00041 ylist = range(height) * width 00042 xlist = range(width) * height 00043 00044 xlist.sort() 00045 00046 map(lambda (x,y): out._out((x,y, left + x * rls, top + y * bts)), zip(xlist, ylist)) 00047 00048 import Image 00049 im = Image.new("RGB", (width, height)) 00050 00051 for i in range(height * width): 00052 x, y, r, g, b = pix._in((int, int, int, int, int)) 00053 print "%s\r" % i, 00054 im.putpixel((x, y), (r, g, b)) 00055 00056 im.save("fractal.png") 00057 00058 out = linda.universe._in(("out", linda.TupleSpace))[1] 00059 pix = linda.universe._in(("pix", linda.TupleSpace))[1] 00060 00061 linda.kernel.message_noreturn("kill_server") 00062 00063 else: 00064 out = linda.universe._rd(("out", linda.TupleSpace))[1] 00065 pix = linda.universe._rd(("pix", linda.TupleSpace))[1] 00066 00067 col_mul = (2**24-1)/float(MaxIters) 00068 00069 while True: 00070 t = out._inp((int, int, float, float)) 00071 if t is None: 00072 break 00073 00074 (x, y, cr, ci) = t 00075 print "in", x, y 00076 00077 #Z = 0 + 0j 00078 #C = cr + ci*1j 00079 00080 #count = -1 00081 #while abs(Z) <= 2.0 and count < MaxIters: 00082 # count += 1 00083 00084 #Z = Z * Z + C 00085 00086 zr, zi = 0.0, 0.0 00087 rsquared = zr * zr 00088 isquared = zi * zi 00089 count = -1 00090 while rsquared + isquared <= 4.0 and count < MaxIters: 00091 count += 1 00092 00093 zi = zr * zi * 2 + ci 00094 zr = rsquared - isquared + cr 00095 00096 rsquared = zr * zr 00097 isquared = zi * zi 00098 00099 count = int(count * col_mul) 00100 00101 pix._out((x, y, count%256, (count>>8)%256, (count>>16)%256)) 00102 00103

PyLinda is © Copyright 2004 Andrew Wilkinson.

Generated on Thu Jan 13 14:12:30 2005 for PyLinda by doxygen 1.3.7