user3006676
user3006676

Reputation: 11

Python Sierpinski Triangle Using Class Point

I have to draw a Sierpinski Triangle using a class point, but when I try to run the program I made it won't let me draw the triangle. We have to have an input function for the length of the three sides of the big triangle(L) and the number of iterations(I).Help please! Here's what I have.

import turtle

class point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def midpoint(self, p2):
        return point((self.x + p2.x) / 2, (self.y + p2.y) / 2)

def DrawSierpinskiTriangle(length, iterations):
    turn = 0
    angle = 60
    # Initialize the turtle
    turtle.hideturtle()
    turle.penup()
    turtle.degrees()
    # Starting point on the cancas
    midpoint = ((self.x + p2.x) / 2, (self.y + p2.y) / 2)
    decode = {'-':Left, '+':Right, 'X':Forward, 'H':Forward}
    code = 'H--X--X'
    # Start the drawing
    turtle.goto(point[0], point[1])
    turtle.pendown()
    turtle.hideturtle()
    turt=turtle.getpen()
    startposition=turt.clone()
    # Get triangle
    path = code
    length = x
    for i in range(0,length):
        path = path.replace('X','XX')
        path = path.replace('H','H--X++H++X--H')
    for i in path:
        [turn, point, fwd, angle, turt]=decode[i](turn, point, fwd, angle, turt)

def main():
    input("Enter the length of the triangle: ")
    input("Enter the number of iterations: ")

main()

Upvotes: 1

Views: 1061

Answers (1)

CDspace
CDspace

Reputation: 2689

A) You probably want to use raw_input, if using python 2.x

B) You're not saving the inputted values. I.e.

tLength = input("Enter the length of the triangle: ")

C) You never call DrawSierpinskiTriangle

In summary, try the below, and that should get you back on track and you can begin debugging your Sierpinski function

def main():
    tLength = input("Enter the length of the triangle: ")
    numIter = input("Enter the number of iterations: ")
    DrawSierpinskiTriangle(tLength, numIter)

main()

Upvotes: 1

Related Questions