Advertisement
Guest User

Non-Obfuscated Python Mandelbrot Set Generator.

a guest
Sep 26th, 2011
826
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.02 KB | None | 0 0
  1. import time
  2. import struct
  3.  
  4. DEPTH = 255
  5.  
  6. mandel_lambda = lambda V, B, c: c and mandel_lambda(V*V + B, B, c - 1) if (abs(V) < 6) else (2 + c- 4*abs(V)**-0.4) / DEPTH
  7.  
  8. def mandel_recursive(V, B, c):
  9.     if c == 0:
  10.         return 0
  11.  
  12.     if abs(V) < 6:
  13.         return mandel_recursive(V**2 + B, B, c-1)
  14.     else:
  15.         return (2 + c- 4*abs(V)**-0.4) / DEPTH
  16.  
  17. def mandel(z0, c, itr):
  18.     z = z0
  19.     for i in range(DEPTH - 1, -1, -1):
  20.         z = (z**2) + c
  21.         if abs(z) > 6:
  22.             return (2 + i - 4 *abs(z)**-0.4) / DEPTH
  23.  
  24.     return 0
  25.  
  26. def get_colour(total):
  27.     r = (total * 80) + ((total**9) * DEPTH) - (950 * (total**99))
  28.     g = (total * 70) - (880 * (total**18)) + (701 * (total**9))
  29.     b = total * (DEPTH**(1 - (total**45) * 2))
  30.     return r, g, b
  31.    
  32. v, x = 1500, 1000;
  33.  
  34. # For information regarding the BMP format, see:
  35. # http://en.wikipedia.org/wiki/BMP_file_format
  36.  
  37. if __name__ == '__main__':
  38.     tests = [(mandel, 'mandelbrot_basic'),
  39.              (mandel_recursive, 'mandelbrot_recursive'),
  40.              (mandel_lambda, 'mandelbrot_lambda'),
  41.             ]
  42.            
  43.     for func, name in tests:
  44.         fname = '%s.bmp' % name
  45.         print "Writing %dx%d image to: %s" % (v, x, fname)
  46.         start = time.time()
  47.         with open(fname, 'wb') as fp:
  48.             fmt = '<QIIHHHH'  # little-endian: unsigned long long, unsigned int (2), unsigned short (4)
  49.             fp.write('BM' + struct.pack(fmt,            # 'BM' == Windows bitmap identifier
  50.                                        v * x * 3 + 26,  # unsigned long long record size (+4 reserved bytes)
  51.                                        26, 12,          # unsigned int(starting offset)
  52.                                        v, x,            # unsigned short(width, height)
  53.                                        1, 24))          # unsigned short(planes, bits per pixel)
  54.             for X in range(v * x):    
  55.                 total = 0
  56.                 for A in range(9):
  57.                     row = X % v
  58.                     off = (A % 3) / 3.0
  59.                     real = off + row
  60.                     imag = (X/v + off - x/2) / 1j
  61.                     c = ((real + imag) * (2.5 / x)) - 2.7
  62.                     esc = func(0, c, DEPTH)
  63.                     total += esc**2
  64.                 colour = get_colour(total / 9)
  65.                 fp.write(struct.pack('BBB', *colour))
  66.         print "Done. %0.3f seconds." % (time.time() - start)
  67.  
  68. # Results on a Intel Core2 6400 @ 2.13 GHz:
  69. #
  70. # pypy-1.6.0
  71. # Writing 1500x1000 image to: mandelbrot_basic.bmp
  72. # Done. 160.632 seconds.
  73. # Writing 1500x1000 image to: mandelbrot_recursive.bmp
  74. # Done. 188.082 seconds.
  75. # Writing 1500x1000 image to: mandelbrot_lambda.bmp
  76. # Done. 132.598 seconds.
  77. #
  78. # python-2.6.5
  79. # Writing 1500x1000 image to: mandelbrot_basic.bmp
  80. # Done. 513.069 seconds.
  81. # Writing 1500x1000 image to: mandelbrot_recursive.bmp
  82. # Done. 661.153 seconds.
  83. # Writing 1500x1000 image to: mandelbrot_lambda.bmp
  84. # Done. 572.474 seconds.
  85. #
  86. # Don't take these numbers too seriously.
  87.  
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement