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 = 101 # Resolution. Should be an odd number.
Y = 101
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.addch(row, col, mandelbrotTest(c))
canvas.refresh(0,0, 0,0, Y-1,X-1)
if __name__ == "__main__":
curses.wrapper(main)
Comments