le_papillon.py

Created by arthurmichel-scolaire

Created on April 26, 2026

4.9 KB


from kandinsky import *
import ion

W, H = 320, 240

XMIN, XMAX = -8.8, 8.8
YMIN, YMAX = -6.8, 7.2

BG      = (10, 8, 28)
ORANGE  = (255, 150, 60)
YELLOW  = (255, 215, 90)
PINK    = (255, 120, 185)
CYAN    = (80, 225, 255)
MINT    = (110, 255, 205)
BLUE    = (110, 175, 255)
INDIGO  = (145, 125, 255)
VIOLET  = (215, 115, 255)
WHITE   = (245, 245, 255)
GOLD    = (255, 238, 125)
BODY    = (255, 240, 180)

def to_pixel(x, y):
    px = int((x - XMIN) * (W - 1) / (XMAX - XMIN))
    py = int((YMAX - y) * (H - 1) / (YMAX - YMIN))
    return px, py

def inside(px, py):
    return 0 <= px < W and 0 <= py < H

def dot(px, py, color, r=1):
    for dx in range(-r, r + 1):
        for dy in range(-r, r + 1):
            x2 = px + dx
            y2 = py + dy
            if 0 <= x2 < W and 0 <= y2 < H:
                set_pixel(x2, y2, color)

def line(px0, py0, px1, py1, color, r=1):
    dx = px1 - px0
    dy = py1 - py0
    steps = max(abs(dx), abs(dy))
    if steps == 0:
        dot(px0, py0, color, r)
        return
    for i in range(steps + 1):
        x = int(px0 + dx * i / steps)
        y = int(py0 + dy * i / steps)
        dot(x, y, color, r)

def dim(color, k=0.30):
    return (int(color[0] * k), int(color[1] * k), int(color[2] * k))

def curve(f, xmin, xmax, color, thickness=1, samples=None):
    if samples is None:
        samples = max(50, int((xmax - xmin) * 80))
    prev = None
    for i in range(samples + 1):
        x = xmin + (xmax - xmin) * i / samples
        y = f(x)
        px, py = to_pixel(x, y)

        if inside(px, py):
            if prev is None:
                dot(px, py, color, thickness)
            else:
                line(prev[0], prev[1], px, py, color, thickness)
            prev = (px, py)
        else:
            prev = None

def glow_curve(f, xmin, xmax, color, thickness=1):
    curve(f, xmin, xmax, dim(color, 0.28), thickness + 1)
    curve(f, xmin, xmax, color, thickness)

def symmetric_curve(f_right, xmin, xmax, color, thickness=1):
    glow_curve(f_right, xmin, xmax, color, thickness)
    glow_curve(lambda x, fr=f_right: fr(-x), -xmax, -xmin, color, thickness)

# Fond 
fill_rect(0, 0, W, H, BG)

# Ailes 
# Grandes courbes aile supérieure
symmetric_curve(lambda x: -0.22*(x-4.0)**2 + 5.7, 0.6, 7.4, ORANGE, 2)
symmetric_curve(lambda x: -0.24*(x-4.0)**2 + 4.6, 1.1, 6.9, YELLOW, 2)
symmetric_curve(lambda x: -0.28*(x-4.0)**2 + 3.6, 1.7, 6.3, PINK, 2)

# Courbes inférieures aile supérieure
symmetric_curve(lambda x: -0.16*(x-3.2)**2 + 2.6, 0.3, 5.0, CYAN, 1)
symmetric_curve(lambda x: -0.12*(x-3.7)**2 + 1.6, 1.0, 6.0, MINT, 1)

# Bord externe aile supérieure
symmetric_curve(lambda x: 1.35*(x-7.2)**2 + 1.45, 5.6, 7.0, ORANGE, 2)

# Grandes courbes aile inférieure
symmetric_curve(lambda x: -0.09*(x-3.3)**2 - 0.7, 0.5, 6.2, BLUE, 2)
symmetric_curve(lambda x: 0.030*(x-3.8)**4 - 4.9, 0.8, 6.0, INDIGO, 2)
symmetric_curve(lambda x: 0.020*(x-3.6)**4 - 3.8, 1.3, 5.6, VIOLET, 1)

