圆柱无法在Python中旋转

时间:2019-07-16 04:25:43

标签: python 3d pythonplotter

我试图绘制一个旋转的圆柱体,其Z轴旋转pi / 4弧度。我在给定here的代码之上构建。我尝试绘制旋转圆柱体的方法如下,首先,我在非旋转圆柱体的表面上生成网格点。然后,我将这些点集与旋转矩阵相乘,以获得旋转圆柱体的点。但是绘制此图形后,我仍然得到了非旋转圆柱体。我附上我在下面尝试过的代码。任何帮助表示赞赏。

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D


def data_for_cylinder_along_z(center_x, center_y, radius, height_z):
    z = np.linspace(0, height_z, 20)
    theta = np.linspace(0, 2*np.pi, 20)
    theta_grid, z_grid = np.meshgrid(theta, z)
    x_grid = radius*np.cos(theta_grid) + center_x
    y_grid = radius*np.sin(theta_grid) + center_y
    return x_grid, y_grid, z_grid


rot_mat = np.array([[np.cos(np.pi/3), 0, -np.sin(np.pi/3)], [0, 1, 0], [np.sin(np.pi/3), 0, np.cos(np.pi/3)]])

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

Xc, Yc, Zc = data_for_cylinder_along_z(0.0, 0.0, 0.05, 0.1)

# Create points for rotated cylinder
new_Xc = Xc.reshape((1, 20*20))
new_Yc = Yc.reshape((1, 20*20))
new_Zc = Zc.reshape((1, 20*20))
all_new = np.vstack((new_Xc, new_Yc, new_Zc))
all_rot = np.dot(rot_mat, all_new)
# print(all_new)
# print(all_rot)
rot_Xc = all_rot[0, :].reshape(20, 20)
rot_Yc = all_rot[1, :].reshape(20, 20)
rot_Zc = all_rot[2, :].reshape(20, 20)

print(Xc)
print(rot_Xc)

ax.plot_surface(rot_Xc, rot_Yc, rot_Zc, alpha=0.5)
plt.show()

0 个答案:

没有答案