我必须使用类点绘制一个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()

最佳答案

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

关于python - 使用类点的Python Sierpinski三角形,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20059975/

10-11 11:35