bettermdbrot.py

Created by nonogamer9

Created on October 11, 2024

1.41 KB

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()

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.