A community-maintained Python framework for creating mathematical animations.
Use math convention in polar coordinates (#2095)
icedcoffeeeebb92fee9c92a
diff --git a/manim/utils/space_ops.py b/manim/utils/space_ops.pyindex a14e16a0d..f9bc29c78 100644--- a/manim/utils/space_ops.py+++ b/manim/utils/space_ops.py@@ -27,6 +27,8 @@ __all__ = ["get_winding_number","cross2d","earclip_triangulation",+ "cartesian_to_spherical",+ "spherical_to_cartesian","perpendicular_bisector",]@@ -752,22 +754,45 @@ def earclip_triangulation(verts: np.ndarray, ring_ends: list) -> list:return [indices[mi] for mi in meta_indices]-def cartesian_to_spherical(vec):+def cartesian_to_spherical(vec: Sequence[float]) -> np.ndarray:+ """Returns an array of numbers corresponding to each+ polar coordinate value (distance, phi, theta).++ Parameters+ ----------+ vec+ A numpy array ``[x, y, z]``.+ """norm = np.linalg.norm(vec)if norm == 0:return 0, 0, 0r = norm- theta = np.arccos(vec[2] / r)- phi = np.arctan2(vec[1], vec[0])- return r, theta, phi+ phi = np.arccos(vec[2] / r)+ theta = np.arctan2(vec[1], vec[0])+ return np.array([r, phi, theta])++def spherical_to_cartesian(spherical: Sequence[float]) -> np.ndarray:+ """Returns a numpy array ``[x, y, z]`` based on the spherical+ coordinates given.-def spherical_to_cartesian(r, theta, phi):+ Parameters+ ----------+ spherical+ A list of three floats that correspond to the following:++ r - The distance between the point and the origin.++ theta - The azimuthal angle of the point to the positive x-axis.++ phi - The vertical angle of the point to the positive z-axis.+ """+ r, theta, phi = sphericalreturn np.array([- r * np.cos(phi) * np.sin(theta),- r * np.sin(phi) * np.sin(theta),- r * np.cos(theta),+ r * np.cos(theta) * np.sin(phi),+ r * np.sin(theta) * np.sin(phi),+ r * np.cos(phi),],)diff --git a/tests/test_space_ops.py b/tests/test_space_ops.pynew file mode 100644index 000000000..42d34b641--- /dev/null+++ b/tests/test_space_ops.py@@ -0,0 +1,13 @@+import numpy as np++from manim import cartesian_to_spherical, spherical_to_cartesian+++def test_polar_coords():+ a = np.array([1, 1, 0])+ b = (2, np.pi / 2, np.pi / 2)+ assert all(+ np.round(cartesian_to_spherical(a), 4)+ == np.round([2 ** 0.5, np.pi / 2, np.pi / 4], 4),+ )+ assert all(np.round(spherical_to_cartesian(b), 4) == np.array([0, 2, 0]))