【发布时间】:2013-11-19 12:29:49
【问题描述】:
我必须使用类点来绘制谢尔宾斯基三角形,但是当我尝试运行我创建的程序时,它不会让我绘制三角形。我们必须有一个大三角形三边长度(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()
【问题讨论】:
-
您可能想要指定程序的更多细节,例如您正在使用 Python
turtle库来绘制三角形。您是否正确导入了turtle库?另外,您能否更详细地描述您的代码所遇到的错误?
标签: python python-3.x