A Better mandelbrot set for numworks that has more colors, zooms much deeper and much more!
from kandinsky import fill_rect, color from math import log from time import monotonic def mandelbrot(x, y, max_iter): c = complex(x, y) z = 0 for n in range(max_iter): if abs(z) > 2: return n z = z*z + c return max_iter def draw_mandelbrot(xmin, xmax, ymin, ymax, max_iter): width, height = 320, 222 for px in range(width): for py in range(height): x = xmin + (xmax - xmin) * px / width y = ymin + (ymax - ymin) * py / height n = mandelbrot(x, y, max_iter) if n == max_iter: c = color(0, 0, 0) else: h = int(255 * n / max_iter) c = color(h, int(h * 0.75), int(h * 0.25)) fill_rect(px, py, 1, 1, c) def zoom_mandelbrot(): xc, yc = -0.75, 0 zoom_factor = 1 max_iter = 50 while True: start_time = monotonic() zoom_width = 3.5 / zoom_factor zoom_height = 2.5 / zoom_factor xmin = xc - zoom_width/2 xmax = xc + zoom_width/2 ymin = yc - zoom_height/2 ymax = yc + zoom_height/2 draw_mandelbrot(xmin, xmax, ymin, ymax, max_iter) end_time = monotonic() print("Frame time:", end_time - start_time, "seconds") zoom_factor = zoom_factor * 1.5 max_iter = min(max_iter + 5, 100) zoom_mandelbrot()