Description
Discussed in #17
Originally posted by ttzytt February 22, 2023
Found some places to improve when using the package to implement the animation of a simple binary search algorithm:
class MyScene(Scene):
def construct(self):
arr = MArray(self, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
arr.shift(LEFT * 4 + UP * 2)
tar_val = MVariable(self, 3, label="target value")
tar_val.shift(DOWN * 2)
l = MArrayPointer(self, arr, 0, label="left")
r = MArrayPointer(self, arr, 9, label="right")
self.play(Create(arr))
self.play(Create(tar_val))
self.play(Create(l))
self.play(Create(r))
mid = MArrayPointer(self, arr, (l.fetch_index() + r.fetch_index()) // 2, label="mid")
self.play(Create(mid))
# find first larger or equal to target value
while (l.fetch_index() <= r.fetch_index()):
anims = []
if (mid.fetch_index() < tar_val.fetch_value()):
anims.append(l.shift_to_elem(mid.fetch_index() + 1, play_anim=False))
else:
anims.append(r.shift_to_elem(mid.fetch_index() - 1, play_anim=False))
anims.append(mid.shift_to_elem((l.fetch_index() + r.fetch_index()) // 2, play_anim=False))
self.play(AnimationGroup(*anims))
self.wait(1)
For every comparison between MArrayPointer
, you have to call fetch_index()
to get the position where the array is pointing. I think it will be better to overload comparators in MArrayPointer so that the implementation will be easier.
The same works for other operators like add and subtract: the addition or subtraction between MArrayPointer
an integer or another MArrayPointer
should give the addition and subtraction between the integer and the index that the pointer is pointing to in a MArray
. That way, the operation of pointers will be much easier, and code like the following will be simplified:
r.shift_to_elem(mid.fetch_index() - 1, play_anim=False
mid.shift_to_elem((l.fetch_index() + r.fetch_index()) // 2
Another possible improvement from overloading operators is to overload __getitem__
and __setitem__
for MArray
. Using these two methods, we could directly access the MArrayElement
inside the MArray
through the bracket operator, thus making animations on MArrayElement
in an easier way.
I'm not sure about your ideas for these modifications, but if you think they are helpful, I can make a PR on that.
@ttzytt Please provide details for which classes and operators you think should be overloaded.
Metadata
Metadata
Assignees
Labels
Projects
Status