Formation I.S.N.

Pythagore sylvestre

arbre de Pyth

Proposer une tortue python récursive réalisant :

  • Avec p = 1 :  arbre 1
  • Avec p = 2 :  arbre 2
  • Avec p = 3 :  arbre 3
  • Avec p = 10 :  Pythagore 10
  • documentation
  • un code

On trouvera la doc sur le module turtle ici.


import turtle as tl
from random import randint
from math import sqrt

 

def carre(cote) :
	tl.pencolor(randint(0,255), randint(0,255), randint(0,255))
	tl.fillcolor(randint(0,255), randint(0,255), randint(0,255))
	tl.begin_fill()
	for _ in range(4) :
		tl.forward(cote)
		tl.right(90)
	tl.end_fill()

def pythagore( cote, profondeur  ) :
	if profondeur > 0 :
		profondeur -= 1
		carre(cote)
		tl.forward(cote)
		cote2 = cote/sqrt(2)
		tl.left(45)
		pythagore(cote2, profondeur)
		tl.right(90)
		tl.penup()
		tl.forward(cote2)
		tl.pendown()
		pythagore(cote2, profondeur)
		tl.penup()
		tl.backward(cote2)
		tl.pendown()
		tl.left(45)
		tl.backward(cote)
	 
cote = 100		
tl.setworldcoordinates( -3*cote, -1*cote, 3.5*cote,4.5*cote)
tl.setheading(90) # orientation intiale de la tête : vers le haut
tl.hideturtle() # on cache la tortue
tl.speed(0)	 # on accélère la tortue
tl.colormode(255) # pour codage rgb des couleurs
pythagore( cote, 3 )
tl.exitonclick() # pour  garder ouverte la fenêtre

la forêt pythagoricienne

Généraliser le code précédent avec des angles quelconques.

 arbre 1
  • documentation
  • un code

On trouvera la doc sur le module turtle ici.


import turtle as tl
from random import randint
from math import sqrt, cos, sin, pi 

def cosinus(angle) :
	""" angle en degrés """
	return cos(pi * angle/180)
	
def sinus(angle) :
	""" angle en degrés """
	return sin(pi * angle/180)

def carre(cote) :
	tl.pencolor(randint(0,255), randint(0,255), randint(0,255))
	tl.fillcolor(randint(0,255), randint(0,255), randint(0,255))
	tl.begin_fill()
	for _ in range(4) :
		tl.forward(cote)
		tl.right(90)
	tl.end_fill()

def pythagore( cote, angle, profondeur  ) :
	if profondeur > 0 :
		profondeur -= 1
		carre(cote)
		tl.forward(cote)
		cote2 = cote * cosinus(angle)
		tl.left(angle)
		pythagore(cote2, angle, profondeur)
		tl.right(90)
		tl.penup()
		tl.forward(cote2)
		tl.pendown()
		cote3 = cote * sinus(angle)
		pythagore(cote3, angle, profondeur)
		tl.penup()
		tl.backward(cote2)
		tl.pendown()
		tl.left(90-angle)
		tl.backward(cote)
	 
cote = 100		
tl.setworldcoordinates( -3*cote, -1*cote, 3.5*cote,4.5*cote)
tl.setheading(90) # orientation intiale de la tête : vers le haut
tl.hideturtle() # on cache la tortue
tl.speed(0)	 # on accélère la tortue
tl.colormode(255) # pour codage rgb des couleurs
pythagore( cote,30, 5 )
tl.exitonclick()