在 python 中绘制 3D 平截头体
Plotting a 3D frustrum in python
我正在尝试在 python 程序中绘制 3D 金字塔平截头体(曲面图),其中平截头体几何形状由下面的平面图像给出:
所有变量 B、L、alpha、beta... 都将在我的程序中给出(请注意,alpha 不等于常规平截头体中的 beta)。任何人都可以帮助我提供有关如何进行此操作的任何建议吗?提前致谢!
是这样的吗?
import numpy as np
import matplotlib.pyplot as plt
def get_vertices(L, B, I, alpha, beta):
P = np.empty((8,3), dtype=float)
Ind = np.array([4,5,6,7])
#Ind = np.array([0,2,4,6])
P[Ind, 0] = L/2 + I*np.tan(np.pi*alpha/180)
P[Ind, 1] = B/2 + I*np.tan(np.pi*beta/180)
P[Ind, 2] = I
P[5, 0] = - P[5, 0]
P[7, 1] = - P[7, 1]
P[6, [0,1]] = - P[6, [0,1]]
Ind = Ind - 4
P[Ind, 0] = L/2
P[Ind, 1] = B/2
P[Ind, 2] = 0
P[1, 0] = - P[1, 0]
P[3, 1] = - P[3, 1]
P[2, [0,1]] = - P[2, [0,1]]
return P
def draw(P):
l_x = -7
r_x = 7
l_y = -7
r_y = 7
l_z = -0.5
r_z = 7
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.set_xlim((l_x, r_x))
ax.set_ylim((l_y, r_y))
ax.set_zlim((l_z, r_z))
Ind = np.array([[0,1], [1,2], [2,3], [3,0]])
for i in Ind:
ax.plot(P[i, 0], P[i, 1], P[i, 2], 'r-')
Ind = np.array([[4,5], [5,6], [6,7], [7,4]])
for i in Ind:
ax.plot(P[i, 0], P[i, 1], P[i, 2], 'r-')
Ind = np.array([[0,4], [1,5], [2,6], [3,7]])
for i in Ind:
ax.plot(P[i, 0], P[i, 1], P[i, 2], 'r-')
ax.plot(P[:,0], P[:,1], P[:,2], 'bo')
plt.show()
return None
L = 8
B = 5
I = 4
alpha = 15
beta = 30
ful = get_vertices(L, B, I, alpha, beta)
draw(ful)
我正在尝试在 python 程序中绘制 3D 金字塔平截头体(曲面图),其中平截头体几何形状由下面的平面图像给出:
所有变量 B、L、alpha、beta... 都将在我的程序中给出(请注意,alpha 不等于常规平截头体中的 beta)。任何人都可以帮助我提供有关如何进行此操作的任何建议吗?提前致谢!
是这样的吗?
import numpy as np
import matplotlib.pyplot as plt
def get_vertices(L, B, I, alpha, beta):
P = np.empty((8,3), dtype=float)
Ind = np.array([4,5,6,7])
#Ind = np.array([0,2,4,6])
P[Ind, 0] = L/2 + I*np.tan(np.pi*alpha/180)
P[Ind, 1] = B/2 + I*np.tan(np.pi*beta/180)
P[Ind, 2] = I
P[5, 0] = - P[5, 0]
P[7, 1] = - P[7, 1]
P[6, [0,1]] = - P[6, [0,1]]
Ind = Ind - 4
P[Ind, 0] = L/2
P[Ind, 1] = B/2
P[Ind, 2] = 0
P[1, 0] = - P[1, 0]
P[3, 1] = - P[3, 1]
P[2, [0,1]] = - P[2, [0,1]]
return P
def draw(P):
l_x = -7
r_x = 7
l_y = -7
r_y = 7
l_z = -0.5
r_z = 7
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.set_xlim((l_x, r_x))
ax.set_ylim((l_y, r_y))
ax.set_zlim((l_z, r_z))
Ind = np.array([[0,1], [1,2], [2,3], [3,0]])
for i in Ind:
ax.plot(P[i, 0], P[i, 1], P[i, 2], 'r-')
Ind = np.array([[4,5], [5,6], [6,7], [7,4]])
for i in Ind:
ax.plot(P[i, 0], P[i, 1], P[i, 2], 'r-')
Ind = np.array([[0,4], [1,5], [2,6], [3,7]])
for i in Ind:
ax.plot(P[i, 0], P[i, 1], P[i, 2], 'r-')
ax.plot(P[:,0], P[:,1], P[:,2], 'bo')
plt.show()
return None
L = 8
B = 5
I = 4
alpha = 15
beta = 30
ful = get_vertices(L, B, I, alpha, beta)
draw(ful)