用线/杆连接3D点
我现在有一系列在三维空间中的点,这些点已经被标出来了。我想把这些点连接起来,形成一个矩形。这个矩形需要有一定的宽度和高度,并且如果它碰到盒子的边界,应该能够“绕过”到盒子的另一边。
我的代码如下:
import matplotlib.pyplot as pyplot
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
x = [0, 0, 0, 0, 50, 50, 50, 50]
y = [50,50,50,50,50,50,50,50]
z = [12.5,37.5,62.5,87.5,25,50,75,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(x, y, z, zdir='z', s=20, c='g')
pyplot.show()
有没有人有什么好主意?非常感谢!
1 个回答
0
一个可能的算法是:
- 找到矩形的顶点
- 重新排列这些顶点,形成一个矩形
这里有一个可能的解决方案:
#!/usr/bin/env python3
import matplotlib.pyplot as pyplot
from numpy import *
from numpy import linalg
from mpl_toolkits.mplot3d import Axes3D
x = array([0, 0, 0, 0, 50, 50, 50, 50])
y = array([50,50,50,50,50,50,50,50])
z = array([12.5,37.5,62.5,87.5,25,50,75,0])
data = concatenate((x[:,newaxis],y[:,newaxis],z[:,newaxis]), axis=1)
center = data.mean(axis=0)
distances = empty((0))
for row in data:
distances = append(distances, linalg.norm(row - center))
vertices = distances.argsort()[-4:]
Vertices_reorder = [vertices[0], vertices[2], vertices[1], vertices[3], vertices[0]]
# plot:
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(x, y, z, zdir='z', s=20, c='g')
ax.plot(x[Vertices_reorder], y[Vertices_reorder], z[Vertices_reorder])
fig.savefig("rect.png")
结果:
我不确定重新排列的实现是否适用于所有情况。