这个就是八个科赫曲线!!!
import turtle
import time
def koch(size, n): # 定义弯曲的直线使其有棱角
if n == 0:
turtle.fd(size)
else:
for angle in [0, 60, -120, 60]: # 每一次都对其进行合理的角度改变
turtle.left(angle)
koch(size / 3, n - 1)
def main(n):
global a
global s
if a == 1:
turtle.color("black") # 这边是控制画笔的颜色
elif a == 2:
turtle.color("purple")
elif a == 3:
turtle.color("blue")
elif a == 4:
turtle.color("Cyan")
elif a == 5:
turtle.color("green")
elif a == 6:
turtle.color("yellow")
elif a == 7:
turtle.color("orange")
elif a == 8:
turtle.color("red")
if n != 0:
a += 1
turtle.penup()
turtle.goto(-200 * (4 / 7) ** (s - n), 100 * (4 / 7) ** (s - n))
# 每一次都是需要合理的等比例的扩大或是缩小
turtle.pendown()
turtle.pensize(2)
level = 5
koch(400 * (4 / 7) ** (s - n), level) # 大小是一个一个试出来的
# 不能超过setup的设定的范围 600,不然就越界了
turtle.right(120)
koch(400 * (4 / 7) ** (s - n), level)
turtle.right(120)
koch(400 * (4 / 7) ** (s - n), level) # 注意这里koch一次就要转一次角度
turtle.right(120)
turtle.hideturtle() # 隐藏画笔
main(n - 1)
a=1
s = int(input()) # 输入其中的科赫曲线的个数
turtle.setup(600, 600)
turtle.speed(0)
main(s) # main(8)
最后附上效果图
可能还是太小了,里面的看的不是很清楚。
PS:这个画的很慢,即便是speed(0)也很慢,所以调试的时候,最好先用level=2来进行调试。否则就很难顶了。。。
大佬的话就当我没说