在那里,我使用赛璐珞库创建了一个动画。此动画显示沿椭圆线移动的点。问题是,当我运行这段代码时,一切都在崇高的文本中工作,但是当我使用 Jupyter 时,它会输出错误。我怎么解决这个问题?您可以在答案中看到我的代码。
import math
import numpy as np
import matplotlib.pyplot as plt
from celluloid import Camera
import copy
img = np.ones((90,60,3))
fig = plt.figure(figsize=(10,8), dpi = 100, facecolor='w')
camera = Camera(fig)
quarterCircleOne = []
quarterCircleTwoHalfOne = []
quarterCircleTwoHalfTwo = []
quarterCircleThree = []
quarterCircleFourHalfOne = []
quarterCircleFourHalfTwo = []
xc = 45
yc = 30
rx = 25
ry = 15
def GenerateElipse(xc,yc,rx,ry):
img[xc, yc] = (255,0,0)
img[xc+1, yc] = (255,0,0)
img[xc-1, yc] = (255,0,0)
img[xc, yc+1] = (255,0,0)
img[xc, yc-1] = (255,0,0)
xk = 0
yk = ry
pk = ry**2 -rx**2 * ry+1/4* rx**2
while ry**2 * xk < rx**2 *yk:
if pk>0:
xk = xk+1
yk = yk-1
pk = pk + 2 * ry**2 *xk + ry**2 - 2* rx**2 *yk
else:
xk = xk+1
pk=pk + 2* ry**2 * xk + ry**2
img[xc+xk, yc+yk] = (0,0,0)
img[xc+xk, yc-yk] = (0,0,0)
img[xc-xk, yc+yk] = (0,0,0)
img[xc-xk, yc-yk] = (0,0,0)
quarterCircleOne.append([xc-xk, yc-yk])
quarterCircleTwoHalfTwo.append([xc-xk, yc+yk])
quarterCircleThree.append([xc+xk, yc+yk])
quarterCircleFourHalfOne.append([xc+xk, yc-yk])
pk = ry**2 * (xk-1/2)**2 + rx**2 * (yk-1)**2 - rx**2 * ry**2
while yk>0:
if pk>0:
yk = yk-1
pk=pk -2 * rx**2 * yk + rx**2
else:
xk = xk+1
yk = yk-1
pk=pk - 2 * rx**2 * yk + 2* ry**2 *xk + rx**2
img[xc+xk, yc+yk] = (0,0,0)
img[xc+xk, yc-yk] = (0,0,0)
img[xc-xk, yc+yk] = (0,0,0)
img[xc-xk, yc-yk] = (0,0,0)
quarterCircleOne.append([xc-xk, yc-yk])
quarterCircleTwoHalfOne.append([xc-xk, yc+yk])
quarterCircleThree.append([xc+xk, yc+yk])
quarterCircleFourHalfTwo.append([xc+xk, yc-yk])
GenerateElipse(45, 30, 25, 15)
def DrawElipse(speed, img):
sortedQuarterCircleOne = sorted(quarterCircleOne,key=lambda x: x[1])
sortedQuarterCircleTwoHalfOne =
sorted(quarterCircleTwoHalfOne,key=lambda x: x[1])
sortedQuarterCircleTwoHalfTwo =
sorted(quarterCircleTwoHalfTwo,key=lambda x: x[0])
sortedQuarterCircleThree = sorted(quarterCircleThree,key=lambda x: x[1],
reverse=True)
sortedQuarterCircleFourHalfTwo =
sorted(quarterCircleFourHalfTwo,key=lambda x: x[1], reverse=True)
sortedQuarterCircleFourHalfOne =
sorted(quarterCircleFourHalfOne,key=lambda x: x[0], reverse=True)
circle = [sortedQuarterCircleOne, sortedQuarterCircleTwoHalfOne,
sortedQuarterCircleTwoHalfTwo, sortedQuarterCircleThree,
sortedQuarterCircleFourHalfTwo, sortedQuarterCircleFourHalfOne]
i = 0
for x in range(0, 6):
for coordinates in circle[x]:
freshImage = copy.deepcopy(img)
freshImage[coordinates[0], coordinates[1]] = (0,0,0)
freshImage[coordinates[0], coordinates[1]+1] = (255,0,0)
freshImage[coordinates[0], coordinates[1]-1] = (255,0,0)
freshImage[coordinates[0]+1, coordinates[1]-1] = (0,0,255)
freshImage[coordinates[0]+1, coordinates[1]+1] = (0,0,255)
freshImage[coordinates[0]+1, coordinates[1]] = (255,0,0)
freshImage[coordinates[0]-1, coordinates[1]-1] = (0,0,255)
freshImage[coordinates[0]-1, coordinates[1]+1] = (0,0,255)
freshImage[coordinates[0]-1, coordinates[1]] = (255,0,0)
if i % speed == 0:
plt.imshow(freshImage)
camera.snap()
i = i + 1
DrawElipse(1, img);
animation = camera.animate()
plt.show()