Ryan Hutton
Ryan Hutton

Reputation: 3

Sierpinski Triangle Inversion

Hey I'm trying to invert a Sierpinski's Triangle in Python for class, I'm new to python and the turtle api. I was wondering if anyone could explain how the points work in the goto function as I'm having trouble understanding how to map out the inverted triangle correctly. (This code was given to me and I have only slightly modified it).

import turtle

def drawTriangle(points,color,myTurtle):
    myTurtle.fillcolor(color)
    myTurtle.up()
    myTurtle.goto(points[0][0],points[0][1])
    myTurtle.down()
    myTurtle.begin_fill()
    myTurtle.goto(points[1][0],points[1][1])
    myTurtle.goto(points[2][0],points[2][1])
    myTurtle.goto(points[0][0],points[0][1])
    myTurtle.end_fill()

def getMid(p1,p2):
    return ( (p1[0]+p2[0]) / 2, (p1[1] + p2[1]) / 2)

def sierpinski(points,degree,myTurtle):
    colormap = ['cyan','purple','orange','navy','gold',
                'firebrick','lawn green']
    drawTriangle(points,colormap[degree],myTurtle)
    if degree > 0:
        sierpinski([points[0],
                        getMid(points[0], points[1]),
                        getMid(points[0], points[2])],
                   degree-1, myTurtle)
        sierpinski([points[1],
                        getMid(points[0], points[1]),
                        getMid(points[1], points[2])],
                   degree-1, myTurtle)
        sierpinski([points[2],
                        getMid(points[2], points[1]),
                        getMid(points[0], points[2])],
                   degree-1, myTurtle)

def main():
   myTurtle = turtle.Turtle()
   myWin = turtle.Screen()
   myPoints = [[-200,-100],[0,200],[200,-100]]
   sierpinski(myPoints,3,myTurtle)
   myWin.exitonclick()

main()

Upvotes: 0

Views: 467

Answers (1)

kartikg3
kartikg3

Reputation: 2620

This is how the coords look (for when you run your code): enter image description here

In your main(), just change the original myPoints from

myPoints = [[-200,-100],[0,200],[200,-100]]

to:

myPoints = [[200,100],[0,-200],[-200,100]]

enter image description here

Upvotes: 1

Related Questions