Le principe générateur
Proposer une tortue python récursive réalisant :
- Avec n = 0 :
- Avec n = 1 :
- Avec n = 2 :
- Avec n = 3 :
Le tapis triangulaire
En déduire une fonction réalisant ce qui suit
Avec n = 3 :
- documentation
- cas de base
- courbe
- tapis
On trouvera la doc sur le module turtle ici.
Le cas n = 1
import turtle as tl
def principe(cote) :
cote /= 2
tl.forward( cote)
tl.right(120)
tl.forward( cote)
tl.right(-120)
tl.forward( cote)
tl.right(-120)
tl.forward( cote)
tl.right(120)
tl.forward( cote)
tl.setheading(0) # orientation intiale de la tête : vers la droite de l'écran
tl.hideturtle() # on cache la tortue
tl.speed(0) # on accélère la tortue
tl.color('magenta')
principe(cote = 200)
tl.exitonclick() # pour garder ouverte la fenêtre
import turtle as tl
def sierpinski(n, cote) :
if (n == 0) : tl.forward(cote)
else :
cote /= 2
sierpinski(n-1, cote)
tl.right(120)
sierpinski(n-1, cote)
tl.right(-120)
sierpinski(n-1, cote)
tl.right(-120)
sierpinski(n-1, cote)
tl.right(120)
sierpinski(n-1, cote)
tl.setheading(0) # orientation intiale de la tête : vers la droite de l'écran
tl.hideturtle() # on cache la tortue
tl.speed(0) # on accélère la tortue
tl.color('magenta')
sierpinski( n = 4, cote = 200 )
tl.exitonclick() # pour garder ouverte la fenêtre
import turtle as tl
def sierpinski(n, cote) :
if (n == 0) : tl.forward(cote)
else :
cote /= 2
sierpinski(n-1, cote)
tl.right(120)
sierpinski(n-1, cote)
tl.right(-120)
sierpinski(n-1, cote)
tl.right(-120)
sierpinski(n-1, cote)
tl.right(120)
sierpinski(n-1, cote)
def triangleSierpinski(n, cote) :
for _ in range(3) :
sierpinski(n, cote)
tl.right(120)
tl.setheading(0) # orientation intiale de la tête : vers la droite de l'écran
tl.hideturtle() # on cache la tortue
tl.speed(0) # on accélère la tortue
tl.color('magenta')
triangleSierpinski( n = 4, cote = 200 )
tl.exitonclick() # pour garder ouverte la fenêtre