gpt4 book ai didi

python - 如何绘制立方体的面?

转载 作者:太空宇宙 更新时间:2023-11-03 18:48:50 24 4
gpt4 key购买 nike

我已经做了一个可以在 python 上旋转的立方体,但现在我想为面着色,以便在旋转时识别每个面。代码如下:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from itertools import product, combinations
from numpy import sin, cos

fig = plt.figure()
ax = fig.gca(projection='3d')
ax.set_aspect("auto")
ax.set_autoscale_on(True)


#dibujar cubo
r = [-10, 10]
for s, e in combinations(np.array(list(product(r,r,r))), 2):
if np.sum(np.abs(s-e)) == r[1]-r[0]:
ax.plot3D(*zip(s,e), color="b")


#dibujar punto
#ax.scatter([0],[0],[0],color="g",s=100)

d = [-2, 2]
theta = np.radians(45)
for s, e in combinations(np.array(list(product(d,d,d))), 2):
if np.sum(np.abs(s-e)) == d[1]-d[0]:
s_rotated = [s[0]*cos(theta)-s[1]*sin(theta),
s[0]*sin(theta)+s[1]*cos(theta),
s[2]]
e_rotated = [e[0]*cos(theta)-e[1]*sin(theta),
e[0]*sin(theta)+e[1]*cos(theta),
e[2]]
ax.plot3D(*zip(s_rotated,e_rotated), color="g")
plt.show()

所以我想绘制里面的立方体。有什么帮助吗?谢谢!

最佳答案

您可以使用补丁。

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from itertools import product, combinations
from numpy import sin, cos
from matplotlib.patches import Rectangle, Circle, PathPatch
import mpl_toolkits.mplot3d.art3d as art3d

fig = plt.figure()
ax = fig.gca(projection='3d')
ax.set_aspect("auto")
ax.set_autoscale_on(True)


r = [-10, 10]
for s, e in combinations(np.array(list(product(r,r,r))), 2):
if np.sum(np.abs(s-e)) == r[1]-r[0]:
ax.plot3D(*zip(s,e), color="b")

colors = ['b', 'g', 'r', 'c', 'm', 'y']
for i, (z, zdir) in enumerate(product([-2,2], ['x','y','z'])):
side = Rectangle((-2, -2), 4, 4, facecolor=colors[i])
ax.add_patch(side)
art3d.pathpatch_2d_to_3d(side, z=z, zdir=zdir)


plt.show()

enter image description here

如果您需要比 x-y 平面更广泛的旋转,则可以使用 Poly3Dcollection。这只是绘制立方体的顶部和底部。您想要如何生成顶点将取决于您正在执行的操作的详细信息。

from mpl_toolkits.mplot3d import Axes3D, art3d
import matplotlib.pyplot as plt
import numpy as np
from itertools import product, combinations
from numpy import sin, cos


fig = plt.figure()
ax = fig.gca(projection='3d')
ax.set_aspect("auto")
ax.set_autoscale_on(True)


r = [-10, 10]
for s, e in combinations(np.array(list(product(r,r,r))), 2):
if np.sum(np.abs(s-e)) == r[1]-r[0]:
ax.plot3D(*zip(s,e), color="b")


btm = np.array([[-2, -2, -2],
[-2, 2, -2],
[ 2, 2, -2],
[2, -2,-2]])
top = np.array([[-2, -2, 2],
[-2, 2, 2],
[ 2, 2, 2],
[2, -2,2]])
theta = np.radians(45)
rot_mx = np.array([[cos(theta), sin(theta), 0],
[-sin(theta), cos(theta), 0],
[ 0, 0, 1]])

btm = np.dot(btm, rot_mx)
side = art3d.Poly3DCollection([btm])
side.set_color('r')
ax.add_collection3d(side)

top = np.dot(top, rot_mx)
side = art3d.Poly3DCollection([top])
side.set_color('g')
ax.add_collection3d(side)


plt.show()

enter image description here

关于python - 如何绘制立方体的面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18853563/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com