Article épinglé

lundi 20 juillet 2020

Python fractal et turtle

Résultat

Script

import turtle
MINIMUM_BRANCH_LENGTH = 5
def build_tree(t, branch_length, shorten_by, angle):
  if branch_length > MINIMUM_BRANCH_LENGTH:
    t.forward(branch_length)
    new_length = branch_length - shorten_by
    t.left(angle)
    build_tree(t, new_length, shorten_by, angle)
    t.right(angle * 2)
    build_tree(t, new_length, shorten_by, angle)
    t.left(angle)
    t.backward(branch_length)
tree = turtle.Turtle()
tree.hideturtle()
tree.setheading(90)
tree.color('green')
build_tree(tree, 50, 5, 30)
turtle.mainloop()

Résultat

Script

import turtle
def koch_curve(t, iterations, length, shortening_factor, angle):
  if iterations == 0:
    t.forward(length)
  else:
    iterations = iterations - 1
    length = length / shortening_factor
    koch_curve(t, iterations, length, shortening_factor, angle)
    t.left(angle)
    koch_curve(t, iterations, length, shortening_factor, angle)
    t.right(angle * 2)
    koch_curve(t, iterations, length, shortening_factor, angle)
    t.left(angle)
    koch_curve(t, iterations, length, shortening_factor, angle)
t = turtle.Turtle()
t.hideturtle()
for i in range(3):
  koch_curve(t, 4, 200, 3, 60)
  t.right(120)
turtle.mainloop()

Aucun commentaire:

Enregistrer un commentaire

Tout commentaire nous engage ;)