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
Conversation
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 |
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) |
This is due to how
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? |
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Cheers again
There was a problem hiding this 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!
Changelog / Overview
Motivation
Pull request for this hackathon submission.
(My Discord tag: UraniumCronorum#9747)
Explanation for Changes
Documentation Reference
Testing Status
Further Comments
Checklist
Reviewer Checklist