Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Added keyword arguments ``match_center``, ``match_width`` etc. to :meth:`.Mobject.become` (#2128)

Nick3a6329dae1fa
diff --git a/manim/mobject/mobject.py b/manim/mobject/mobject.py
index 654e4616e..6b10e7c9c 100644
--- a/manim/mobject/mobject.py
+++ b/manim/mobject/mobject.py
@@ -2603,10 +2603,37 @@ class Mobject:
def interpolate_color(self, mobject1, mobject2, alpha):
raise NotImplementedError("Please override in a child class.")
- def become(self, mobject: "Mobject", copy_submobjects: bool = True):
+ def become(
+ self,
+ mobject: "Mobject",
+ copy_submobjects: bool = True,
+ match_height: bool = False,
+ match_width: bool = False,
+ match_depth: bool = False,
+ match_center: bool = False,
+ stretch: bool = False,
+ ):
"""Edit points, colors and submobjects to be identical
to another :class:`~.Mobject`
+ .. note::
+
+ If both match_height and match_width are ``True`` then the transformed :class:`~.Mobject`
+ will match the height first and then the width
+
+ Parameters
+ ----------
+ match_height
+ If ``True``, then the transformed :class:`~.Mobject` will match the height of the original
+ match_width
+ If ``True``, then the transformed :class:`~.Mobject` will match the width of the original
+ match_depth
+ If ``True``, then the transformed :class:`~.Mobject` will match the depth of the original
+ match_center
+ If ``True``, then the transformed :class:`~.Mobject` will match the center of the original
+ stretch
+ If ``True``, then the transformed :class:`~.Mobject` will stretch to fit the proportions of the original
+
Examples
--------
.. manim:: BecomeScene
@@ -2620,6 +2647,22 @@ class Mobject:
circ.become(square)
self.wait(0.5)
"""
+
+ if stretch:
+ mobject.stretch_to_fit_height(self.height)
+ mobject.stretch_to_fit_width(self.width)
+ mobject.stretch_to_fit_depth(self.depth)
+ else:
+ if match_height:
+ mobject.match_height(self)
+ if match_width:
+ mobject.match_width(self)
+ if match_depth:
+ mobject.match_depth(self)
+
+ if match_center:
+ mobject.move_to(self.get_center())
+
self.align_data(mobject)
for sm1, sm2 in zip(self.get_family(), mobject.get_family()):
sm1.points = np.array(sm2.points)
diff --git a/tests/control_data/graphical_units_data/mobjects/become.npz b/tests/control_data/graphical_units_data/mobjects/become.npz
new file mode 100644
index 000000000..4980e9d1d
Binary files /dev/null and b/tests/control_data/graphical_units_data/mobjects/become.npz differ
diff --git a/tests/test_graphical_units/test_mobjects.py b/tests/test_graphical_units/test_mobjects.py
index 4ef82bde3..1bf5e7d99 100644
--- a/tests/test_graphical_units/test_mobjects.py
+++ b/tests/test_graphical_units/test_mobjects.py
@@ -8,3 +8,20 @@ __module_test__ = "mobjects"
def test_PointCloudDot(scene):
p = PointCloudDot()
scene.add(p)
+
+
+@frames_comparison
+def test_become(scene):
+ s = Rectangle(width=2, height=1, color=RED).shift(UP)
+ d1, d2, d3 = (Dot() for _ in range(3))
+
+ s1 = s.copy().become(d1, match_width=True).set_opacity(0.25).set_color(BLUE)
+ s2 = (
+ s.copy()
+ .become(d2, match_height=True, match_center=True)
+ .set_opacity(0.25)
+ .set_color(GREEN)
+ )
+ s3 = s.copy().become(d3, stretch=True).set_opacity(0.25).set_color(YELLOW)
+
+ scene.add(s, d1, d2, d3, s1, s2, s3)