Advertisement
kabbotta

Buddhabrot Variation

Sep 14th, 2013
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.23 KB | None | 0 0
  1. import Image
  2. import numpy as np
  3. from random import uniform
  4. from math import sqrt, cos, sin, pi
  5.  
  6. WIDTH, HEIGHT = 800, 800
  7. directory = 'C:/PythonWorkspace/Images/GateAnimation/RotX'
  8.  
  9. a, b, c = 1.0, 1.0, 1.0     #Coefficients
  10. period = 600                #Period of transform
  11. epsilon = 2 * pi / period   #Small value to transform by
  12. iterations = 600            #Number of series terms to sum
  13. points = 2000000            #Number of randomly chosen test points
  14. escapeRadius = 2.0          #"Bailout radius" for determining if C diverges
  15. imagShift, realShift = 0.0, 0.3     #Shift view of complex plane
  16. ratioReal, ratioImag = WIDTH / 2.8, HEIGHT / 2.8   #Screen:C-plane ratio
  17.  
  18. for n in xrange( period ):
  19.     counters = np.zeros( ( WIDTH, HEIGHT ) )
  20.     img = Image.new( 'RGB', ( WIDTH, HEIGHT ), ( 200, 200, 200 ) )
  21.    
  22.     a = a                                           #Transform coefficients
  23.     b = b * cos( epsilon  ) - c * sin( epsilon )
  24.     c = b * sin( epsilon ) + c * cos( epsilon )
  25.    
  26.     for i in xrange( points ):
  27.         z = 0
  28.         path = []
  29.         realPart = uniform( -2.4, 1.0 )
  30.         imagPart = uniform( -1.8, 1.8 )
  31.         C = complex( realPart, imagPart )
  32.         for j in xrange( iterations ):
  33.             q = z.conjugate()               #Change of variable
  34.             z = a * q**3 + b * q**2 + c * q + C   #Generating Function
  35.             path.extend( [ z.imag + imagShift, z.real + realShift ] )
  36.             if abs( z ) > escapeRadius:
  37.                 while path:
  38.                     xValue = int( path.pop() * ratioReal ) + WIDTH / 2
  39.                     yValue = int( path.pop() * ratioImag ) + HEIGHT / 2
  40.                     if xValue > 0 and yValue > 0:
  41.                         if xValue < WIDTH and yValue < HEIGHT:
  42.                             counters[ xValue ][ yValue ] += 1
  43.                             counters[ xValue ][ -yValue ] += 1
  44.                 break
  45.  
  46.     maxCount = np.amax( counters )
  47.     for x in xrange( WIDTH ):           #Develop image from hit counts
  48.         for y in xrange( HEIGHT ):
  49.             color = int( 255 * ( sqrt( counters[x][y] / maxCount ) ) )
  50.             img.putpixel( ( y, x ), ( color / 2, color / 2, color ) )
  51.  
  52.     img.save( directory + '/frame' + str( n + 1 ) + '.bmp' )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement