Guest User

Untitled

a guest
Oct 17th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. #!/bin/python
  2.  
  3. # Mandelbrot Set Viewer! By Bob Webb, 2012
  4.  
  5. import sys
  6. import functools
  7. import time
  8. import pygame
  9. import pickle
  10. import colorsys
  11. from pygame.locals import *
  12.  
  13. class memoized(object):
  14. '''Decorator. Caches a function's return value each time it is called.
  15. If called later with the same arguments, the cached value is returned
  16. (not reevaluated).
  17. '''
  18. def __init__(self, func):
  19. self.func = func
  20. self.cache = {}
  21. def __call__(self, *args):
  22. try:
  23. return self.cache[args]
  24. except KeyError:
  25. value = self.func(*args)
  26. self.cache[args] = value
  27. return value
  28. except TypeError:
  29. # uncachable -- for instance, passing a list as an argument.
  30. # Better to not cache than to blow up entirely.
  31. return self.func(*args)
  32. def __repr__(self):
  33. '''Return the function's docstring.'''
  34. return self.func.__doc__
  35. def __get__(self, obj, objtype):
  36. '''Support instance methods.'''
  37. return functools.partial(self.__call__, obj)
  38.  
  39. def drawData(surface,data,colourScheme):
  40. '''
  41. Draws a 2D grid of values ('data') to 'surface' using the given 'colourScheme'
  42. '''
  43. for x in range(len(data) - 1):
  44. for y in range(len(data[x]) - 1):
  45. surface.set_at((x,y),colourScheme(data[x][y]))
  46.  
  47. return surface
  48.  
  49.  
  50. @memoized
  51. def itersToColour(iters):
  52. #print iters/255
  53. rgb = [int(255*x) for x in colorsys.hls_to_rgb(1.0/(iters+1),0.5,0.8)]
  54. #print rgb
  55.  
  56. return Color(*rgb)
  57.  
  58. def main():
  59. inputFileName = sys.argv[1] # the pickle file to be read ^^
  60.  
  61. dataFile = open(inputFileName,'r')
  62.  
  63. data = pickle.load(dataFile)
  64.  
  65. pygame.init()
  66.  
  67. winWidth,winHeight = len(data),len(data[0])
  68.  
  69. surf = pygame.display.set_mode((winWidth,winHeight))
  70. pygame.display.set_caption("Mandelbrot set!")
  71.  
  72.  
  73. drawData(surf,data,itersToColour)
  74.  
  75. pygame.image.save(surf,"mandelbrot "+time.asctime(time.gmtime())+".png")
  76. print "Image displayed!"
  77.  
  78. pygame.display.flip()
  79.  
  80. while True:
  81. # loop shite
  82. # handle input
  83. for event in pygame.event.get():
  84. if event.type == QUIT:
  85. pygame.quit()
  86. sys.exit()
  87. elif event.type == KEYDOWN and event.key == K_ESCAPE:
  88. pygame.event.post(pygame.event.Event(QUIT))
  89.  
  90.  
  91. if __name__== '__main__' : main()
Add Comment
Please, Sign In to add comment