Skip to content

Instantly share code, notes, and snippets.

@ecylmz
Created December 27, 2010 18:21
Show Gist options
  • Save ecylmz/756378 to your computer and use it in GitHub Desktop.
Save ecylmz/756378 to your computer and use it in GitHub Desktop.
from graphics import *
def sierpinskiT(points,level,win):
colormap = ['blue','red','green','white','yellow','violet','orange']
p = Polygon(points)
p.setFill(colormap[level])
p.draw(win)
if level > 0:
sierpinskiT([points[0],getMid(points[0],points[1]),getMid(points[0],points[2])],level-1,win)
sierpinskiT([points[1],getMid(points[0],points[1]),getMid(points[1],points[2])],level-1,win)
sierpinskiT([points[2],getMid(points[2],points[1]),getMid(points[0],points[2])],level-1,win)
def getMid(p1,p2):
return Point( ((p1.getX()+p2.getX()) / 2.0), ((p1.getY()+p2.getY()) / 2.0))
if __name__ == '__main__':
win = GraphWin('st',500,500)
win.setCoords(20,-10,80,50)
myPoints = [Point(25,0),Point(50,43.3),Point(75,0)]
sierpinskiT(myPoints,6,win)
win.getMouse()
win.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment