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

Add setitem to mobject #1852

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

TurkeyBilly
Copy link
Contributor

Motivation

image
We normally treat VGroup and mobjects as lists of submobjects. We should give them the same property like a list to get and modify a specific index of element.

Proposed changes

  • In mobject.mobject.py, added setitem method under getitem

Test

Code:

    def str_list_to_tex_list(self, f: list[str]) -> VGroup:
        return  VGroup(
            *[Tex(s, **self.tex_kwargs)
            for s in f]
        )

    def construct(self): 
        formulas = [
            r"f(x)+g(x)=(m_{1}+m_{2})x+(b_{1}+b_{2})",
            r"[f(x)+g(x)]=m_{1}+m_{2}", #
            r"[f(x)+g(x)]=f'(x)+g'(x)", #
            r"f(x)\cdot g(x)=(m_{1}x+b_{1})(m_{2}x+b_{2})",
            r"=m_{1}m_{2}x^{2}+(m_{1}b_{2}+m_{2}b_{1})x+b_{1}b_{2}",
            r"[f(x)\cdot g(x)]=2m_{1}m_{2}+m_{1}b_{2}+m_{2}b_{1}", #
            r"[f(x)\cdot g(x)]=m_{1}(m_{2}+b_{2})+m_{2}(m_{1}+b_{1})", #
            r"[f(x)\cdot g(x)]=f'(x)g(x)+g'(x)f(x)" #
        ]
        formulas = self.str_list_to_tex_list(formulas).arrange(DOWN)
        di = [2, 3, 6, 7, 8]
        for i in di:
            i = i - 1
            cp = derivative_sign.copy().next_to(formulas[i], LEFT)
            self.add(cp)
            formulas[i] = VGroup(cp, formulas[i])

Result:
image

@3b1b
Copy link
Owner

3b1b commented Nov 3, 2022

You'd want to use the method Mobject.replace_submobject instead of self.split().setitem.

In general, you don't want to directly edit the submoject list, since restructuring a mobject often requires other things like bounding boxes and updater lists to be updated.

Comment on lines +318 to +321

def __setitem__(self, index, value):
return self.split().__setitem__(index, value)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants