Skip to content

Commit de210ba

Browse files
Improve docstring for interpolate method in Mobject class (#4149)
Co-authored-by: Francisco Manríquez Novoa <[email protected]>
1 parent ab48557 commit de210ba

File tree

3 files changed

+59
-9
lines changed

3 files changed

+59
-9
lines changed

manim/animation/transform.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ def make_arc_path(start, end, arc_angle):
123123
124124
self.play(*anims, run_time=2)
125125
self.wait()
126+
127+
See also
128+
--------
129+
:class:`~.ReplacementTransform`, :meth:`~.Mobject.interpolate`, :meth:`~.Mobject.align_data`
126130
"""
127131

128132
def __init__(

manim/mobject/mobject.py

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2840,22 +2840,64 @@ def interpolate(
28402840
"""Turns this :class:`~.Mobject` into an interpolation between ``mobject1``
28412841
and ``mobject2``.
28422842
2843+
The interpolation is applied to the points and color of the mobject.
2844+
2845+
Parameters
2846+
----------
2847+
mobject1
2848+
The starting Mobject.
2849+
mobject2
2850+
The target Mobject.
2851+
alpha
2852+
Interpolation factor between 0 (at ``mobject1``) and 1 (at ``mobject2``).
2853+
path_func
2854+
The function defining the interpolation path. Defaults to a straight path.
2855+
2856+
Returns
2857+
-------
2858+
:class:`Mobject`
2859+
``self``
2860+
2861+
2862+
.. note::
2863+
2864+
- Both mobjects must have the same number of points. If not, this will raise an error.
2865+
Use :meth:`~.VMobject.align_points` to match point counts beforehand if needed.
2866+
- This method is used internally by the :class:`~.Transform` animation
2867+
to interpolate between two mobjects during a transformation.
2868+
28432869
Examples
28442870
--------
28452871
2846-
.. manim:: DotInterpolation
2872+
.. manim:: InterpolateExample
28472873
:save_last_frame:
28482874
2849-
class DotInterpolation(Scene):
2875+
class InterpolateExample(Scene):
28502876
def construct(self):
2851-
dotR = Dot(color=DARK_GREY)
2852-
dotR.shift(2 * RIGHT)
2853-
dotL = Dot(color=WHITE)
2854-
dotL.shift(2 * LEFT)
2855-
2856-
dotMiddle = VMobject().interpolate(dotL, dotR, alpha=0.3)
2877+
# No need for point alignment:
2878+
dotL = Dot(color=DARK_GREY).to_edge(LEFT)
2879+
dotR = Dot(color=YELLOW).scale(10).to_edge(RIGHT)
2880+
dotMid1 = VMobject().interpolate(dotL, dotR, alpha=0.1)
2881+
dotMid2 = VMobject().interpolate(dotL, dotR, alpha=0.25)
2882+
dotMid3 = VMobject().interpolate(dotL, dotR, alpha=0.5)
2883+
dotMid4 = VMobject().interpolate(dotL, dotR, alpha=0.75)
2884+
dots = VGroup(dotL, dotR, dotMid1, dotMid2, dotMid3, dotMid4)
2885+
2886+
# Needs point alignment:
2887+
line = Line(ORIGIN, UP).to_edge(LEFT)
2888+
sq = Square(color=RED, fill_opacity=1, stroke_color=BLUE).to_edge(RIGHT)
2889+
line.align_points(sq)
2890+
mid1 = VMobject().interpolate(line, sq, alpha=0.1)
2891+
mid2 = VMobject().interpolate(line, sq, alpha=0.25)
2892+
mid3 = VMobject().interpolate(line, sq, alpha=0.5)
2893+
mid4 = VMobject().interpolate(line, sq, alpha=0.75)
2894+
linesquares = VGroup(line, sq, mid1, mid2, mid3, mid4)
2895+
2896+
self.add(VGroup(dots, linesquares).arrange(DOWN, buff=1))
2897+
See also
2898+
--------
2899+
:class:`~.Transform`, :meth:`~.VMobject.align_points`, :meth:`~.VMobject.interpolate_color`
28572900
2858-
self.add(dotL, dotR, dotMiddle)
28592901
"""
28602902
self.points = path_func(mobject1.points, mobject2.points, alpha)
28612903
self.interpolate_color(mobject1, mobject2, alpha)

manim/mobject/types/vectorized_mobject.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1720,6 +1720,10 @@ def align_points(self, vmobject: VMobject) -> Self:
17201720
-------
17211721
:class:`VMobject`
17221722
``self``
1723+
1724+
See also
1725+
--------
1726+
:meth:`~.Mobject.interpolate`, :meth:`~.Mobject.align_data`
17231727
"""
17241728
self.align_rgbas(vmobject)
17251729
# TODO: This shortcut can be a bit over eager. What if they have the same length, but different subpath lengths?

0 commit comments

Comments
 (0)