fractale_test_2.py

Created by dan-tabet

Created on September 30, 2025

1.19 KB


import kandinsky
WIDTH, HEIGHT = 320, 222
C = complex(-0.8, 0.156)  # paramètre de Julia (change pour d'autres formes)
MAX_ITER = 50             # nombre max d'itérations
ESCAPE_RADIUS = 4.0       # seuil de divergence

def julia(z):
    """Retourne le nombre d'itérations avant divergence."""
    i = 0
    while i < MAX_ITER and (z.real*z.real + z.imag*z.imag) <= ESCAPE_RADIUS:
        z = z*z + C
        i += 1
    return i

def iter_to_color(i):
    """Convertit une itération en couleur RGB."""
    if i >= MAX_ITER:
        return (0, 0, 0)  # noir dans l'ensemble
    t = i / MAX_ITER
    r = int(255 * t)
    g = int(128 * (1 - t))
    b = int(255 * (1 - t))
    return (r, g, b)

def pixel_to_complex(px, py):
    """Convertit un pixel écran en coordonnée complexe (ajustée au ratio)."""
    zx = 3.0 * (px / WIDTH) - 1.5      # intervalle [-1.5, +1.5]
    zy = 2.0 * (py / HEIGHT) - 1.0     # intervalle [-1.0, +1.0]
    return complex(zx, -zy)            # inversion verticale

def main():
    for y in range(HEIGHT):
        for x in range(WIDTH):
            z = pixel_to_complex(x, y)
            i = julia(z)
            kandinsky.set_pixel(x, y, iter_to_color(i))

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.