A community-maintained Python framework for creating mathematical animations.
New features: AnnotationDot and LabledDot (#462)
kolibril13797a31b0ac1d
diff --git a/manim/mobject/geometry.py b/manim/mobject/geometry.pyindex bc06db8b5..3ed68fba9 100644--- a/manim/mobject/geometry.py+++ b/manim/mobject/geometry.py@@ -1,6 +1,5 @@"""Mobjects that are simple geometric shapes."""-__all__ = ["TipableVMobject","Arc",@@ -9,6 +8,8 @@ __all__ = ["CurvedDoubleArrow","Circle","Dot",+ "AnnotationDot",+ "LabeledDot","SmallDot","Ellipse","AnnularSector",@@ -31,7 +32,6 @@ __all__ = ["RoundedRectangle",]-import warningsimport numpy as npimport math@@ -52,7 +52,7 @@ from ..utils.space_ops import line_intersectionfrom ..utils.space_ops import get_normfrom ..utils.space_ops import normalizefrom ..utils.space_ops import rotate_vector-from ..utils.color import RED, WHITE, BLUE+from ..utils.color import *DEFAULT_DOT_RADIUS = 0.08DEFAULT_SMALL_DOT_RADIUS = 0.04@@ -150,10 +150,7 @@ class TipableVMobject(VMobject):if at_start:self.put_start_and_end_on(tip.base, self.get_end())else:- self.put_start_and_end_on(- self.get_start(),- tip.base,- )+ self.put_start_and_end_on(self.get_start(), tip.base)return selfdef asign_tip_attr(self, tip, at_start):@@ -258,9 +255,7 @@ class Arc(TipableVMobject):[np.cos(a) * RIGHT + np.sin(a) * UPfor a in np.linspace(- self.start_angle,- self.start_angle + self.angle,- self.num_components,+ self.start_angle, self.start_angle + self.angle, self.num_components)])@@ -274,12 +269,7 @@ class Arc(TipableVMobject):# Use tangent vectors to deduce anchorshandles1 = anchors[:-1] + (d_theta / 3) * tangent_vectors[:-1]handles2 = anchors[1:] - (d_theta / 3) * tangent_vectors[1:]- self.set_anchors_and_handles(- anchors[:-1],- handles1,- handles2,- anchors[1:],- )+ self.set_anchors_and_handles(anchors[:-1], handles1, handles2, anchors[1:])def get_arc_center(self, warning=True):"""@@ -301,10 +291,7 @@ class Arc(TipableVMobject):n1 = rotate_vector(t1, TAU / 4)n2 = rotate_vector(t2, TAU / 4)try:- return line_intersection(- line1=(a1, a1 + n1),- line2=(a2, a2 + n2),- )+ return line_intersection(line1=(a1, a1 + n1), line2=(a2, a2 + n2))except Exception:if warning:warnings.warn("Can't find Arc center, using ORIGIN instead")@@ -341,11 +328,7 @@ class ArcBetweenPoints(Arc):arc_height = radius - math.sqrt(radius ** 2 - halfdist ** 2)angle = math.acos((radius - arc_height) / radius) * sign- Arc.__init__(- self,- angle=angle,- **kwargs,- )+ Arc.__init__(self, angle=angle, **kwargs)if angle == 0:self.set_points_as_corners([LEFT, RIGHT])self.put_start_and_end_on(start, end)@@ -410,11 +393,79 @@ class Dot(Circle):class SmallDot(Dot):+ """+ A dot with small radius+ """++ CONFIG = {"radius": DEFAULT_SMALL_DOT_RADIUS}+++class AnnotationDot(Dot):+ """+ A dot with bigger radius and bold stroke to annotate scenes.+ """+CONFIG = {- "radius": DEFAULT_SMALL_DOT_RADIUS,+ "radius": DEFAULT_DOT_RADIUS * 1.3,+ "stroke_width": 5,+ "stroke_color": WHITE,+ "fill_color": BLUE,}+class LabeledDot(Dot):+ """A :class:`Dot` containing a label in its center.++ Parameters+ ----------+ label : Union[:class:`str`, :class:`~.SingleStringMathTex`, :class:`~.Text`, :class:`~.Tex`]+ The label of the :class:`Dot`. This is rendered as :class:`~.MathTex`+ by default (i.e., when passing a :class:`str`), but other classes+ representing rendered strings like :class:`~.Text` or :class:`~.Tex`+ can be passed as well.++ radius : :class:`float`+ The radius of the :class:`Dot`. If ``None`` (the default), the radius+ is calculated based on the size of the ``label``.++ Examples+ --------++ .. manim:: SeveralLabeledDots+ :save_last_frame:++ class SeveralLabeledDots(Scene):+ def construct(self):+ sq = Square(fill_color=RED, fill_opacity=1)+ self.add(sq)+ dot1 = LabeledDot(Tex("42", color=RED))+ dot2 = LabeledDot(MathTex("a", color=GREEN))+ dot3 = LabeledDot(Text("ii", color=BLUE))+ dot4 = LabeledDot("3")+ dot1.next_to(sq, UL)+ dot2.next_to(sq, UR)+ dot3.next_to(sq, DL)+ dot4.next_to(sq, DR)+ self.add(dot1, dot2, dot3, dot4)+ """++ def __init__(self, label, radius=None, **kwargs) -> None:+ if isinstance(label, str):+ from manim import MathTex++ rendered_label = MathTex(label, color=BLACK)+ else:+ rendered_label = label++ if radius is None:+ radius = (+ 0.1 + max(rendered_label.get_width(), rendered_label.get_height()) / 2+ )+ Dot.__init__(self, radius=radius, **kwargs)+ rendered_label.move_to(self.get_center())+ self.add(rendered_label)++class Ellipse(Circle):CONFIG = {"width": 2, "height": 1}@@ -477,10 +528,7 @@ class Annulus(Circle):class Line(TipableVMobject):- CONFIG = {- "buff": 0,- "path_arc": None, # angle of arc specified here- }+ CONFIG = {"buff": 0, "path_arc": None} # angle of arc specified heredef __init__(self, start=LEFT, end=RIGHT, **kwargs):digest_config(self, kwargs)@@ -558,10 +606,7 @@ class Line(TipableVMobject):return np.tan(self.get_angle())def set_angle(self, angle):- self.rotate(- angle - self.get_angle(),- about_point=self.get_start(),- )+ self.rotate(angle - self.get_angle(), about_point=self.get_start())def set_length(self, length):self.scale(length / self.get_length())@@ -601,10 +646,7 @@ class DashedLine(Line):return 1def calculate_positive_space_ratio(self):- return fdiv(- self.dash_length,- self.dash_length + self.dash_spacing,- )+ return fdiv(self.dash_length, self.dash_length + self.dash_spacing)def get_start(self):if len(self.submobjects) > 0:@@ -640,10 +682,7 @@ class TangentLine(Line):class Elbow(VMobject):- CONFIG = {- "width": 0.2,- "angle": 0,- }+ CONFIG = {"width": 0.2, "angle": 0}def __init__(self, **kwargs):VMobject.__init__(self, **kwargs)@@ -727,27 +766,19 @@ class Arrow(Line):def get_default_tip_length(self):max_ratio = self.max_tip_length_to_length_ratio- return min(- self.tip_length,- max_ratio * self.get_length(),- )+ return min(self.tip_length, max_ratio * self.get_length())def set_stroke_width_from_length(self):max_ratio = self.max_stroke_width_to_length_ratioself.set_stroke(- width=min(- self.initial_stroke_width,- max_ratio * self.get_length(),- ),+ width=min(self.initial_stroke_width, max_ratio * self.get_length()),family=False,)return selfclass Vector(Arrow):- CONFIG = {- "buff": 0,- }+ CONFIG = {"buff": 0}def __init__(self, direction=RIGHT, **kwargs):if len(direction) == 2:@@ -773,9 +804,7 @@ class CubicBezier(VMobject):class Polygon(VMobject):- CONFIG = {- "color": BLUE,- }+ CONFIG = {"color": BLUE}def __init__(self, *vertices, **kwargs):VMobject.__init__(self, **kwargs)@@ -820,9 +849,7 @@ class Polygon(VMobject):class RegularPolygon(Polygon):- CONFIG = {- "start_angle": None,- }+ CONFIG = {"start_angle": None}def __init__(self, n=6, **kwargs):digest_config(self, kwargs, locals())@@ -857,9 +884,7 @@ class Rectangle(Polygon):class Square(Rectangle):- CONFIG = {- "side_length": 2.0,- }+ CONFIG = {"side_length": 2.0}def __init__(self, **kwargs):digest_config(self, kwargs)@@ -869,9 +894,7 @@ class Square(Rectangle):class RoundedRectangle(Rectangle):- CONFIG = {- "corner_radius": 0.5,- }+ CONFIG = {"corner_radius": 0.5}def __init__(self, **kwargs):Rectangle.__init__(self, **kwargs)diff --git a/tests/control_data/graphical_units_data/geometry/AnnotationDotTest.npy b/tests/control_data/graphical_units_data/geometry/AnnotationDotTest.npynew file mode 100644index 000000000..8d822d6c4Binary files /dev/null and b/tests/control_data/graphical_units_data/geometry/AnnotationDotTest.npy differdiff --git a/tests/test_graphical_units/test_geometry.py b/tests/test_graphical_units/test_geometry.pyindex 84366097a..86720b603 100644--- a/tests/test_graphical_units/test_geometry.py+++ b/tests/test_graphical_units/test_geometry.py@@ -54,6 +54,12 @@ class DotTest(Scene):self.play(Animation(dot))+class AnnotationDotTest(Scene):+ def construct(self):+ adot = AnnotationDot()+ self.play(Animation(adot))++class EllipseTest(Scene):def construct(self):e = Ellipse()