Skip to content
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

Improve docstring for interpolate method in Mobject class #4149

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
f4c2ada
Improve docstring for interpolate method in Mobject class
irvanalhaq9 Feb 3, 2025
6f72a8e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 3, 2025
161a415
Add 'See alse'
irvanalhaq9 Feb 4, 2025
76a379c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 4, 2025
3e91a2d
Improve Note
irvanalhaq9 Feb 4, 2025
573f2af
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 4, 2025
ce291bb
Add 'See also' in Transform docs
irvanalhaq9 Feb 4, 2025
911be09
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 4, 2025
e0e30b1
Change reference for align_points method to VMobject.align_points
irvanalhaq9 Feb 4, 2025
7c613f2
Add 'See also' in VMobject.align_points docs
irvanalhaq9 Feb 4, 2025
c6146a1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 4, 2025
51704fe
Improve 'See also' in Transform docs
irvanalhaq9 Feb 4, 2025
da6ff9e
Add VMobject.interpolate_color in 'See also'
irvanalhaq9 Feb 4, 2025
db35713
Merge branch 'main' into improve-docstring-interpolate
irvanalhaq9 Feb 4, 2025
f84cfdd
Add explanation of parameters
irvanalhaq9 Feb 7, 2025
702c42b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 7, 2025
4d40c19
Move note to above Parameters
irvanalhaq9 Feb 7, 2025
b7391d7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 7, 2025
e4c9b84
Fix return writing self
irvanalhaq9 Feb 7, 2025
8a378e9
Move note to below return
irvanalhaq9 Feb 7, 2025
9d0ddac
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 7, 2025
6bba225
Move note to below return
irvanalhaq9 Feb 7, 2025
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
5 changes: 5 additions & 0 deletions manim/animation/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ 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 @@ -2815,22 +2815,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
5 changes: 5 additions & 0 deletions manim/mobject/types/vectorized_mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -1720,6 +1720,11 @@ 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