球坐标的计算
Calculation of spherical coordinates
我不得不说我对基础数学知之甚少感到害怕和惊讶。基本上我所拥有的是原点 (0,0,0),我知道圆的半径 (10),并且我知道两个角度(theta 和 phi)。鉴于此假设,我想计算球体上的投影点。我通过阅读 , , http://tutorial.math.lamar.edu/Classes/CalcII/SphericalCoords.aspx and https://en.wikipedia.org/wiki/Spherical_coordinate_system 的答案得出了底部代码。
我当前的代码:
#!/usr/bin/env python3
import math
PI = math.pi
PI_2 = PI / 2
def calc_sphere_coordinates(radius, phi, theta):
# see:
# see:
# see: http://mathinsight.org/spherical_coordinates
# see: https://en.wikipedia.org/wiki/Spherical_coordinate_system
# see: http://tutorial.math.lamar.edu/Classes/CalcII/SphericalCoords.aspx
# φ phi is the polar angle, rotated down from the positive z-axis (slope)
# θ theta is azimuthal angle, the angle of the rotation around the z-axis (aspect)
# z
# | x
# | /
# |/
# +-------- y
# both angles need to be in radians, not degrees!
theta = theta * PI / 180
phi = phi * PI / 180
x = radius * math.sin(phi) * math.cos(theta)
y = radius * math.sin(phi) * math.sin(theta)
z = radius * math.cos(phi)
return (x, y, z)
if __name__ == "__main__":
# calculate point position in hemisphere by rotating down from positive z-axis
for i in (10, 20, 30, 40, 50, 60, 70 , 80, 90):
print(calc_sphere_coordinates(10, i, 0))
print("-"*10)
# calculate point position in hemisphere by rotating around the z axis
for i in (10, 20, 30, 40, 50, 60, 70 , 80, 90):
print(calc_sphere_coordinates(10, 0, i))
print("-"*10)
# calculate point position by rotating in both directions
for i in (10, 20, 30, 40, 50, 60, 70 , 80, 90):
print(calc_sphere_coordinates(10, i, 90-i))
代码输出结果如下:
(1.7364817766693033, 0.0, 9.84807753012208)
(3.420201433256687, 0.0, 9.396926207859085)
(4.999999999999999, 0.0, 8.660254037844387)
(6.4278760968653925, 0.0, 7.660444431189781)
(7.66044443118978, 0.0, 6.427876096865393)
(8.660254037844386, 0.0, 5.000000000000001)
(9.396926207859083, 0.0, 3.4202014332566884)
(9.84807753012208, 0.0, 1.7364817766693041)
(10.0, 0.0, 6.123233995736766e-16)
----------
(0.0, 0.0, 10.0)
(0.0, 0.0, 10.0)
(0.0, 0.0, 10.0)
(0.0, 0.0, 10.0)
(0.0, 0.0, 10.0)
(0.0, 0.0, 10.0)
(0.0, 0.0, 10.0)
(0.0, 0.0, 10.0)
(0.0, 0.0, 10.0)
----------
(0.30153689607045814, 1.7101007166283433, 9.84807753012208)
(1.16977778440511, 3.2139380484326963, 9.396926207859085)
(2.5, 4.330127018922192, 8.660254037844387)
(4.131759111665348, 4.92403876506104, 7.660444431189781)
(5.868240888334652, 4.92403876506104, 6.427876096865393)
(7.5, 4.330127018922192, 5.000000000000001)
(8.83022221559489, 3.2139380484326963, 3.4202014332566884)
(9.69846310392954, 1.7101007166283433, 1.7364817766693041)
(10.0, 0.0, 6.123233995736766e-16)
带 (10.0, 0.0, 6.123233995736766e-16)
的线的 z 坐标不应该是 0
而不是 6.123233995736766e-16
吗?无论使用什么角度 (0.0, 0.0, 10.0)
,围绕 z 轴旋转也总是得到相同的结果。
据我所知,您的代码运行良好。老实说,必须承认 6.123233995736766e-16 对于所有实际应用程序来说几乎都是 0,对吧?
你的问题归结为
为什么 math.cos(math.pi / 2.0)
不等于零
原因在于浮点数是如何生成并存储在计算机中的。尝试在 python 中计算 0.1+0.2
。吃惊?!如果您想了解更多,只需 google 浮点错误 或任何相关内容。
我不得不说我对基础数学知之甚少感到害怕和惊讶。基本上我所拥有的是原点 (0,0,0),我知道圆的半径 (10),并且我知道两个角度(theta 和 phi)。鉴于此假设,我想计算球体上的投影点。我通过阅读 ,
我当前的代码:
#!/usr/bin/env python3
import math
PI = math.pi
PI_2 = PI / 2
def calc_sphere_coordinates(radius, phi, theta):
# see:
# see:
# see: http://mathinsight.org/spherical_coordinates
# see: https://en.wikipedia.org/wiki/Spherical_coordinate_system
# see: http://tutorial.math.lamar.edu/Classes/CalcII/SphericalCoords.aspx
# φ phi is the polar angle, rotated down from the positive z-axis (slope)
# θ theta is azimuthal angle, the angle of the rotation around the z-axis (aspect)
# z
# | x
# | /
# |/
# +-------- y
# both angles need to be in radians, not degrees!
theta = theta * PI / 180
phi = phi * PI / 180
x = radius * math.sin(phi) * math.cos(theta)
y = radius * math.sin(phi) * math.sin(theta)
z = radius * math.cos(phi)
return (x, y, z)
if __name__ == "__main__":
# calculate point position in hemisphere by rotating down from positive z-axis
for i in (10, 20, 30, 40, 50, 60, 70 , 80, 90):
print(calc_sphere_coordinates(10, i, 0))
print("-"*10)
# calculate point position in hemisphere by rotating around the z axis
for i in (10, 20, 30, 40, 50, 60, 70 , 80, 90):
print(calc_sphere_coordinates(10, 0, i))
print("-"*10)
# calculate point position by rotating in both directions
for i in (10, 20, 30, 40, 50, 60, 70 , 80, 90):
print(calc_sphere_coordinates(10, i, 90-i))
代码输出结果如下:
(1.7364817766693033, 0.0, 9.84807753012208)
(3.420201433256687, 0.0, 9.396926207859085)
(4.999999999999999, 0.0, 8.660254037844387)
(6.4278760968653925, 0.0, 7.660444431189781)
(7.66044443118978, 0.0, 6.427876096865393)
(8.660254037844386, 0.0, 5.000000000000001)
(9.396926207859083, 0.0, 3.4202014332566884)
(9.84807753012208, 0.0, 1.7364817766693041)
(10.0, 0.0, 6.123233995736766e-16)
----------
(0.0, 0.0, 10.0)
(0.0, 0.0, 10.0)
(0.0, 0.0, 10.0)
(0.0, 0.0, 10.0)
(0.0, 0.0, 10.0)
(0.0, 0.0, 10.0)
(0.0, 0.0, 10.0)
(0.0, 0.0, 10.0)
(0.0, 0.0, 10.0)
----------
(0.30153689607045814, 1.7101007166283433, 9.84807753012208)
(1.16977778440511, 3.2139380484326963, 9.396926207859085)
(2.5, 4.330127018922192, 8.660254037844387)
(4.131759111665348, 4.92403876506104, 7.660444431189781)
(5.868240888334652, 4.92403876506104, 6.427876096865393)
(7.5, 4.330127018922192, 5.000000000000001)
(8.83022221559489, 3.2139380484326963, 3.4202014332566884)
(9.69846310392954, 1.7101007166283433, 1.7364817766693041)
(10.0, 0.0, 6.123233995736766e-16)
带 (10.0, 0.0, 6.123233995736766e-16)
的线的 z 坐标不应该是 0
而不是 6.123233995736766e-16
吗?无论使用什么角度 (0.0, 0.0, 10.0)
,围绕 z 轴旋转也总是得到相同的结果。
据我所知,您的代码运行良好。老实说,必须承认 6.123233995736766e-16 对于所有实际应用程序来说几乎都是 0,对吧?
你的问题归结为
为什么 math.cos(math.pi / 2.0)
不等于零
原因在于浮点数是如何生成并存储在计算机中的。尝试在 python 中计算 0.1+0.2
。吃惊?!如果您想了解更多,只需 google 浮点错误 或任何相关内容。