我正在尝试创建一个可以使用Tkinter绘制带有动画的分形的程序,但是当我尝试对绘制过程进行动画处理时,出现了一个问题,该问题导致绘制的每条新线均与上一条不相交。这是代码
from tkinter import *
import math
import sys
sys.setrecursionlimit(10000)
class Curlicue:
def __init__(self, contstant, iterations):
self.root = Tk()
self.c = contstant
self.maxiter = iterations
self.root = Tk()
self.w = self.root.winfo_screenwidth()
self.h = self.root.winfo_screenheight()
self.canvas = Canvas(self.root, width=self.w, height=self.h)
self.canvas.pack()
def curli(self, iters):
c = self.c
# z=(z+2*math.pi*(i+1)*self.c)%(2*math.pi)
if iters == 0:
return 0
elif iters == 1:
return (4 * math.pi * c) % (2 * math.pi)
else:
return (self.curli(iters - 1) + 2 * math.pi * (iters + 1) * c) % (2 * math.pi)
def create_line(self):
rad = self.curli(0)
line_length = 10
center_x = 0
center_y = 600
end_x = center_x + line_length * math.cos(rad)
end_y = center_y + line_length * math.sin(rad)
self.canvas.create_line(center_x, center_y, end_x, end_y, fill='black', activewidth=10)
for i in range(1, self.maxiter):
next_x = end_x
next_y = end_y
end_x = next_x + line_length * math.cos(rad)
end_y = next_y + line_length * math.sin(rad)
dis_x = abs(end_x - next_x)
dis_y = abs(end_y - next_y)
new_line = self.canvas.create_line(next_x, next_y, end_x, end_y, fill='black', activewidth=10)
self.canvas.move(new_line, dis_x, dis_y)
self.canvas.update()
rad = self.curli(i)
self.canvas.pack()
mainloop()
c = Curlicue(math.pi, 100)
c.create_line()
如果我删除了self.canvas.move
函数,则线条会完美地相互连接,但是看不到动画。