# mandel2
#Mandelbrot using gridsample
from __future__ import division, print_function
import sys
from complex import Complex
from gridsample import GridSample
from graphics import GraphWin, color_rgb, Circle
LIMIT = 1000
def mandelfn(x, y):
""" Compute Mandelbrot iterations for point (x,y) in complex plane"""
c = Complex(x,y)
z = c
count = 0
while count < LIMIT and z.mag2() < 4:
z = z*z + c
count += 1
return count
def bwmap():
"""Create simple black and white color map"""
return ["white"]*LIMIT +["black"]
def histMap(histogram, rgb1, rgb2):
"""Create color map that blends from rgb1 to rgb2 using cumulative
frequency counts in histogram.
pre: histogram is a list of non-negative ints, rgb1 and rgb2 are
color triples (r,g,b), where r,g,b, are ints in range(256)
post: returns a list, colors, of graphics colors such that
colors[i] is a color that is cum/total of the way between
rgb1 and rgb2 where cum is sum(histogram[:i+1]), and total
is sum(histogram).
"""
def displayGrid(grid, win, colormap):
"""display grid in win using colormap
pre: grid and graphwin are the same size (width and height),
colormap is a list of graphics colors and len(colormap) >= Max of
grid values + 1
post: grid is drawn in win using in colormap to represent grid values.
"""
def main():
try:
width = int(sys.argv[1])
except (IndexError, ValueError):
width = 350
height = int(round(4/7 * width))
print("Calculating Points")
grid = GridSample(width, height)
grid.setCoords(-2.5, -1, 1, 1)
grid.samplefn(mandelfn)
print("done")
win = GraphWin("Mandelbrot", width, height, autoflush=False)
colors = bwmap()
# for color display uncomment the next 3 lines
# hist = grid.histogram()[:-1]
#colors = histMap(hist, (0,0,180), (255,255,255))
#colors.append("black")
displayGrid(grid, win, colors)
win.close()
main()