mandelbrot.py

Created by alban-scientifique

Created on June 07, 2024

1.56 KB


import kandinsky
import ion
from ion import *

def mandelbrot(N_iteration, x_center, y_center, scale):
    for x in range(320):
        for y in range(222):
            # Calculer la séquence de Mandelbrot pour le point c = (c_r, c_i)
            z = complex(0, 0)
            # Redimensionner pour s'adapter à l'écran de dessin (320x222) avec des ajustements de zoom et de panoramique
            c = complex(scale * (x - 160) / 159 + x_center, scale * (y - 111) / 111 + y_center)
            i = 0
            while (i < N_iteration) and abs(z) < 2:
                i += 1
                z = z * z + c
            # Choisir la couleur du point de la séquence de Mandelbrot
            rgb = int(255 * i / N_iteration)
            col = kandinsky.color(int(rgb), int(rgb * 0.75), int(rgb * 0.25))
            # Dessiner un pixel coloré en 'col' à la position (x, y)
            kandinsky.set_pixel(x, y, col)

def main():
    N_iteration = 50
    x_center = -0.75
    y_center = 0
    scale = 3.5
    zoom_factor = 0.9
    move_factor = 0.1

    while True:
        mandelbrot(N_iteration, x_center, y_center, scale)
        
        if ion.keydown(KEY_EXE):
            scale *= zoom_factor
        elif ion.keydown(KEY_LEFT):
            x_center -= scale * move_factor
        elif ion.keydown(KEY_RIGHT):
            x_center += scale * move_factor
        elif ion.keydown(KEY_UP):
            y_center += scale * move_factor
        elif ion.keydown(KEY_DOWN):
            y_center -= scale * move_factor
        elif ion.keydown(KEY_BACK):
            break

main()

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.