turtle graphics - Drawing a fractal tree in Python -
i trying draw fractal tree in python, has 3 branches. know how draw tree 2 branches, 3 branches...not sure tried find examples, couldn`t. found examples of trees 2 branches. have ideas how that?
for 2 branches tree used following code:
import turtle def tree(f_lenght, min_lenght=10): """ draws tree 2 branches using recursion """ turtle.forward(f_lenght) if f_lenght > min_lenght: turtle.left(45) tree(0.6*f_lenght, min_lenght) turtle.right(90) tree(0.6*f_lenght, min_lenght) turtle.left(45) turtle.back(f_lenght) turtle.left(90) tree(100) turtle.exitonclick()
here expanded example. using method make branches, easy make them overlap added few parameters that. feel free play around code, example of arbitrary levels of recursion.
import turtle def tree(f_length, spray=90., branches=2, f_scale=0.5, f_scale_friction=1.4, min_length=10): """ draws tree 2 branches using recursion """ step = float(spray / (branches - 1)) f_scale /= f_scale_friction turtle.forward(f_length) if f_length > min_length: turtle.left(spray / 2) tree(f_scale * f_length, spray, branches, f_scale, f_scale_friction, min_length) counter in range(branches - 1): turtle.right(step) tree(f_scale * f_length, spray, branches, f_scale, f_scale_friction, min_length) turtle.left(spray / 2) turtle.back(f_length) turtle.left(90) tree(80, spray=120, branches=4) turtle.exitonclick()
Comments
Post a Comment