遞歸函數繪制分形樹
分形幾何學的基本思想:客觀事物具有自相似的層次結構,局部與整體存在形态\功能\資訊\時間\空間方面具有統計意義上的相似性,成為自相似性.自相似性是指局部是整體成比例縮小的性質.
算法思路:
- 可以疊代的操作:
- 終止條件:樹枝長度初始30,小于5(某長度)停止
- 确定了長度,角度
"""
Draw a fractal tree using recursion.
"""
import turtle
def draw_branch(branch_length,):
if branch_length > 5:
#draw right branch
turtle.forward(branch_length)
turtle.right(20)
draw_branch(branch_length - 15)
#draw left branch
turtle.left(40)
draw_branch(branch_length - 15)
#Return to the previous branch
turtle.right(20)
turtle.backward(branch_length)
def main():
turtle.left(90)
turtle.penup()
turtle.backward(150)
turtle.pendown()
turtle.color('green')
turtle.forward(40)
draw_branch(100)
if __name__== '__main__':
main()