Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Added :meth`.PolarPlane.get_polar_graph` (#2157)

icedcoffeeeea6d8886b9112
diff --git a/manim/mobject/coordinate_systems.py b/manim/mobject/coordinate_systems.py
index be7cd555e..a2ef229bc 100644
--- a/manim/mobject/coordinate_systems.py
+++ b/manim/mobject/coordinate_systems.py
@@ -153,6 +153,54 @@ class CoordinateSystem:
def point_to_coords(self, point):
raise NotImplementedError()
+ def polar_to_point(self, radius: float, azimuth: float) -> np.ndarray:
+ r"""Gets a point from polar coordinates.
+
+ Parameters
+ ----------
+ radius
+ The coordinate radius (:math:`r`).
+
+ azimuth
+ The coordinate azimuth (:math:`\theta`).
+
+ Returns
+ -------
+ numpy.ndarray
+ The point.
+
+ Examples
+ --------
+
+ .. manim:: PolarToPointExample
+ :ref_classes: PolarPlane Vector
+ :save_last_frame:
+
+ class PolarToPointExample(Scene):
+ def construct(self):
+ polarplane_pi = PolarPlane(azimuth_units="PI radians", size=6)
+ polartopoint_vector = Vector(polarplane_pi.polar_to_point(3, PI/4))
+ self.add(polarplane_pi)
+ self.add(polartopoint_vector)
+ """
+ return self.coords_to_point(radius * np.cos(azimuth), radius * np.sin(azimuth))
+
+ def point_to_polar(self, point: np.ndarray) -> Tuple[float, float]:
+ r"""Gets polar coordinates from a point.
+
+ Parameters
+ ----------
+ point
+ The point.
+
+ Returns
+ -------
+ Tuple[:class:`float`, :class:`float`]
+ The coordinate radius (:math:`r`) and the coordinate azimuth (:math:`\theta`).
+ """
+ x, y = self.point_to_coords(point)
+ return np.sqrt(x ** 2 + y ** 2), np.arctan2(y, x)
+
def c2p(self, *coords):
"""Abbreviation for coords_to_point"""
return self.coords_to_point(*coords)
@@ -161,6 +209,14 @@ class CoordinateSystem:
"""Abbreviation for point_to_coords"""
return self.point_to_coords(point)
+ def pr2pt(self, radius: float, azimuth: float) -> np.ndarray:
+ """Abbreviation for :meth:`polar_to_point`"""
+ return self.polar_to_point(radius, azimuth)
+
+ def pt2pr(self, point: np.ndarray) -> Tuple[float, float]:
+ """Abbreviation for :meth:`point_to_polar`"""
+ return self.point_to_polar(point)
+
def get_axes(self):
raise NotImplementedError()
@@ -800,6 +856,44 @@ class CoordinateSystem:
def get_parametric_curve(self, function, **kwargs):
return self.plot_parametric_curve(function, **kwargs)
+ def plot_polar_graph(
+ self,
+ r_func: Callable[[float], float],
+ theta_range: Sequence[float] = [0, 2 * PI],
+ **kwargs,
+ ) -> ParametricFunction:
+ """A polar graph.
+
+ Parameters
+ ----------
+ r_func
+ The function r of theta.
+ theta_range
+ The range of theta as ``theta_range = [theta_min, theta_max, theta_step]``.
+ kwargs
+ Additional parameters passed to :class:`~.ParametricFunction`.
+
+ Examples
+ --------
+ .. manim:: PolarGraphExample
+ :ref_classes: PolarPlane
+ :save_last_frame:
+
+ class PolarGraphExample(Scene):
+ def construct(self):
+ plane = PolarPlane()
+ r = lambda theta: 2 * np.sin(theta * 5)
+ graph = plane.plot_polar_graph(r, [0, 2 * PI], color=ORANGE)
+ self.add(plane, graph)
+ """
+ graph = ParametricFunction(
+ function=lambda th: self.pr2pt(r_func(th), th),
+ t_range=theta_range,
+ **kwargs,
+ )
+ graph.underlying_function = r_func
+ return graph
+
def input_to_graph_point(
self,
x: float,
@@ -2624,7 +2718,7 @@ class PolarPlane(Axes):
Tuple[:class:`~.VGroup`, :class:`~.VGroup`]
The first (i.e the non faded lines and circles) and second (i.e the faded lines and circles) sets of lines and circles, respectively.
"""
- center = self.get_center_point()
+ center = self.get_origin()
ratio_faded_lines = self.faded_line_ratio
offset = self.azimuth_offset
@@ -2685,62 +2779,6 @@ class PolarPlane(Axes):
mob.insert_n_curves(num_inserted_curves - num_curves)
return self
- def polar_to_point(self, radius: float, azimuth: float) -> np.ndarray:
- r"""Gets a point from polar coordinates.
-
- Parameters
- ----------
- radius
- The coordinate radius (:math:`r`).
-
- azimuth
- The coordinate azimuth (:math:`\theta`).
-
- Returns
- -------
- numpy.ndarray
- The point.
-
- Examples
- --------
-
- .. manim:: PolarToPointExample
- :ref_classes: PolarPlane Vector
- :save_last_frame:
-
- class PolarToPointExample(Scene):
- def construct(self):
- polarplane_pi = PolarPlane(azimuth_units="PI radians", size=6)
- polartopoint_vector = Vector(polarplane_pi.polar_to_point(3, PI/4))
- self.add(polarplane_pi)
- self.add(polartopoint_vector)
- """
- return self.coords_to_point(radius * np.cos(azimuth), radius * np.sin(azimuth))
-
- def pr2pt(self, radius: float, azimuth: float) -> np.ndarray:
- """Abbreviation for :meth:`polar_to_point`"""
- return self.polar_to_point(radius, azimuth)
-
- def point_to_polar(self, point: np.ndarray) -> Tuple[float, float]:
- r"""Gets polar coordinates from a point.
-
- Parameters
- ----------
- point
- The point.
-
- Returns
- -------
- Tuple[:class:`float`, :class:`float`]
- The coordinate radius (:math:`r`) and the coordinate azimuth (:math:`\theta`).
- """
- x, y = self.point_to_coords(point)
- return np.sqrt(x ** 2 + y ** 2), np.arctan2(y, x)
-
- def pt2pr(self, point: np.ndarray) -> Tuple[float, float]:
- """Abbreviation for :meth:`point_to_polar`"""
- return self.point_to_polar(point)
-
def get_coordinate_labels(
self,
r_values: Optional[Iterable[float]] = None,
diff --git a/tests/control_data/graphical_units_data/plot/polar_graph.npz b/tests/control_data/graphical_units_data/plot/polar_graph.npz
new file mode 100644
index 000000000..18bd0f477
Binary files /dev/null and b/tests/control_data/graphical_units_data/plot/polar_graph.npz differ
diff --git a/tests/test_graphical_units/test_axes.py b/tests/test_graphical_units/test_axes.py
index 3e8e06250..929377f27 100644
--- a/tests/test_graphical_units/test_axes.py
+++ b/tests/test_graphical_units/test_axes.py
@@ -244,6 +244,14 @@ def test_get_z_axis_label(scene):
scene.add(ax, lab)
+@frames_comparison
+def test_polar_graph(scene):
+ polar = PolarPlane()
+ r = lambda theta: 4 * np.sin(theta * 4)
+ polar_graph = polar.plot_polar_graph(r)
+ scene.add(polar, polar_graph)
+
+
@frames_comparison
def test_log_scaling_graph(scene):
ax = Axes(