farry

mandelbrot-ppm

Oct 16th, 2021 (edited)
710
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. from math import log2, log1p
  4. import time
  5.  
  6. w, h = 900, 720
  7. t1 = time.time()
  8. xc, yc = -0.6, 0.005
  9. mag = 0.65
  10. imax, zmax = 190, 50
  11.  
  12. blues  = [(v**4, v**2.5, v) for v in (0.2 + x/200 for x in range(160))]
  13. sepias = [(v, v**1.5, v**3) for v in (x/96 for x in range(95, -1, -1))]
  14. rgbcolors = [[int(256 * v) for v in x] for x in blues + sepias]
  15.  
  16. databytes = bytearray([])
  17. for y in range(h):
  18.     for x in range(w):
  19.         c = complex(xc + (2*x - w) / (w*mag), -yc - (2*y - h) / (w*mag))
  20.         z = complex(0.0, 0.0)
  21.         backgnd = 0
  22.         for i in range(imax):
  23.             z = z * z + c
  24.             if abs(z) >= zmax:
  25.                 backgnd = log1p(i + 0.1 - log2(log2(abs(z) + 1))) / 5.25
  26.                 break
  27.         databytes += bytearray(rgbcolors[int(backgnd * 255)])
  28.  
  29. with open('mand.ppm','wb') as fout:
  30.     fout.write("P6\n{} {}\n255\n".format(w,h).encode("ascii"))
  31.     fout.write(databytes)
  32.  
  33. print("Elapsed time: {:7.2f} s".format(time.time() - t1))
  34.  
  35.  
Add Comment
Please, Sign In to add comment