Skip to content

Instantly share code, notes, and snippets.

@marty-Wallace
Created March 14, 2017 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marty-Wallace/895f5ad0db391a0087fedd72eaba1dad to your computer and use it in GitHub Desktop.
Save marty-Wallace/895f5ad0db391a0087fedd72eaba1dad to your computer and use it in GitHub Desktop.
Draw a sierpinsky triangle with Turtle graphics
import math
from turtle import *
size = 800
minimum = 8
pythagoras = math.sqrt(3) / 2
def s(l, x, y):
if l>minimum:
l = l/2
s(l, x, y)
s(l, x+l, y)
s(l, x+l/2, y+l*pythagoras)
else:
goto(x, y)
pendown()
begin_fill()
forward(l)
left(120)
forward(l)
left(120)
forward(l)
end_fill()
setheading(0)
penup()
goto(x,y)
penup()
speed('fastest')
s(size, -size/2, -size*pythagoras/2.0)
done()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment