I want to make sure the approach I'm using to display an array isn't an awkward work-around for a better approach I'm unaware of. A lot of what I have so far feels pretty awkward.
For spacing elements I set each element of a TextMobject array .next_to the previous element to the right with a buffer. Is there a better approach?
For boxes around elements I was going to draw the top and bottom lines the full width of the array separately, and then draw vertical lines between elements. I don't envision caring about animating the drawing of these boxes in different ways, though I do plan to highlight elements using the existing tools for that. Is there a better approach?
Here's my "hello world" code so far. Any suggestions/improvements more than welcomed!
class ExampleArray(Scene):
def construct(self):
n = 6
labels = TextMobject(* ["[" + str(index) + "]" for index in range(n)] )
text = TextMobject( *[str(random.randint(0,10)) for i in range(n) ] )
# space it out
for i in range(1,len(text)):
text[i].next_to(text[i-1], RIGHT, buff = 0.8)
text.move_to(ORIGIN)
for i in range(len(labels)):
labels[i].scale(0.5)
labels[i].next_to( text[i], DOWN, buff = 0.5)
# can I just make a copy of top_line?
top_line = Line(text.get_left() + LEFT, text.get_right() + RIGHT)
bottom_line = Line(text.get_left() + LEFT, text.get_right() + RIGHT)
top_line.next_to(text, UP)
bottom_line.next_to(text, DOWN)
self.add(labels)
self.add(top_line, bottom_line)
for i in range(len(text)):
self.play(Write(text[i]))
self.wait(0.1)