说一下94 – 绘制谢尔宾斯基三角形

绘制谢尔宾斯基三角形

import turtle

# 绘制单个三角形
# [[x1,y1], [x2,y2], [x3,y3]]
def draw_triangle(points, color, t):
    t.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])
    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], t)
    if degree >= 0:
        # 绘制左下角三角形
        sierpinski([points[0], mid_point(points[0], points[1]), mid_point(points[0], points[2])], degree - 1, t)
        # 绘制上方的三角形
        sierpinski([points[1], mid_point(points[0], points[1]), mid_point(points[1], points[2])], degree - 1, t)
        # 绘制右下角三角形
        sierpinski([points[2], mid_point(points[2], points[1]), mid_point(points[0], points[2])], degree - 1, t)
        
t = turtle.Turtle()
win = turtle.Screen()
points = [[-200, -100], [0, 200], [200, -100]]
sierpinski(points, 4, t)
win.exitonclick()

正文完