# Bord externe aile inférieure
symmetric_curve(lambda x: -0.80*(x-4.9)**2 - 1.3, 5.0, 6.6, BLUE, 2)

# structure 
symmetric_curve(lambda x: -0.05*(x-1.0)**3 - 0.18*(x-1.0)**2 + 2.8, 0.2, 3.8, WHITE, 1)
symmetric_curve(lambda x: -0.025*(x-0.8)**3 - 0.08*(x-0.8)**2 + 3.5, 0.2, 5.0, PINK, 1)
symmetric_curve(lambda x: -0.012*(x-0.7)**3 - 0.14*(x-0.7)**2 - 0.6, 0.2, 5.2, WHITE, 1)
symmetric_curve(lambda x: -0.020*(x-1.4)**3 - 0.18*(x-1.4)**2 - 1.0, 0.7, 3.8, VIOLET, 1)

# Motifs aile supérieure
symmetric_curve(lambda x: -1.8*(x-2.3)**2 + 4.5, 1.9, 2.7, GOLD, 1)
symmetric_curve(lambda x:  1.6*(x-2.3)**2 + 3.8, 1.95, 2.65, GOLD, 1)

symmetric_curve(lambda x: -1.2*(x-5.5)**2 + 4.6, 5.0, 6.0, GOLD, 1)
symmetric_curve(lambda x:  1.0*(x-5.5)**2 + 3.9, 5.05, 5.95, GOLD, 1)

# Motifs aile inférieure
symmetric_curve(lambda x: -1.6*(x-2.7)**2 - 1.8, 2.3, 3.1, MINT, 1)
symmetric_curve(lambda x:  1.5*(x-2.7)**2 - 2.6, 2.35, 3.05, MINT, 1)

symmetric_curve(lambda x: -1.2*(x-4.8)**2 - 2.1, 4.4, 5.2, MINT, 1)
symmetric_curve(lambda x:  1.1*(x-4.8)**2 - 2.8, 4.45, 5.15, MINT, 1)


# Corps papillon 

glow_curve(lambda x: -3.5*x**2 + 5.0, -0.45, 0.45, BODY, 2)
glow_curve(lambda x:  2.5*x**2 + 4.2, -0.35, 0.35, BODY, 1)

glow_curve(lambda x: -6.0*x**2 + 3.5, -0.55, 0.55, BODY, 2)
glow_curve(lambda x:  4.5*x**2 + 2.0, -0.45, 0.45, BODY, 1)

glow_curve(lambda x: -5.0*x**2 + 1.3, -0.50, 0.50, BODY, 2)
glow_curve(lambda x:  4.0*x**2 - 0.3, -0.40, 0.40, BODY, 1)

glow_curve(lambda x: -4.0*x**2 - 1.3, -0.45, 0.45, BODY, 2)
glow_curve(lambda x:  3.0*x**2 - 3.2, -0.35, 0.35, BODY, 1)

glow_curve(lambda x: -2.5*x**2 - 3.9, -0.30, 0.30, BODY, 2)
glow_curve(lambda x:  8.0*x**2 - 6.2, -0.20, 0.20, BODY, 1)


# Antennes 
glow_curve(lambda x: -0.9*(x-0.2)**3 + 1.8*(x-0.2)**2 + 5.1, 0.2, 1.6, WHITE, 1)
glow_curve(lambda x:  0.9*(x+0.2)**3 + 1.8*(x+0.2)**2 + 5.1, -1.6, -0.2, WHITE, 1)

# Embouts des antennes 
symmetric_curve(lambda x: -14*(x-1.65)**2 + 6.35, 1.55, 1.75, GOLD, 1)
symmetric_curve(lambda x:  11*(x-1.65)**2 + 6.05, 1.57, 1.73, GOLD, 1)

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.