A community-maintained Python framework for creating mathematical animations.
Add documentation for growing animations (#2340)
Marcin Serwin0757579460e5
diff --git a/docs/source/installation/troubleshooting.rst b/docs/source/installation/troubleshooting.rstindex 81db753ec..982685e5c 100644--- a/docs/source/installation/troubleshooting.rst+++ b/docs/source/installation/troubleshooting.rst@@ -220,7 +220,7 @@ See below to fix aliases.3. Within this menu disable the alias(es) that is causing the issue (``python`` and/or ``python3``).``IndexError: List index out of range``---------+---------------------------------------Did you install LaTeX using MiKTeX? If so, open the MiKTeX console,install the ``cm-super`` package, then delete the ``media`` directory anddiff --git a/manim/animation/growing.py b/manim/animation/growing.pyindex c48fd23b8..35aa51701 100644--- a/manim/animation/growing.py+++ b/manim/animation/growing.py@@ -1,4 +1,25 @@-"""Animations that grow mobjects."""+"""Animations that introduce mobjects to scene by growing them from points.++.. manim:: Growing++ class Growing(Scene):+ def construct(self):+ square = Square()+ circle = Circle()+ triangle = Triangle()+ arrow = Arrow(LEFT, RIGHT)+ star = Star()++ VGroup(square, circle, triangle).set_x(0).arrange(buff=1.5).set_y(2)+ VGroup(arrow, star).move_to(DOWN).set_x(0).arrange(buff=1.5).set_y(-2)++ self.play(GrowFromPoint(square, ORIGIN))+ self.play(GrowFromCenter(circle))+ self.play(GrowFromEdge(triangle, DOWN))+ self.play(GrowArrow(arrow))+ self.play(SpinInFromNothing(star))++"""__all__ = ["GrowFromPoint",@@ -14,6 +35,8 @@ import numpy as npfrom ..animation.transform import Transformfrom ..constants import PI+from ..utils.deprecation import deprecated_params+from ..utils.paths import spiral_pathif typing.TYPE_CHECKING:from ..mobject.geometry import Arrow@@ -21,6 +44,35 @@ if typing.TYPE_CHECKING:class GrowFromPoint(Transform):+ """Introduce an :class:`~.Mobject` by growing it from a point.++ Parameters+ ----------+ mobject+ The mobjects to be introduced.+ point+ The point from which the mobject grows.+ point_color+ Initial color of the mobject before growing to its full size. Leave empty to match mobject's color.++ Examples+ --------++ .. manim :: GrowFromPointExample++ class GrowFromPointExample(Scene):+ def construct(self):+ dot = Dot(3 * UR, color=GREEN)+ squares = [Square() for _ in range(4)]+ VGroup(*squares).set_x(0).arrange(buff=1)+ self.add(dot)+ self.play(GrowFromPoint(squares[0], ORIGIN))+ self.play(GrowFromPoint(squares[1], [-2, 2, 0]))+ self.play(GrowFromPoint(squares[2], [3, -2, 0], RED))+ self.play(GrowFromPoint(squares[3], dot, dot.get_color()))++ """+def __init__(self, mobject: "Mobject", point: np.ndarray, point_color: str = None, **kwargs) -> None:@@ -41,21 +93,97 @@ class GrowFromPoint(Transform):class GrowFromCenter(GrowFromPoint):+ """Introduce an :class:`~.Mobject` by growing it from its center.++ Parameters+ ----------+ mobject+ The mobjects to be introduced.+ point_color+ Initial color of the mobject before growing to its full size. Leave empty to match mobject's color.++ Examples+ --------++ .. manim :: GrowFromCenterExample++ class GrowFromCenterExample(Scene):+ def construct(self):+ squares = [Square() for _ in range(2)]+ VGroup(*squares).set_x(0).arrange(buff=2)+ self.play(GrowFromCenter(squares[0]))+ self.play(GrowFromCenter(squares[1], point_color=RED))++ """+def __init__(self, mobject: "Mobject", point_color: str = None, **kwargs) -> None:point = mobject.get_center()super().__init__(mobject, point, point_color=point_color, **kwargs)class GrowFromEdge(GrowFromPoint):- def __init__(self, mobject: "Mobject", edge: np.ndarray, **kwargs) -> None:+ """Introduce an :class:`~.Mobject` by growing it from one of its bounding box edges.++ Parameters+ ----------+ mobject+ The mobjects to be introduced.+ edge+ The direction to seek bounding box edge of mobject.+ point_color+ Initial color of the mobject before growing to its full size. Leave empty to match mobject's color.++ Examples+ --------++ .. manim :: GrowFromEdgeExample++ class GrowFromEdgeExample(Scene):+ def construct(self):+ squares = [Square() for _ in range(4)]+ VGroup(*squares).set_x(0).arrange(buff=1)+ self.play(GrowFromEdge(squares[0], DOWN))+ self.play(GrowFromEdge(squares[1], RIGHT))+ self.play(GrowFromEdge(squares[2], UR))+ self.play(GrowFromEdge(squares[3], UP, point_color=RED))+++ """++ def __init__(+ self, mobject: "Mobject", edge: np.ndarray, point_color: str = None, **kwargs+ ) -> None:point = mobject.get_critical_point(edge)- super().__init__(mobject, point, **kwargs)+ super().__init__(mobject, point, point_color=point_color, **kwargs)class GrowArrow(GrowFromPoint):- def __init__(self, arrow: "Arrow", **kwargs) -> None:+ """Introduce an :class:`~.Arrow` by growing it from its start toward its tip.++ Parameters+ ----------+ arrow+ The arrow to be introduced.+ point_color+ Initial color of the arrow before growing to its full size. Leave empty to match arrow's color.++ Examples+ --------++ .. manim :: GrowArrowExample++ class GrowArrowExample(Scene):+ def construct(self):+ arrows = [Arrow(2 * LEFT, 2 * RIGHT), Arrow(2 * DR, 2 * UL)]+ VGroup(*arrows).set_x(0).arrange(buff=2)+ self.play(GrowArrow(arrows[0]))+ self.play(GrowArrow(arrows[1], point_color=RED))++ """++ def __init__(self, arrow: "Arrow", point_color: str = None, **kwargs) -> None:point = arrow.get_start()- super().__init__(arrow, point, **kwargs)+ super().__init__(arrow, point, point_color=point_color, **kwargs)def create_starting_mobject(self) -> "Mobject":start_arrow = self.mobject.copy()@@ -66,5 +194,48 @@ class GrowArrow(GrowFromPoint):class SpinInFromNothing(GrowFromCenter):- def __init__(self, mobject: "Mobject", path_arc: float = PI, **kwargs) -> None:- super().__init__(mobject, path_arc=path_arc, **kwargs)+ """Introduce an :class:`~.Mobject` spinning and growing it from its center.++ Parameters+ ----------+ mobject+ The mobjects to be introduced.+ angle+ The amount of spinning before mobject reaches its full size. E.g. 2*PI means+ that the object will do one full spin before being fully introduced.+ point_color+ Initial color of the mobject before growing to its full size. Leave empty to match mobject's color.++ Examples+ --------++ .. manim :: SpinInFromNothingExample++ class SpinInFromNothingExample(Scene):+ def construct(self):+ squares = [Square() for _ in range(3)]+ VGroup(*squares).set_x(0).arrange(buff=2)+ self.play(SpinInFromNothing(squares[0]))+ self.play(SpinInFromNothing(squares[1], angle=2 * PI))+ self.play(SpinInFromNothing(squares[2], point_color=RED))++ """++ @deprecated_params(+ params="path_arc",+ message="Parameter angle is more robust, supporting angles greater than PI.",+ )+ def __init__(+ self,+ mobject: "Mobject",+ angle: float = PI / 2,+ point_color: str = None,+ **kwargs+ ) -> None:+ if "path_arc" in kwargs:+ super().__init__(mobject, point_color=point_color, **kwargs)+ else:+ self.angle = angle+ super().__init__(+ mobject, path_func=spiral_path(angle), point_color=point_color, **kwargs+ )diff --git a/manim/utils/paths.py b/manim/utils/paths.pyindex eae38998e..fc76cf64f 100644--- a/manim/utils/paths.py+++ b/manim/utils/paths.py@@ -58,3 +58,17 @@ def clockwise_path():def counterclockwise_path():return path_along_arc(np.pi)+++def spiral_path(angle, axis=OUT):+ if abs(angle) < STRAIGHT_PATH_THRESHOLD:+ return straight_path+ if np.linalg.norm(axis) == 0:+ axis = OUT+ unit_axis = axis / np.linalg.norm(axis)++ def path(start_points, end_points, alpha):+ rot_matrix = rotation_matrix((alpha - 1) * angle, unit_axis)+ return start_points + alpha * np.dot(end_points - start_points, rot_matrix.T)++ return pathdiff --git a/tests/control_data/graphical_units_data/creation/SpinInFromNothing.npz b/tests/control_data/graphical_units_data/creation/SpinInFromNothing.npzindex 8aa8612e2..1c051cf52 100644Binary files a/tests/control_data/graphical_units_data/creation/SpinInFromNothing.npz and b/tests/control_data/graphical_units_data/creation/SpinInFromNothing.npz differ