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()

    #for i in range(int(sys.argv[2])):
    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
    #left, right, top, bottom = 0.52151515, 0.55855219, 0.63294052, 0.67783389

    rls = (right - left) / width
    bts = (bottom - top) / height
    for y in range(height):
        for x in range(width):
            out._out(((x,y), (left + x * rls, top + y * bts)))
        print "out", y

    import Image
    im = Image.new("RGB", (width, height))

    #for y in range(height):
    #    for x in range(width):
    for i in range(height * width):
            (x, y), (r, g, b) = pix._in(((int, int), (int, int, int)))
            print "in", x, y
            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]

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)))



Generated on Mon May 3 11:29:44 2004 for PyLinda by doxygen 1.3.5