如何在 Python 中绘制 3D 截头金字塔(使用用户输入)?

问题描述 投票:0回答:1

我对Python比较陌生,一直在尝试创建一个小程序,根据用户输入的较大和较小的底面以及高度来绘制截断的金字塔。但每次我绘制绘图时,较小的底座都不居中。

如果有人能指出我的代码中的错误,我将非常感激。

import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection, Line3DCollection
import matplotlib.pyplot as plt
import math

G = 4
g = 2
h = 2

a = math.sqrt(math.pow(h,2)+math.pow((((G/2)-(g/2))),2))
Z = np.array([[-1, -1, -1],
                  [(-1+G), -1, -1 ],
                  [(-1+G), (-1+G), -1],
                  [-1, (-1+G), -1],
                  [(-1+(a/3)), (-1+(a/3)), (-1+h)],
                  [(-1+(a/3)+g), (-1+(a/3)), (-1+h)],
                  [(-1+(a/3)+g), (-1+(a/3)+g), (-1+h)],
                  [(-1+(a/3)), (-1+(a/3)+g), (-1+h)]])

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

r = [-1,1]

X, Y = np.meshgrid(r, r)
ax.scatter3D(Z[:, 0], Z[:, 1], Z[:, 2])

faces = [[Z[0],Z[1],Z[2],Z[3]],
 [Z[4],Z[5],Z[6],Z[7]],
 [Z[0],Z[1],Z[5],Z[4]],
 [Z[2],Z[3],Z[7],Z[6]],
 [Z[1],Z[2],Z[6],Z[5]],
 [Z[4],Z[7],Z[3],Z[0]],
 [Z[2],Z[3],Z[7],Z[6]]]

ax.add_collection3d(Poly3DCollection(faces,
 facecolors='cyan', linewidths=1, edgecolors='r', alpha=.25))

plt.show()
python
1个回答
0
投票

如果你能从正面观察金字塔,它实际上可能位于中心位置。

© www.soinside.com 2019 - 2024. All rights reserved.