使用类点的Python Sierpinski三角形

时间:2013-11-18 23:12:06

标签: python python-3.x

我必须使用类点绘制一个Sierpinski三角形,但是当我尝试运行程序时,我不会让我绘制三角形。我们必须有一个输入函数,用于大三角形(L)的三边长度和迭代次数(I)。请帮助!这就是我所拥有的。

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()

1 个答案:

答案 0 :(得分:1)

A)如果使用python 2.x

,您可能想要使用raw_input

B)您没有保存输入的值。即

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

C)你永远不会打电话给DrawSierpinskiTriangle

总之,请尝试以下操作,这应该让您重新回到正轨,您可以开始调试您的Sierpinski功能

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

main()