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

Rework example for :class:~.Variable #1706

Merged
merged 5 commits into from Jul 7, 2021

Conversation

UraniumCronorum
Copy link
Contributor

@UraniumCronorum UraniumCronorum commented Jun 18, 2021

Changelog / Overview

Motivation

Pull request for this hackathon submission.

(My Discord tag: UraniumCronorum#9747)

Explanation for Changes

Documentation Reference

Testing Status

Further Comments

Checklist

  • I have read the Contributing Guidelines
  • I have written a descriptive PR title (see top of PR template for examples)
  • I have written a changelog entry for the PR or deem it unnecessary
  • My new functions/classes either have a docstring or are private
  • My new functions/classes have tests added and (optional) examples in the docs
  • My new documentation builds, looks correctly formatted, and adds no additional build warnings

Reviewer Checklist

  • The PR title is descriptive enough
  • The PR is labeled correctly
  • The changelog entry is completed if necessary
  • Newly added functions/classes either have a docstring or are private
  • Newly added functions/classes have tests added and (optional) examples in the docs
  • Newly added documentation builds, looks correctly formatted, and adds no additional build warnings

@UraniumCronorum UraniumCronorum marked this pull request as ready for review June 18, 2021 00:38
@PhotonSpheres
Copy link
Member

PhotonSpheres commented Jun 18, 2021

Hey there, thank you for your contribution!
Unfortunately, I don't think this example fits well: Since you interpolate linearly between the beginning numbers and their respective end numbers, there will be wrong equations on-screen during the animation, see the screenshot below.
I guess one fix would be to calculate the square number for each frame.
ex

@hydrobeam hydrobeam added documentation Improvements or additions to documentation hackathon manim-hackathon related labels Jun 18, 2021
@PhotonSpheres
Copy link
Member

If you don't mind a little more input, I can suggest the following changes to your example:

class VariableExample(Scene):
    def construct(self):
        x = 0
        x_sqr = x**2
        x_display = Variable(x, "x", num_decimal_places=3)
        sqr_display = Variable(x_sqr, "x^2", num_decimal_places=3).next_to(x_display,DOWN)
        Group(x_display, sqr_display).center()
        self.play(
            Write(x_display),
            Write(sqr_display)
        )
        x_tracker = x_display.tracker
        sqr_tracker = sqr_display.tracker
        x = 5.6
        x_sqr = x**2
        self.play(
            x_tracker.animate(rate_func=linear).set_value(x),
            sqr_tracker.animate(rate_func=lambda x: x**2).set_value(x_sqr),
        )
        self.wait()
VariableExample.mp4

@UraniumCronorum
Copy link
Contributor Author

Hi, thanks for the feedback.

I tried running the code you suggested and it seems to work fine, but when I change the initial value of x to something other than 0, the results start to get out of sync again. I'm trying to figure out how to fix this, but I haven't made much progress yet.

I also tried your suggestion about calculating the values for each frame, and this seems to work:

class VariableExample(Scene):
    def construct(self):
        start = 2.0

        x_var = Variable(start, 'x', num_decimal_places=3)
        sqr_var = Variable(start**2, 'x^2',
                           num_decimal_places=3).next_to(x_var, DOWN)
        self.add(x_var, sqr_var)

        x_var.tracker.add_updater(lambda m, dt: m.set_value(m.get_value()+dt))
        sqr_var.tracker.add_updater(lambda m:
                                    m.set_value(x_var.tracker.get_value()**2))
        self.add(x_var.tracker)
        self.add(sqr_var.tracker)

        self.wait(3)

@PhotonSpheres
Copy link
Member

I tried running the code you suggested and it seems to work fine, but when I change the initial value of x to something other than 0, the results start to get out of sync again. I'm trying to figure out how to fix this, but I haven't made much progress yet.

This is due to how rate_func. I don't know how easy it would be to generalize my suggestion to other start values than 0.

I also tried your suggestion about calculating the values for each frame, and this seems to work:

I really like your example, just adding the tracker to the scene seems a bit weird to me. I think, building up on your idea, this might be a bit cleaner

class VariableExample(Scene):
    def construct(self):
        start = 2.0

        x_var = Variable(start, 'x', num_decimal_places=3)
        sqr_var = Variable(start**2, 'x^2', num_decimal_places=3)
        Group(x_var, sqr_var).arrange(DOWN)

        sqr_var.add_updater(lambda v: v.tracker.set_value(x_var.tracker.get_value()**2))

        self.add(x_var, sqr_var)
        self.play(x_var.tracker.animate.set_value(5), run_time=2, rate_func=linear)
        self.wait(0.1)

What do you think?

@behackl
Copy link
Member

behackl commented Jun 30, 2021

I like the latest suggestion by @PhotonSpheres, +1 for reworking the example from this PR in that direction.

Also, instead of removing the current example, I suggest to keep the current one and add this as an additional example.

Copy link
Member

@PhotonSpheres PhotonSpheres left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Cheers again

Copy link
Member

@behackl behackl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for your contribution!

@behackl behackl enabled auto-merge (squash) July 7, 2021 11:09
@behackl behackl merged commit 195197b into ManimCommunity:main Jul 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation hackathon manim-hackathon related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants