######################### # # # Sierpinski triangle # # Live coding demo # # Roman Lossa # # 2011-10-06 # # # ######################### # import turtle module from turtle import * # speed up the turtle speed(0) delay(0) hideturtle() # triangle function def triangle(dist=400, x="", y=""): # end of recursion if dist < 5: return # set start position at first call if x=="" or y=="": x = -dist/2 y = -dist/2 # move to start position penup() goto(x, y) pendown() # bottom line x1,y1 = position() # start pos of 1st sub triangle forward(dist/2) x2,y2 = position() # start pos of 2nd sub triangle forward(dist/2) left(120) # right line forward(dist) left(120) # left line forward(dist/2) x3,y3 = position() # start pos of 3rd sub triangle forward(dist/2) left(120) # draw sub triangles triangle(dist/2, x1, y1) triangle(dist/2, x2, y2) triangle(dist/2, x3, y3) # speed up drawing tracer(1000) # execute triangle() # for usability reasons... exitonclick()