Advertisement
calcpage

mandel_serial03.py

May 14th, 2020
1,475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.19 KB | None | 0 0
  1. #!/usr/bin/python3
  2. #mandel_serial03.py
  3. #works with commandline arguments on RPI
  4. #output *.png file
  5. #if chmod 755:
  6. #./mandel_serial03.py hort vert colors
  7. import sys
  8. import timeit
  9. print('len(sys.argv) = ' + str(len(sys.argv)))
  10. print('sys.argv[0] = ' + sys.argv[0])
  11. w, h, maxit = sys.argv[1], sys.argv[2], sys.argv[3]
  12. print('w = ' + w)
  13. print('h = ' + h)
  14. print('maxit = ' + maxit)
  15.  
  16. def mandelbrot(x, y, maxit):
  17.     c = x + y*1j
  18.     z = 0 + 0j
  19.     it = 0
  20.     while abs(z) < 2 and it < maxit:
  21.         z = z**2 + c
  22.         it += 1
  23.     return it
  24.    
  25. x1, x2 = -2.0, 1.0
  26. y1, y2 = -1.0, 1.0
  27. #w, h = 150, 100
  28. #maxit = 127
  29. w = int(w)
  30. h = int(h)
  31. maxit = int(maxit)
  32.  
  33. start_time=timeit.default_timer()
  34. import numpy
  35. C = numpy.zeros([h, w], dtype='i')
  36. dx = (x2 - x1) / w
  37. dy = (y2 - y1) / h
  38. for i in range(h):
  39.     y = y1 + i * dy
  40.     for j in range(w):
  41.         x = x1 + j * dx
  42.         C[i, j] = mandelbrot(x, y, maxit)
  43. print("cpu time = " + str(timeit.default_timer()-start_time) + ' micro seconds')
  44.        
  45.  
  46. import matplotlib
  47. matplotlib.use('Agg')
  48. import matplotlib.pyplot as plt
  49. #pyplot.imshow(C, aspect='equal')
  50. #pyplot.spectral()
  51. #pyplot.show()
  52. plt.imshow(C, aspect='equal', cmap='nipy_spectral_r')
  53. plt.savefig("mandelbrot.png", dpi=200)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement