본문 바로가기

Programming/Manim Lectures

[05-4-A] DecimalNumber

반응형

When you want to express a number in Manim, you can handle it in Text or TextMobject, but it is convenient to use DecimalNumber, a class for expressing numbers efficiently.

 


DecimalNumber class

object > Container > Mobject > VMobject > DecimalNumber
manimlib.mobject.numbers.DecimalNumber(self, number=0, **kwargs)

Create a numeric object that can be rendered on the screen with the 'number' given as an argument.

 

Parameters: number=0
    The number to make into an object. It can be updated via the set_value() method.

**kwargs
    - num_decimal_places=2: Number of decimal places
    
    - include_sign=False: Decide whether to add positive/negative sign
    
    - group_with_commas=True: Whether a comma (,) is added every three digits
    
    - digit_to_digit_buff=0.05: Margin between letters representing numbers
    
    - show_ellipsis=False: Whether to display empty digits as dots in the whole digit
    
    - unit=None: Unit character to end the number. 
                 It is displayed aligned with the bottom of the number. 
                 Use "^" to align with the top of the number.
                 
    - include_background_rectangle=False: Decide whether to display the background rectangle 
                                          surrounding the displayed number. 
                                          Note that the square color is black.
                                          
    - edge_to_fix=LEFT: Specifies which side of the space is fixed and displayed. 
                        This parameter is reflected only when a numeric value is specified 
                        through set_value.

    - In addition, all **kwargs items used for VMobject are available

You can use TexMobject for numbers used in formulas, but it is convenient to use DecimalNumber to express the number changing continuously like a clock.

 

소숫점 자릿 수 지정

The number of decimal places can be specified in 'num_decimal_places' argument.

 

If you want to represent up to 3 decimal places,

num = DecimalNumber(n, num_decimal_places=3)

 

+/- sign

The positive/negative sign is determined by the 'include_sign' parameter.

 

The default is not to add, and 'include_sign=True' to add a sign.

num = DecimalNumber(n, include_sign=True)

In the figure above, only two decimal places appear because the default value of 'num_decimal_places=2' is applied because 'num_decimal_places' is not specified.

 

Group comma

By default, numbers are separated by 3 digits by comma (,). To get rid of this, specify group_with_commas=False.

num = DecimalNumber(n)
num = DecimalNumber(n, group_with_commas=False)

 

Margin between numbers

You can adjust the 'digit_to_digit_buff' to adjust the spacing between displayed numbers. The default size is 0.05

num = DecimalNumber(n)
num = DecimalNumber(n, digit_to_digit_buff=0.2)

 

Expression for ellipsis(empty digit)

To show dots in empty spots, set show_ellipsis=True.

The edge_to_fix=LEFT is the default value, so if you create a DecimalNumber as the default value, it is expressed in terms of the left side of the entire expression space.

 

If you specify show_ellipsis=True, the empty space on the right side is displayed as dots.

num=DecimalNumber(n, show_ellipsis=True)

 

Unit representation of numbers

You can paste the unit letter such as "cm" "%", you want to express.

 

Note that a symbol like "%" must be specified with an escape symbol (two backslashes, \\).

 

Unit characters are aligned by the bottom of the body number. If you add "^", it will be aligned based on the top side of the number.

 

  n=99.1
  num=DecimalNumber(n, unit="\\%")
  num=DecimalNumber(n, unit="^\\%")

 

Background box display.

Set 'include_background_rectangle=True' if you want the background box to wrap around the number when it is displayed. Default is False

 

Note that the color of the background box is black, so setting this value to True has no effect when the screen background is black.

 

The example below creates a white box and puts a DecimalNumber object there.

 

    rect = Rectangle(fill_opacity=1,fill_color=WHITE)
    n=3.14
    num = DecimalNumber(n, include_background_rectangle=True).move_to(rect)

 

Alignment

The number is aligned by the 'edge_to_fix' value. The default is LEFT. To align right, set edge_to_fix=RIGHT. However, this alignment is reflected only when a numeric value is specified through set_value.

 

As of April 2020 version: The constructor of DecimalNumber must contain the aligning function using 'edge_to_fix', but it is missing, and it is aligned in the direction specified by 'edge_to_fix' only when calling the 'set_value' method.
    d1 = DecimalNumber(0, edge_to_fix=RIGHT)
    d2 = DecimalNumber(0, edge_to_fix=RIGHT).shift(DOWN)

    d1.set_value(10)
    d2.set_value(1000)

 


Example Program

When there is a sliding point on the x-axis, create an animation that represents the x value of this point as a number up to 2 decimal places.

DeciamalNumber is suitable for representing varying numbers, as required by the example program above.

 

To create the example above, we have not introduced it yet (we will cover it in '[06-3] Animation by object updating '), but we will use the 'add_updater' method to create an animation in which the number of DecimalNumber changes according to the movement of the object.

 

class DecimalTest(Scene):
    def construct(self):
        x_axis = NumberLine(x_min=-5, x_max=5)
        dot = Dot(color=RED, radius=0.15).move_to(x_axis.get_left())
        number = DecimalNumber(-5, color=RED).next_to(dot, UP)

        number.add_updater(lambda m: m.next_to(dot, UP))
        number.add_updater(lambda m: m.set_value(dot.get_center()[0]))

        self.add(x_axis,dot,number)
        self.play(dot.shift, RIGHT * 10, rate_func=there_and_back, run_time=10)
        self.wait()

 

 


Next: [05-4-B] Integer

 

[05-4-B] Integer

Integer class object > Container > Mobject > VMobject > DecimalNumber > Integer manimlib.mobject.numbers.Integer(self, number=0, **kwargs) Used when dealing with integer numbers without decimal poin..

infograph.tistory.com

Go To: [99] Table of Contents

반응형