科赫曲线绘制

递归思想:函数加分支
递归链条:线段的组合
递归基例:初始线段

#科赫雪花绘制小包裹
import turtle as tu
def koch(size, n):
    if n == 0:
        tu.fd(size)
    else:
        for angle in [0, 60, -120, 60]:
            tu.left(angle)
            koch(size / 3, n - 1)
def main():
    tu.setup(800, 400)
    tu.penup()
    tu.goto(-300, -50)
    tu.pendown()
    tu.pensize(2)
    koch(600, 3)
    tu.hideturtle()

科赫雪花

#科赫雪花绘制小包裹
import turtle as tu
def koch(size, n):
 if n == 0:
     tu.fd(size)
 else:
     for angle in [0, 60, -120, 60]:
         tu.left(angle)
         koch(size / 3, n - 1)
def main():
 tu.setup(600, 600)
 tu.penup()
 tu.goto(-200, 100)
 tu.pendown()
 tu.pensize(2)
 level = 3
 koch(400, level)
 tu.right(120)
 koch(400, level)
 tu.right(120)
 koch(400, level)
 tu.hideturtle()
main()

运行效果