Skip to content

Improve docstring for interpolate method in Mobject class #4149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions manim/animation/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ def make_arc_path(start, end, arc_angle):

self.play(*anims, run_time=2)
self.wait()

See also
--------
:class:`~.ReplacementTransform`, :meth:`~.Mobject.interpolate`, :meth:`~.Mobject.align_data`
"""

def __init__(
Expand Down
60 changes: 51 additions & 9 deletions manim/mobject/mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -2840,22 +2840,64 @@ def interpolate(
"""Turns this :class:`~.Mobject` into an interpolation between ``mobject1``
and ``mobject2``.

The interpolation is applied to the points and color of the mobject.

Parameters
----------
mobject1
The starting Mobject.
mobject2
The target Mobject.
alpha
Interpolation factor between 0 (at ``mobject1``) and 1 (at ``mobject2``).
path_func
The function defining the interpolation path. Defaults to a straight path.

Returns
-------
:class:`Mobject`
``self``


.. note::

- Both mobjects must have the same number of points. If not, this will raise an error.
Use :meth:`~.VMobject.align_points` to match point counts beforehand if needed.
- This method is used internally by the :class:`~.Transform` animation
to interpolate between two mobjects during a transformation.

Examples
--------

.. manim:: DotInterpolation
.. manim:: InterpolateExample
:save_last_frame:

class DotInterpolation(Scene):
class InterpolateExample(Scene):
def construct(self):
dotR = Dot(color=DARK_GREY)
dotR.shift(2 * RIGHT)
dotL = Dot(color=WHITE)
dotL.shift(2 * LEFT)

dotMiddle = VMobject().interpolate(dotL, dotR, alpha=0.3)
# No need for point alignment:
dotL = Dot(color=DARK_GREY).to_edge(LEFT)
dotR = Dot(color=YELLOW).scale(10).to_edge(RIGHT)
dotMid1 = VMobject().interpolate(dotL, dotR, alpha=0.1)
dotMid2 = VMobject().interpolate(dotL, dotR, alpha=0.25)
dotMid3 = VMobject().interpolate(dotL, dotR, alpha=0.5)
dotMid4 = VMobject().interpolate(dotL, dotR, alpha=0.75)
dots = VGroup(dotL, dotR, dotMid1, dotMid2, dotMid3, dotMid4)

# Needs point alignment:
line = Line(ORIGIN, UP).to_edge(LEFT)
sq = Square(color=RED, fill_opacity=1, stroke_color=BLUE).to_edge(RIGHT)
line.align_points(sq)
mid1 = VMobject().interpolate(line, sq, alpha=0.1)
mid2 = VMobject().interpolate(line, sq, alpha=0.25)
mid3 = VMobject().interpolate(line, sq, alpha=0.5)
mid4 = VMobject().interpolate(line, sq, alpha=0.75)
linesquares = VGroup(line, sq, mid1, mid2, mid3, mid4)

self.add(VGroup(dots, linesquares).arrange(DOWN, buff=1))
See also
--------
:class:`~.Transform`, :meth:`~.VMobject.align_points`, :meth:`~.VMobject.interpolate_color`

self.add(dotL, dotR, dotMiddle)
"""
self.points = path_func(mobject1.points, mobject2.points, alpha)
self.interpolate_color(mobject1, mobject2, alpha)
Expand Down
4 changes: 4 additions & 0 deletions manim/mobject/types/vectorized_mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -1720,6 +1720,10 @@ def align_points(self, vmobject: VMobject) -> Self:
-------
:class:`VMobject`
``self``

See also
--------
:meth:`~.Mobject.interpolate`, :meth:`~.Mobject.align_data`
"""
self.align_rgbas(vmobject)
# TODO: This shortcut can be a bit over eager. What if they have the same length, but different subpath lengths?
Expand Down