1 思路

绘制一个三角形,取三角形的三边中点,以每两个中点及其所在边共有的顶点,三个一组,组合成三个小三角形,对三个三角形进行绘制,如下图所示,分别为ADF,DBE,FEC,以上步骤再重复

2 Python程序

import turtle t = turtle.Turtle() t.hideturtle() FillColors=[ '#CAE1FF', '#FFEFDB', '#8470FF', '#FF6347', '#FFDEAD', '#C1FFC1' ] def get_midpoint(a, b): ax, ay = a bx, by = b return (ax bx) / 2, (ay by) / 2 def draw_triangle(a, b, c, depth): ax, ay = a bx, by = b cx, cy = c t.penup() _tcolor = FillColors[depth % len(FillColors)] t.color("black", _tcolor) t.goto(ax, ay) t.pendown() t.begin_fill() t.goto(bx, by) t.goto(cx, cy) t.goto(ax, ay) t.end_fill() t.penup() def draw_sierpinski(triangle, depth): a, b, c = triangle draw_triangle(a, b, c, depth) if depth == 0: return else: d = get_midpoint(a, b) e = get_midpoint(b, c) f = get_midpoint(c, a) draw_sierpinski([a, d, f], depth-1) draw_sierpinski([d, b, e], depth-1) draw_sierpinski([f, e, c], depth-1) if __name__ == '__main__': triangle = [[-200, -100], [0, 200], [200, -100]] draw_sierpinski(triangle, 5) turtle.done()

3 效果

pythonturtle图形大全(Pythonturtle绘制分形图三)(1)

,