#!/usr/bin/python # mandel.py v. 1.0 # 2009 - Seth Just # Licensed under Creative Commons Attribution-Noncommercial-Share Alike 3.0 License # This software is provided with no warranty of any kind. # The author is not liable for any consequences arising from the use of this software. # Set size of render in rows and columns s_width = 80 s_height = 24 # Set width of view width = 3 # Set center of view (x,y) = (-1,0) # Ensure values are floats width = float(width) x = float(x) y = float(y) # Caluculate width so that view is square height = s_height*(width/s_width)*2 # Calculate coordinate offsets x_off = (width/2)-x y_off = (height/2)-y # Returns appropriate symbol from a gradient def symbol(iter, max_iter): gradient = ['.',':','o','O','8','@'] return gradient[int(iter*(len(gradient)-1)/max_iter)] # Calculates whether a number diverges after max_iter iterations def mandel(a,max_iter): z = complex(0) iter = 0 while iter < max_iter: iter+=1 z = z*z + a if abs(z) >= 2: break return symbol(iter,max_iter) # Main Program Here # Outer loop iterates over rows for i in range(1, s_height): # Set imaginary value based on the row's number comp = -1*(((i/float(s_height))*height)-y_off) # Initialize a buffer for the row's content line = [] # Inner loop iterates across the columns of the row for j in range(1, s_width): # Set the real value based on the column's number real = (((j/float(s_width))*width)-x_off) # Figure out which symbol to render and add it to the buffer sym = mandel(complex(real, comp),20) line.append(sym) # Print the row print ''.join(line)