Advertisement
granada

Mandelbrot with real escape time algorithm

May 25th, 2013
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | None | 0 0
  1. import numpy
  2. import math
  3.  
  4. screenx=1600/2
  5. screeny=1200/2
  6.  
  7. bailout=2048/16
  8. bailout=64
  9.  
  10. inc=10.0
  11.  
  12. real_min, real_max=-2.5,1.5
  13. imag_min, imag_max=-1.5,1.5
  14.  
  15. real_range=numpy.arange(real_min,real_max,(real_max-real_min)/screenx)
  16. imag_range=numpy.arange(imag_min,imag_max,(imag_max-imag_min)/screeny)
  17.  
  18. fout=open('mandelbrot.ppm','w')
  19. fout.write('P3\n'+str(screenx)+' '+str(screeny)+'\n255\n')
  20.  
  21. for im in imag_range:
  22.     for re in real_range:
  23.         c=complex(re,im)
  24.         z=complex(0.0,0.0)
  25.         n=0
  26.         while abs(z)<4 and n<bailout:
  27.             z=z*z+c
  28.             n+=1
  29.         if n==bailout:
  30.             fout.write('0 0 0 ')
  31.         else:
  32.             h=((n+1-math.log(math.log(abs(z)))/math.log(2)))
  33.             while h>inc:
  34.                 h-=inc
  35.             h/=inc
  36.             h*=6.0
  37.             hi=int(h)
  38.             hf=h-hi
  39.             if hi==0:
  40.                 r,g,b=1.0,hf,0.0
  41.             elif hi==1:
  42.                 r,g,b=1.0-hf,1.0,0.0
  43.             elif hi==2:
  44.                 r,g,b=0.0,1.0,hf
  45.             elif hi==3:
  46.                 r,g,b=0.0,1.0-hf,1.0
  47.             elif hi==4:
  48.                 r,g,b=hf,0.0,1.0
  49.             elif hi==5:
  50.                 r,g,b=1.0,0.0,1.0-hf
  51.            
  52.             r*=255
  53.             g*=255
  54.             b*=255
  55.            
  56.             fout.write(str(int(r))+' '+str(int(g))+' '+str(int(b))+' ');
  57.     fout.write('\n')
  58.     print('Imaginary value starts in '+str(imag_min)+' and goes to '+str(imag_max)+' and is at '+str(im))
  59.  
  60. fout.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement