<p>啊!上面所有代码中仍有错误。。这是一个顶级的谷歌结果。。
TLDR编号:
我已经用VPython测试过了,用atan2表示theta(elev)是错误的,用
阿科斯!对phi(azim)来说是正确的。
我推荐sympy1.0acos函数(它甚至不抱怨r=0的acos(z/r))。</p>
<p><a href="http://mathworld.wolfram.com/SphericalCoordinates.html" rel="noreferrer">http://mathworld.wolfram.com/SphericalCoordinates.html</a></p>
<p>如果我们把它转换成物理系统(r,θ,phi)=(r,elev,方位角),我们得到:</p>
<pre><code>r = sqrt(x*x + y*y + z*z)
phi = atan2(y,x)
theta = acos(z,r)
</code></pre>
<p>非优化但<strong>正确的</strong>右手物理系统代码:</p>
<pre><code>from sympy import *
def asCartesian(rthetaphi):
#takes list rthetaphi (single coord)
r = rthetaphi[0]
theta = rthetaphi[1]* pi/180 # to radian
phi = rthetaphi[2]* pi/180
x = r * sin( theta ) * cos( phi )
y = r * sin( theta ) * sin( phi )
z = r * cos( theta )
return [x,y,z]
def asSpherical(xyz):
#takes list xyz (single coord)
x = xyz[0]
y = xyz[1]
z = xyz[2]
r = sqrt(x*x + y*y + z*z)
theta = acos(z/r)*180/ pi #to degrees
phi = atan2(y,x)*180/ pi
return [r,theta,phi]
</code></pre>
<p>您可以使用以下函数自己测试它:</p>
<pre><code>test = asCartesian(asSpherical([-2.13091326,-0.0058279,0.83697319]))
</code></pre>
<p>一些象限的其他测试数据:</p>
<pre><code>[[ 0. 0. 0. ]
[-2.13091326 -0.0058279 0.83697319]
[ 1.82172775 1.15959835 1.09232283]
[ 1.47554111 -0.14483833 -1.80804324]
[-1.13940573 -1.45129967 -1.30132008]
[ 0.33530045 -1.47780466 1.6384716 ]
[-0.51094007 1.80408573 -2.12652707]]
</code></pre>
<p>我还使用了VPython来方便地可视化矢量:</p>
<pre><code>test = v.arrow(pos = (0,0,0), axis = vis_ori_ALA , shaftwidth=0.05, color=v.color.red)
</code></pre>