Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

439
Views
Convertir coordenadas esféricas en cartesianas y luego volver a convertirlas en cartesianas no da el resultado deseado

Estoy tratando de escribir dos funciones para convertir coordenadas cartesianas en coordenadas esféricas y viceversa. Aquí están las ecuaciones que he usado para las conversiones (también se pueden encontrar en esta página de Wikipedia ):

ingrese la descripción de la imagen aquí

Y

ingrese la descripción de la imagen aquí

Aquí está mi función spherical_to_cartesian :

 def spherical_to_cartesian(theta, phi): x = math.cos(phi) * math.sin(theta) y = math.sin(phi) * math.sin(theta) z = math.cos(theta) return x, y, z

Aquí está mi función cartesian_to_spherical :

 def cartesian_to_spherical(x, y, z): theta = math.atan2(math.sqrt(x ** 2 + y ** 2), z) phi = math.atan2(y, x) if x >= 0 else math.atan2(y, x) + math.pi return theta, phi

Y, aquí está el código del controlador:

 >>> t, p = 27.500, 7.500 >>> x, y, z = spherical_to_cartesian(t, p) >>> print(f"Cartesian coordinates:\tx={x}\ty={y}\tz={z}") Cartesian coordinates: x=0.24238129061573832 y=0.6558871334524494 z=-0.7148869687796651 >>> theta, phi = cartesian_to_spherical(x, y, z) >>> print(f"Spherical coordinates:\ttheta={theta}\tphi={phi}") Spherical coordinates: theta=2.367258771281654 phi=1.2168146928204135

No puedo entender por qué obtengo valores diferentes para theta y phi que mis valores iniciales (los valores de salida ni siquiera están cerca de los valores de entrada). ¿Cometí un error en mi código que no puedo ver?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Parece que estás dando tus ángulos en grados, mientras que todas las funciones trigonométricas esperan radianes. Multiplique grados con math.pi/180 para obtener radianes y multiplique radianes con 180/math.pi para obtener grados.

over 4 years ago · Santiago Trujillo Report

0

Los resultados son correctos, pero debe verificar su valor utilizando la operación módulo pi.

Las funciones trigonométricas en el paquete math esperan los ángulos de entrada en radianes. Esto significa que sus ángulos son mayores que 2*pi y son equivalentes a cualquier otro valor obtenido al sumar o restar 2*pi (que también representa una rotación completa en radianes).

En concreto tienes que:

 >>> 27.5 % (2*math.pi) 2.367258771281655 >>> 7.500 % (2*math.pi) 1.2168146928204138
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!