立即学习:https://edu.csdn.net/course/play/26755/340208?utm_source=blogtoedu
代码为
import turtle
#绘制三角形,左边为[[x1,y1], [x2,y2], [x3,y3]]
def draw_triangle(points, color, t):
d.fillcolor(color)
t.up()
#将画笔移动到第一个点
t.goto(points[0][0], points[0][1])
t.down()
t.begin_fill()#填充颜色
t.goto(points[1][0], points[1][1])#绘制了3条边
t.goto(points[2][0], points[2][1])
t.goto(points[0][0], points[0][1])
t.end_fill()
#求中位点
def mid_point(p1,p2):
return [(p1[0] + p2[0])/2, (p1[1] + p2[1])/2]
def sierpinski(points, degree, t):
colormap = ['blue', 'red', 'green', 'yellow', 'violet', 'orange']
#绘制大的三角形
draw_triangle(points, colormap[degree - 1], t)
if degree >= 0:
#绘制左下角三角形
sierpinski([points[0], mid_point(points[0],points[1]), mid_po