我在3D立方體內有一組點。我想根據觀察者的位置在2D中獲得這些點的視覺投影。到目前爲止,我一直試圖在3D中繪製我的點,並設置高程和方位角,以便我可以在一側觀察立方體。在Matplotlib中設置查看位置3d散點圖
這是一個簡單的例子,但我需要能夠概括我的代碼觀察者的任何(X,Y,Z)的位置。這是我迄今爲止所嘗試的:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
coord = np.random.uniform(2.,4., (10000,3)) # cube points
los = np.array([10.,3.,3]) #observer position (looking at side of cube)
center = np.array([3.,3.,3.]) #cube center
def elev(los, target):
diff = target - los
cosel = np.sum(los*diff)/np.sqrt(np.sum(los**2.) * np.sum(diff**2.))
el = np.degrees(np.arccos(cosel))
return el
def azi(los, target):
diff = target - los
cosazi = (-los[2]*los[0]*diff[0] - los[2]*los[1]*diff[1] + \\
(los[0]**2.+los[1]**2)*diff[2])/np.sqrt((los[0]**2.+los[1]**2.)* \\
(los[0]**2.+los[1]**2.+los[2]**2)*(diff[0]**2.+diff[1]**2.+diff[2]**2))
sinazi = (-los[2]*diff[0] + los[0]*diff[1])/\\
np.sqrt((los[0]**2.+los[1]**2.)*(diff[0]**2.+diff[1]**2.+diff[2]**2.))
tanazi = sinazi/cosazi
azi = np.degrees(np.arctan(tanazi))
return azi
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(coord[:,0], coord[:,1], coord[:,2], edgecolor = "none", alpha = 0.3)
ax.view_init(elev=elev(los, center), azim=azi(los,center))
plt.show()
我應該看到一個完美的廣場,但我的觀點是一個角度。出了什麼問題?
我已經借了仰角和方位角公式從這裏:https://gis.stackexchange.com/questions/58923/calculate-view-angle