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()

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.