python 三角形类_使用类Poin的Python-Sierpinski三角形

我必须用类点来画一个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()

本文链接:https://my.lmcjl.com/post/4854.html

展开阅读全文

4 评论

留下您的评论.