Created
March 14, 2017 19:27
-
-
Save marty-Wallace/895f5ad0db391a0087fedd72eaba1dad to your computer and use it in GitHub Desktop.
Draw a sierpinsky triangle with Turtle graphics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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