Advertisement
clairec

Mandelbrot Set (Python 2)

Apr 30th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.19 KB | None | 0 0
  1. from __future__ import division
  2. import curses
  3. range = xrange
  4.  
  5. ITOCHAR = {5:'@', 4:'O', 3:'*', 2:'.', 1:'`', 0:' '}
  6.  
  7. def mandelbrotTest(c):
  8.     ''''Tests to see if a point is is the mandelbrot set, and assigns it
  9.    a character based on how quickly it escaped, if at all.'''
  10.  
  11.     Z = c
  12.     i = 0
  13.    
  14.     while abs(Z) <= 2:
  15.         if i > 500:
  16.             break
  17.         Z = Z ** 2 + c
  18.         i += 1
  19.    
  20.     return ITOCHAR[i // 100]
  21.  
  22.  
  23.  
  24. def main(stdscr):
  25.     C = curses.COLS
  26.     R = curses.LINES
  27.  
  28.     X = C - 1 if C % 2 == 0 else C    # Resolution. Should be an odd number.
  29.     Y = R - 1 if R % 2 == 0 else R
  30.    
  31.     xMid = X // 2  # This represents the axes on a complex plane.
  32.     yMid = Y // 2
  33.    
  34.     dx = 2 / xMid  # How much each cell represents in the context
  35.     dy = 2 / yMid  # of the complex plane.
  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 = (-2 + dx * col), (2 - dy * row)
  44.             c = complex(x, y)
  45.             stdscr.addstr(row, col, mandelbrotTest(c))
  46.         stdscr.refresh()
  47.  
  48.  
  49.  
  50. if __name__ == "__main__":
  51.     curses.wrapper(main)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement