我在 3D 空间中绘制了点,四个多边形以与 y 轴和 x 轴成角度的方式穿过数据立方体。
我想,对于框中的每个黑色数据点,计算出以数据点为中心的正方形顶点的坐标。使用这些坐标,我可以绘制一个 polgyon,就像我现在正在做的那样。这个正方形的尺寸需要与第 7 行定义的宽度值相同。绘制的正方形必须倾斜,以便它正好位于现有的绘制平面上。
有人知道解决这个问题的最佳方法吗?另一个困难的事情是,如果正方形离开盒子,它应该环绕盒子的另一边。盒子可以与相同的盒子水平和垂直堆叠,无限平铺。
我的代码可以在下面找到(抱歉很乱):
import matplotlib.pyplot as pyplot
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
angle = np.arctan2(1,4)
xangle = np.arctan2(1,4)
width = 100/np.cos(angle)
print 'angle:', angle*(180/np.pi)
print 'xangle:', xangle*(180/np.pi)
print 'width:', width
x1 = [0, 0, 0, 0, 50, 50, 50, 50]
y1 = [50,50,50,50,50,50,50,50]
z1 = [12.5,37.5,62.5,87.5,25,50,75,0]
x2 = [0,0,0,0,0,0,0,0,50,50,50,50,50,50,50,50]
y2 = [0,50,0,50,0,50,0,50,0,50,0,50,0,50,0,50]
z2 = [0,12.5,25,37.5,50,62.5,75,87.5,12.5,25,37.5,50,62.5,75,87.5,0]
fig = pyplot.figure()
ax = fig.add_subplot(111, projection = '3d')
ax.set_xlim(0,100)
ax.set_ylim(0,100)
ax.set_zlim(0,100)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
#ax.view_init(elev=90, azim=90)
#ax.scatter(x1, y1, z1, zdir='z', s=20, c='g')
ax.scatter(x2, y2, z2, zdir='z', s=20, c='r') #THESE ARE RICHARD'S COORDINATES notice how they do not lie on the plane
xa = [0,100,100,0]
ya = [0,0,100,100]
za = [0,-6.25,18.75,25]
verts = [zip(xa,ya,za)]
ax.add_collection3d(Poly3DCollection(verts))
xb = [0,100,100,0]
yb = [0,0,100,100]
zb = [25,-6.25+25,18.75+25,50]
verts = [zip(xb,yb,zb)]
ax.add_collection3d(Poly3DCollection(verts))
xc = [0,100,100,0]
yc = [0,0,100,100]
zc = [50,-6.25+25*2,18.75+25*2,75]
verts = [zip(xc,yc,zc)]
ax.add_collection3d(Poly3DCollection(verts))
xd = [0,100,100,0]
yd = [0,0,100,100]
zd = [75,-6.25+25*3,18.75+25*3,100]
verts = [zip(xd,yd,zd)]
ax.add_collection3d(Poly3DCollection(verts))
#pyplot.show()
x = [0]
y = [0]
z = [0]
for i in range(1,100):
new_x = x[(len(x)-1)] + 50
new_y = y[(len(y)-1)] + 12.5
new_z = z[(len(z)-1)]
if new_x >= 100:
new_x = new_x - 100
new_z = new_z + 6.25
if new_y >= 100:
new_y = new_y - 100
if new_z >= 100:
new_z = new_z - 100
if new_x == 0 and new_y == 0 and new_z == 0:
print 'done!', i
x.append(new_x)
y.append(new_y)
z.append(new_z)
ax.scatter(x, y, z, zdir='z', s=20, c='k', zorder=1)
pyplot.show()