gosper.py

Created by schraf

Created on December 07, 2023

1.66 KB

Script de Tell Urik @t3llurik


from math import *
from turtle import *

def td(angle) : # angle en degres
    global azimut
    azimut = azimut - angle 
    
def tg(angle) :
    global azimut
    azimut = azimut + angle 
    
def av(nb_pas) : # 1 pas = 1/100eme unité
    global x, y
    xx = x + nb_pas / 6 * cos(azimut * pi/180) # cos(angle en radians)
    yy = y + nb_pas / 6 * sin(azimut * pi/180) # sin(angle en radians)
    penup()
    goto(x, y)
    pendown()
    goto(xx, yy)
    x = xx
    y = yy
    
def gosper(longueur , sens, niveau) :
    if niveau == 0 :
        av(longueur)
    else :
        if sens == 1 :
            td(60)
            gosper(longueur / 2, -1, niveau - 1)
            tg(60)
            gosper(longueur / 2, 1, niveau - 1)
            gosper(longueur / 2, 1, niveau - 1)
            tg(120)
            gosper(longueur / 2, 1, niveau - 1)
            tg(60)
            gosper(longueur / 2, -1, niveau - 1)
            td(120)
            gosper(longueur / 2, -1, niveau - 1)
            td(60)
            gosper(longueur / 2, 1, niveau - 1)
            
        else :
            gosper(longueur / 2, -1, niveau - 1)
            tg(60)
            gosper(longueur / 2, 1, niveau - 1)
            tg(120)
            gosper(longueur / 2, 1, niveau - 1)
            td(60)
            gosper(longueur / 2, -1, niveau - 1)
            td(120)
            gosper(longueur / 2, -1, niveau - 1)
            gosper(longueur / 2, -1, niveau - 1)
            td(60)
            gosper(longueur / 2, 1, niveau - 1)
            tg(60)
            
# initialisation
xinit = 70
yinit = -70
x = xinit
y = yinit
azimut = 90

# Dessiner la courbe de Hilbert
gosper(360, 1, 4)
hideturtle()