Mandelbrot Set on Adafruit CLUE

by | Jul 2, 2020

The Mandelbrot set is an implentation of fractal theory that makes an ideal test for coding on any device.

Mandelbrot Set for Adafruit Clue (MicroPython)

# Manage Capacitor Voltage using micro:bit
# Copyright R J Zealley 10/07/2020
# Licence: copy and resuse with attribution 

import board
from adafruit_clue import clue
from adafruit_display_shapes.circle import Circle
#from adafruit_display_shapes.line import Line
import displayio

MAX_ITER = 24
WIDTH = 240
HEIGHT = 240

# Plot window
RE_START = -2
RE_END = 1
IM_START = -1
IM_END = 1

def mandelbrot(c):
    z = 0
    n = 0
    while abs(z) <= 2 and n < MAX_ITER:
        z = z*z + c
        n += 1
    return n

def show_values():
    for a in range(-10, 10, 5):
        for b in range(-10, 10, 5):
            c = complex(a / 10, b / 10)
            print(c, mandelbrot(c))

def clear_screen():
    # Make a background color fill
    color_bitmap = displayio.Bitmap(320, 240, 1)
    color_palette = displayio.Palette(1)
    color_palette[0] = 0xFFFFFF
    bg_sprite = displayio.TileGrid(color_bitmap, x=0, y=0, pixel_shader=color_palette)
    splash.append(bg_sprite)

def draw_mandelbrot():
    color_bitmap = displayio.Bitmap(WIDTH, HEIGHT, 16)
    color_palette = displayio.Palette(16)

    color_palette[0] = 0x000000
    color_palette[1] = 0x000080
    color_palette[2] = 0x008000
    color_palette[3] = 0x008080
    color_palette[4] = 0x800000
    color_palette[5] = 0x800080
    color_palette[6] = 0x808000
    color_palette[7] = 0xC0C0C0
    color_palette[8] = 0x808080
    color_palette[9] = 0x0000FF
    color_palette[10] = 0x00FF00
    color_palette[11] = 0x00FFFF
    color_palette[12] = 0xFF0000
    color_palette[13] = 0xFF00FF
    color_palette[14] = 0xFFFF00
    color_palette[15] = 0xFFFFFF

    for x in range(0, WIDTH):
        for y in range(0, HEIGHT):
            # Convert pixel coordinate to complex number
            c = complex(RE_START + (x / WIDTH) * (RE_END - RE_START),
                        IM_START + (y / HEIGHT) * (IM_END - IM_START))
            # Compute the number of iterations
            m = mandelbrot(c)
            # The color depends on the number of iterations
            color = 15 - int(m * 15 / MAX_ITER)

            # Plot the point
            #splash.append(Line(x, y, x, y, color))
            #splash.append(Circle(x, y, 1, outline=color))
            #draw.point([x, y], (color, color, color))
            color_bitmap[x,y] = color

    bg_sprite = displayio.TileGrid(color_bitmap, x=0, y=0, pixel_shader=color_palette)
    splash.append(bg_sprite)

def calc_mandelbrot():
    for x in range(0, WIDTH):
        for y in range(0, HEIGHT):
            # Convert pixel coordinate to complex number
            c = complex(RE_START + (x / WIDTH) * (RE_END - RE_START),
                        IM_START + (y / HEIGHT) * (IM_END - IM_START))
            # Compute the number of iterations
            m = mandelbrot(c)
            # The color depends on the number of iterations
            color = 15 - int(m * 15 / MAX_ITER)
            # Plot the point
            print(str(x), str(y), str(color))

if __name__ == "__main__":
    print("Mandelbrot:")

    while True:
        if clue.button_a:
            splash = displayio.Group(max_size=4)
            board.DISPLAY.show(splash)

            clear_screen()

            draw_mandelbrot()

        if clue.button_b:
            calc_mandelbrot()