我有一个任务,我需要把三维立方体投影到二维笛卡尔平面上,我已经绘制了顶点,但仍需要以某种方式对其进行动画制作。在
我尝试过使用FuncAnimation(),但不知道它是如何工作的。我对python还不熟悉,所以别对我客气,谢谢。在import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
A = np.array([-0.5,-0.5,-0.5])
B = np.array([0.5,-0.5,-0.5])
C = np.array([0.5,0.5,-0.5])
D = np.array([-0.5,0.5,-0.5])
E = np.array([-0.5,-0.5,0.5])
F = np.array([0.5,-0.5,0.5])
G = np.array([0.5,0.5,0.5])
H = np.array([-0.5,0.5,0.5])
load = np.array([A,B,C,D,E,F,G,H])
print(load)
fig = plt.figure()
ax = plt.axes(xlim =(-1,1),ylim =(-1,1))
# Declared to allow for x and y axis only
projection = np.array([ [1,0,0], [0,1,0] ])
xdata,ydata = [],[]
plt.title("Render 3D Cube in 2D Space")
for x in load:
for angle in range(360):
rotationY = np.array([ [np.cos(angle),0,np.sin(angle)],
[0,1,0],
[-np.sin(angle),0,np.cos(angle)] ])
rotationX = np.array([ [1,0,0],
[0,np.cos(angle),-np.sin(angle)],
[0,np.sin(angle),np.cos(angle)] ])
# Drawing each points
rotated = np.dot(rotationY,x)
rotated = np.dot(rotationX,rotated)
projected2d = np.dot(projection,rotated)
#projected2d = np.dot(projection,x) -With no rotations
line = ax.plot(projected2d[0],projected2d[1],c = "blue",marker = "o")
def animate(i):
x0,y0 = i
xdata.append(x0)
ydata.append(y0)
line.set_data(xdata,ydata)
return line
anim = FuncAnimation(fig,animate,interval =200,frames = 30)
plt.grid()
#plt.draw()
plt.show()