Welcome, guest | Sign In | My Account | Store | Cart
# Multi-threaded Mandelbrot Fractal (Do not run using IDLE!)
# FB - 201104306
import threading
from PIL import Image
w = 512 # image width
h = 512 # image height
image = Image.new("RGB", (w, h))
wh = w * h
maxIt = 256 # max number of iterations allowed
# drawing region (xa < xb & ya < yb)
xa = -2.0
xb = 1.0
ya = -1.5
yb = 1.5
xd = xb - xa
yd = yb - ya
numThr = 5 # number of threads to run
# lock = threading.Lock()

class ManFrThread(threading.Thread): 
    def __init__ (self, k):
          self.k = k
          threading.Thread.__init__(self)
    def run(self):
        # each thread only calculates its own share of pixels
        for i in range(k, wh, numThr):
            kx = i % w
            ky = int(i / w)
            a = xa + xd * kx / (w - 1.0)
            b = ya + yd * ky / (h - 1.0)
            x = a
            y = b
            for kc in range(maxIt):
                x0 = x * x - y * y + a
                y = 2.0 * x * y + b
                x = x0                
                if x * x + y * y > 4:
                    # various color palettes can be created here
                    red = (kc % 8) * 32
                    green = (16 - kc % 16) * 16
                    blue = (kc % 16) * 16
                    # lock.acquire()
                    global image
                    image.putpixel((kx, ky), (red, green, blue))
                    # lock.release()
                    break

if __name__ == "__main__":
    tArr = []
    for k in range(numThr): # create all threads
        tArr.append(ManFrThread(k))
    for k in range(numThr): # start all threads
        tArr[k].start()
    for k in range(numThr): # wait until all threads finished
        tArr[k].join()
    image.save("MandelbrotFractal.png", "PNG")

Diff to Previous Revision

--- revision 2 2011-04-30 16:54:07
+++ revision 3 2011-05-01 17:33:20
@@ -2,12 +2,12 @@
 # FB - 201104306
 import threading
 from PIL import Image
-w = 500 # image width
-h = 500 # image height
+w = 512 # image width
+h = 512 # image height
 image = Image.new("RGB", (w, h))
 wh = w * h
 maxIt = 256 # max number of iterations allowed
-# drawing region
+# drawing region (xa < xb & ya < yb)
 xa = -2.0
 xb = 1.0
 ya = -1.5
@@ -25,18 +25,18 @@
         # each thread only calculates its own share of pixels
         for i in range(k, wh, numThr):
             kx = i % w
-            ky = (i - kx) / w #= int(i / w)
-            a = xa + xd * kx / w
-            b = ya + yd * ky / h
+            ky = int(i / w)
+            a = xa + xd * kx / (w - 1.0)
+            b = ya + yd * ky / (h - 1.0)
             x = a
             y = b
             for kc in range(maxIt):
                 x0 = x * x - y * y + a
-                y = 2 * x * y + b
+                y = 2.0 * x * y + b
                 x = x0                
                 if x * x + y * y > 4:
                     # various color palettes can be created here
-                    red = 255 - (kc % 16) * 16
+                    red = (kc % 8) * 32
                     green = (16 - kc % 16) * 16
                     blue = (kc % 16) * 16
                     # lock.acquire()

History