def xyz_to_spr(x, y, z):
"""Convierte las coordenadas cartesianas (x,y,z) a las coordenadas esfericas (r,phi,theta) con angulos en grados"""
# Calculo el radio
r = math.sqrt(x ** 2 + y ** 2 + z ** 2)
# Calculo el angulo theta
if z > 0:
theta = math.atan(math.sqrt(x ** 2 + y ** 2) / z)
elif z == 0:
theta = math.pi / 2
else:
theta = math.pi + math.atan(math.sqrt(x ** 2 + y ** 2) / z)
# Calculo el angulo phi
if x > 0:
if y > 0:
phi = math.atan(y / x)
else:
phi = 2 * math.pi + math.atan(y / x)
elif x == 0:
phi = sgn(y) * math.pi / 2
else:
phi = math.pi + math.atan(y / x)
theta = math.degrees(theta)
phi = math.degrees(phi) % 360
theta = min(max(theta, 0.000001), 180)
return r, phi, theta
评论列表
文章目录