Code Monkey home page Code Monkey logo

manim-data-structures's Introduction

Light Mode Logo

PyPI Latest Release MIT License Code style: black Documentation Status Downloads

A plugin that contains common data structures to create Manimations.

Installation

To install, simply run the following command:

pip install manim-data-structures

Importing

Simply use the following line of code to import the package:

from manim_data_structures import *

Usage

Code

class MainScene(Scene):
    def construct(self):
        # Create an array
        arr = MArray([8, 7, 6, 5, 4])
        self.play(Create(arr))

        # Animate array
        self.play(arr.animate.shift(UP * 2.5 + LEFT * 5))

        # Animate array element
        self.play(arr.animate_elem(3).shift(DOWN * 0.5))

        # Animate array element mobjects
        self.play(arr.animate_elem_square(0).set_fill(BLACK), arr.animate_elem_value(0).rotate(PI / 2).set_fill(RED), arr.animate_elem_index(0).rotate(PI / 2))

        # Display array with hex values
        arr2 = MArray([0, 2, 4, 6, 8], index_hex_display=True, index_offset=4)
        self.play(Create(arr2))
        self.play(arr2.animate.shift(DOWN * 2.5 + LEFT * 5))

        # Create customized array
        arr3 = MArray([1, 1, 2], mob_square_args={'color': GRAY_A, 'fill_color': RED_E, 'side_length': 0.5}, mob_value_args={'color': GOLD_A, 'font_size': 28}, mob_index_args={'color': RED_E, 'font_size': 22})
        self.play(Create(arr3))

        # Append element
        self.play(Write(arr2.append_elem(10)))

        # Append customized element
        self.play(Write(arr2.append_elem(12, mob_square_args={'fill_color': BLACK})))

        # Update value of element
        self.play(Write(arr2.update_elem_value(3, 0, mob_value_args={'color': RED})), arr2.animate_elem_square(3).set_fill(WHITE))

        self.wait()

Output

Array_Example.mp4

Other Links

manim-data-structures's People

Contributors

drageelr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

manim-data-structures's Issues

Update python version

Description of the proposed feature

The library requires an old version of python

Overloading operators for MVariable, MArray and MArrayPointer classes.

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.

Index out of bounds! Error when run example code block

Description of the bug

When I run the example code black. I met the Exception: Index out of bounds!

Expected behavior

Steps to reproduce

Code for reproducing the problem
Paste your code here.
I just past-and-copy the example code 

Additional media files

Images/GIFs image

Logs

Terminal output
PASTE HERE OR PROVIDE LINK TO https://pastebin.com/ OR SIMILAR

System specifications

System Details
  • OS (with version, e.g., Windows 10 v2004 or macOS 10.15 (Catalina)):
  • RAM:
  • Python version (python/py/python3 --version):
  • Installed modules (provide output from pip list):
Python 3.10.8
LaTeX details
  • LaTeX distribution (e.g. TeX Live 2020):
  • Installed LaTeX packages:
image
FFMPEG

Output of ffmpeg -version:

ffmpeg version 4.2.2 Copyright (c) 2000-2019 the FFmpeg developers

Additional comments

Add support for 2D Arrays

Add support for 2D arrays as an M2DArray class. The accompanying pointer and sliding window classes (e.g., M2DArrayPointer and M2DArraySlidingWindow) should also be added.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.