mirror of
https://github.com/3b1b/manim.git
synced 2025-07-31 22:13:30 +08:00
Refactored DecimalNumber to better use string formatting syntax
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
|
|
||||||
from constants import *
|
from constants import *
|
||||||
|
import operator as op
|
||||||
|
|
||||||
from mobject.svg.tex_mobject import SingleStringTexMobject
|
from mobject.svg.tex_mobject import SingleStringTexMobject
|
||||||
from mobject.types.vectorized_mobject import VMobject
|
from mobject.types.vectorized_mobject import VMobject
|
||||||
@ -9,33 +10,28 @@ from mobject.types.vectorized_mobject import VMobject
|
|||||||
class DecimalNumber(VMobject):
|
class DecimalNumber(VMobject):
|
||||||
CONFIG = {
|
CONFIG = {
|
||||||
"num_decimal_places": 2,
|
"num_decimal_places": 2,
|
||||||
|
"include_sign": False,
|
||||||
|
"group_with_commas": True,
|
||||||
"digit_to_digit_buff": 0.05,
|
"digit_to_digit_buff": 0.05,
|
||||||
"show_ellipsis": False,
|
"show_ellipsis": False,
|
||||||
"unit": None, # Aligned to bottom unless it starts with "^"
|
"unit": None, # Aligned to bottom unless it starts with "^"
|
||||||
"include_background_rectangle": False,
|
"include_background_rectangle": False,
|
||||||
"include_sign": False,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def __init__(self, number, **kwargs):
|
def __init__(self, number, **kwargs):
|
||||||
VMobject.__init__(self, **kwargs)
|
VMobject.__init__(self, **kwargs)
|
||||||
# TODO, make this more ediable with a getter and setter
|
# TODO, make this more ediable with a getter and setter
|
||||||
self.number = number
|
self.number = number
|
||||||
ndp = self.num_decimal_places
|
|
||||||
|
|
||||||
# Build number string
|
|
||||||
if isinstance(number, complex):
|
if isinstance(number, complex):
|
||||||
num_string = '%.*f%s%.*fi' % (
|
formatter = self.get_complex_formatter()
|
||||||
ndp, number.real,
|
|
||||||
"-" if number.imag < 0 else "+",
|
|
||||||
ndp, abs(number.imag)
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
num_string = '%.*f' % (ndp, number)
|
formatter = self.get_formatter()
|
||||||
if self.include_sign and number >= 0:
|
num_string = formatter.format(number)
|
||||||
num_string = "+" + num_string
|
|
||||||
negative_zero_string = "-%.*f" % (ndp, 0.)
|
if num_string.startswith("-") and number == 0:
|
||||||
if num_string == negative_zero_string:
|
|
||||||
num_string = num_string[1:]
|
num_string = num_string[1:]
|
||||||
|
|
||||||
self.add(*[
|
self.add(*[
|
||||||
SingleStringTexMobject(char, **kwargs)
|
SingleStringTexMobject(char, **kwargs)
|
||||||
for char in num_string
|
for char in num_string
|
||||||
@ -66,12 +62,39 @@ class DecimalNumber(VMobject):
|
|||||||
for i, c in enumerate(num_string):
|
for i, c in enumerate(num_string):
|
||||||
if c == "-" and len(num_string) > i + 1:
|
if c == "-" and len(num_string) > i + 1:
|
||||||
self[i].align_to(self[i + 1], alignment_vect=UP)
|
self[i].align_to(self[i + 1], alignment_vect=UP)
|
||||||
|
elif c == ",":
|
||||||
|
self[i].shift(self[i].get_height() * DOWN / 2)
|
||||||
if self.unit and self.unit.startswith("^"):
|
if self.unit and self.unit.startswith("^"):
|
||||||
self.unit_sign.align_to(self, UP)
|
self.unit_sign.align_to(self, UP)
|
||||||
#
|
#
|
||||||
if self.include_background_rectangle:
|
if self.include_background_rectangle:
|
||||||
self.add_background_rectangle()
|
self.add_background_rectangle()
|
||||||
|
|
||||||
|
def get_formatter(self, **kwargs):
|
||||||
|
"""
|
||||||
|
Configuration is based first off instance attributes,
|
||||||
|
but overwritten by any kew word argument. Relevant
|
||||||
|
key words:
|
||||||
|
- include_sign
|
||||||
|
- group_with_commas
|
||||||
|
- num_decimal_places
|
||||||
|
- field_name (e.g. 0 or 0.real)
|
||||||
|
"""
|
||||||
|
config = dict(self.__dict__)
|
||||||
|
config.update(kwargs)
|
||||||
|
return reduce(op.add, [
|
||||||
|
"{",
|
||||||
|
config.get("field_name", ""),
|
||||||
|
":",
|
||||||
|
"+" if config["include_sign"] else "",
|
||||||
|
"," if config["group_with_commas"] else "",
|
||||||
|
".", str(config["num_decimal_places"]), "f",
|
||||||
|
"}",
|
||||||
|
])
|
||||||
|
|
||||||
|
def get_complex_formatter(self, **kwargs):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class Integer(DecimalNumber):
|
class Integer(DecimalNumber):
|
||||||
CONFIG = {
|
CONFIG = {
|
||||||
|
Reference in New Issue
Block a user