2014-09-04 82 views
0

我在3D空間中有一系列點,當前繪圖。我想將它們連接在一起形成一個矩形。矩形將需要一定的寬度和高度,並且如果遇到框邊界,應該以某種方式'圍繞'到框的另一側。用線條/條連接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() 

沒有任何人有什麼想法?非常感謝

回答

0

一個可能的算法,是

  1. 找到矩形的頂點
  2. 重新排序頂點形成rectagle

這裏是一個可能的解決方案:

#!/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") 

結果:

enter image description here

我不知道重新排序實施將適用於所有情況。