Advertisement
clairec

BADASS FUCKING FRACTAL

Apr 30th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | None | 0 0
  1. import curses
  2.  
  3.  
  4. def mandelbrotTest(c):
  5.     ''''Tests to see if a point is is the mandelbrot set, and assigns it
  6.    a character based on how quickly it escaped, if at all.'''
  7.  
  8.     iToChar = {5:'@', 4:'O', 3:'*', 2:'.', 1:'`', 0:' '}
  9.    
  10.     f = lambda z: z ** 2 + c
  11.     Z = f(0)
  12.     i = 0
  13.    
  14.     while abs(Z) <= 2:
  15.         if i > 500:
  16.             break
  17.         Z = f(Z)
  18.         i += 1
  19.    
  20.     return iToChar[int(i / 100)]
  21.  
  22.  
  23.  
  24. def main(stdscr):
  25.     X = 51        # Resolution. Should be an odd number.
  26.     Y = 51
  27.    
  28.     xMid = X // 2  # This represents the axes on a complex plane.
  29.     yMid = Y // 2
  30.    
  31.     dx = 2 / xMid  # How much each cell represents in the context
  32.     dy = 2 / yMid  # of the complex plane.
  33.    
  34.     conCol = lambda col: -2 + dx * col # Converts a colum/row to the equivalent  
  35.     conRow = lambda row: 2 - dy * row  # Complex number
  36.    
  37.     stdscr.clear()
  38.     stdscr.refresh()
  39.     canvas = curses.newpad(Y,X)
  40.    
  41.     for row in range(Y):
  42.         for col in range(X):
  43.             x, y = conCol(col), conRow(row)
  44.             c = complex(x, y)
  45.             canvas.addstr(row, col, mandelbrotTest(c))
  46.             canvas.refresh(0,0, 0,0, Y,X)
  47.  
  48.  
  49.  
  50. if __name__ == "__main__":
  51.     curses.wrapper(main)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement