clairec icon

BADASS FUCKING FRACTAL

clairec | PRO | 04/30/16 10:16:53 AM UTC | 0 ⭐ | 486 👁️ | Never ⏰ | []
Python |

1.2 KB

|

None

|

0 👍

/

0 👎

import curses
 
 
def mandelbrotTest(c):
    ''''Tests to see if a point is is the mandelbrot set, and assigns it
    a character based on how quickly it escaped, if at all.'''
 
    iToChar = {5:'@', 4:'O', 3:'*', 2:'.', 1:'`', 0:' '} 
    
    f = lambda z: z ** 2 + c
    Z = f(0)
    i = 0
    
    while abs(Z) <= 2:
        if i > 500:
            break
        Z = f(Z)
        i += 1
    
    return iToChar[int(i / 100)]
 
 
 
def main(stdscr):
    X = 51        # Resolution. Should be an odd number.
    Y = 51
    
    xMid = X // 2  # This represents the axes on a complex plane.
    yMid = Y // 2
    
    dx = 2 / xMid  # How much each cell represents in the context 
    dy = 2 / yMid  # of the complex plane.
   
    conCol = lambda col: -2 + dx * col # Converts a colum/row to the equivalent   
    conRow = lambda row: 2 - dy * row  # Complex number
    
    stdscr.clear()
    stdscr.refresh()
    canvas = curses.newpad(Y,X)
    
    for row in range(Y):
        for col in range(X):
            x, y = conCol(col), conRow(row)
            c = complex(x, y)
            canvas.addstr(row, col, mandelbrotTest(c))
            canvas.refresh(0,0, 0,0, Y,X)
 
 
 
if __name__ == "__main__":
    curses.wrapper(main)

Comments