pythagore.py

Created by schraf

Created on August 11, 2022

1.08 KB


from turtle import *
from random import randint
from math import sqrt
from kandinsky import fill_rect

def degrade(a, b, nbVal = 10):
    (r1,g1,b1) = a
    (r2,g2,b2) = b
    dr = int((r2 - r1) / nbVal)
    dg = int((g2 - g1) / nbVal)
    db = int((b2 - b1) / nbVal)
    color = []
    for i in range(nbVal):
        color.append((r2, g2, b2))
        r2 = r2 - dr
        g2 = g2 - dg
        b2 = b2 - db
    return color


def carre(cote) :
  for _ in range(4) :
    forward(cote)
    right(90)

def pythagore(cote, profondeur):
  if profondeur > 0 :
    color(coul[profondeur])
    pensize(profondeur // 2)
    profondeur -= 1
    carre(cote)
    forward(cote)
    cote2 = cote/sqrt(2)
    left(45)
    pythagore(cote2, profondeur)
    right(90)
    penup()
    forward(cote2)
    pendown()
    pythagore(cote2, profondeur)
    penup()
    backward(cote2)
    pendown()
    left(45)
    backward(cote)

coul = degrade((150,80,0), (0,255,0), 11)
fill_rect(0,0,320,222,(0,0,0))
cote = 55
penup()
goto(-cote/2,-105)
pendown()
setheading(90)
speed(0)
pythagore(cote,10)
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 <a href="https://www.numworks.com/legal/cookies-policy/">cookies policy</a>.