def bordered_group( self, *children: Mobject, border_attrs: Optional[Dict] = None, title: Optional[str] = None, title_attrs: Optional[Dict] = None, ): group = VGroup(*children) width = ( abs(max(child.get_x(RIGHT) for child in children) - min(child.get_x(LEFT) for child in children)) + 1.2 ) height = abs(max(child.get_y(UP) for child in children) - min(child.get_y(DOWN) for child in children)) + 1.2 rect = Rectangle(**_merge(border_attrs, width=width, height=height)) rect.move_to(group.get_center_of_mass()) group.add_to_back(rect) if title: label = self.text_box( title, **_merge(title_attrs, bg_color=BLACK, border_color=BLACK, rounded=False, shadow=False) ) label.scale(0.8) label.move_to(group.get_top()) group.add(label) return group
def construct(self): expr2 = self.expr.subs(e, math.e) f = lambdify(x, expr2, 'numpy') domains = continuous_domain(self.expr, x, Interval(self.x_min, self.x_max)) if type(domains) is Union: domains = domains.args else: domains = [domains] self.setup_axes(animate=True) func_graph = VGroup() for domain in domains: graph = self.get_graph(f, self.function_color, get_left_bound(domain), get_right_bound(domain)) func_graph.add(graph) graph_lab = self.get_graph_label(func_graph[0], label=latex(self.expr)) self.play(ShowCreation(func_graph, run_time=3)) self.play(ShowCreation(graph_lab)) self.wait(5)
def __init__(self, diagram: SequenceDiagram, title: str): super().__init__() self.diagram = diagram self.title = title tblock = VGroup() title = Text(title, font=self.CONFIG["text_font"], color=self.CONFIG["actor_text_color"]) rect = (Rectangle(height=self._get_text_height(title) + 0.5, width=title.get_width() + 0.5).round_corners(0.2).set_fill( self.CONFIG["actor_fill_color"], 1)) title.move_to(rect) tblock.add(rect, title) self.block = tblock self.line = DashedLine( start=rect.get_edge_center(DOWN), end=[rect.get_center()[0], rect.get_bottom()[1], 0], stroke_style="dashed", dash_length=DEFAULT_DASH_LENGTH * 4, stroke_width=DEFAULT_STROKE_WIDTH / 2, positive_space_ratio=0.7, ) self.bblock = tblock.copy() self.bblock.next_to(self.line, direction=DOWN, buff=0) self.add(tblock, self.line, self.bblock)
def test_vgroup_item_assignment_at_correct_position(): """Test VGroup item-assignment adds to correct position for VMObjects""" n_items = 10 vgroup = VGroup() for _i in range(n_items): vgroup.add(VMobject()) new_obj = VMobject() vgroup[6] = new_obj assert vgroup[6] == new_obj assert len(vgroup) == n_items
def test_vgroup_item_assignment_at_correct_position(using_opengl_renderer): """Test VGroup item-assignment adds to correct position for OpenGLVMObjects""" n_items = 10 vgroup = VGroup() for _i in range(n_items): vgroup.add(OpenGLVMobject()) new_obj = OpenGLVMobject() vgroup[6] = new_obj assert vgroup[6] == new_obj assert len(vgroup) == n_items
def construct(self): data = [20, 0, 0, -5] x = [0, 8, 38, 39] self.setup_axes() dot_collection = VGroup() for time, val in enumerate(data): dot = Dot().move_to(self.coords_to_point(x[time], val)) self.add(dot) dot_collection.add(dot) l1 = Line(dot_collection[0].get_center(), dot_collection[1].get_center()) l2 = Line(dot_collection[1].get_center(), dot_collection[2].get_center()) l3 = Line(dot_collection[2].get_center(), dot_collection[3].get_center()) self.add(l1, l2, l3)
def __init__(self, target: Actor, label: str, direction: np.array): super().__init__(target) self.target = target self.label = "\n".join(wrap(label, 30)) self.direction = direction block = VGroup() title = Text(self.label, font="Helvetica").scale(0.7) rect = Rectangle(height=title.get_height() + 0.3, width=title.get_width() + 0.3) title.move_to(rect) block.add(rect, title) block.next_to(target.get_center(), direction) self.add(block)
def test_vgroup_add(): """Test the VGroup add method.""" obj = VGroup() assert len(obj.submobjects) == 0 obj.add(VMobject()) assert len(obj.submobjects) == 1 with pytest.raises(TypeError): obj.add(Mobject()) assert len(obj.submobjects) == 1 with pytest.raises(TypeError): # If only one of the added object is not an instance of VMobject, none of them should be added obj.add(VMobject(), Mobject()) assert len(obj.submobjects) == 1 with pytest.raises(ValueError): # a Mobject cannot contain itself obj.add(obj)
def test_vgroup_add(using_opengl_renderer): """Test the VGroup add method.""" obj = VGroup() assert len(obj.submobjects) == 0 obj.add(OpenGLVMobject()) assert len(obj.submobjects) == 1 with pytest.raises(TypeError): obj.add(OpenGLMobject()) assert len(obj.submobjects) == 1 with pytest.raises(TypeError): # If only one of the added object is not an instance of OpenGLVMobject, none of them should be added obj.add(OpenGLVMobject(), OpenGLMobject()) assert len(obj.submobjects) == 1 with pytest.raises(ValueError): # a OpenGLMobject cannot contain itself obj.add(obj)
def __init__(self, source: Actor, label: str): super().__init__(source) self.label = "\n".join(wrap(label, 30)) line_block = VGroup() spacing = 0.4 distance = 0.8 center_x = source.get_center()[0] line = Polygon( [center_x, spacing, 0], [center_x + distance, spacing, 0], [center_x + distance, -1 * spacing, 0], [center_x + distance / 2, -1 * spacing, 0], [center_x + distance, -1 * spacing, 0], [center_x + distance, spacing, 0], [center_x, spacing, 0], color=WHITE, ) line.set_stroke(width=ARROW_STROKE_WIDTH) arrow = Arrow( start=[center_x + distance, -1 * spacing, 0], end=[center_x, -1 * spacing, 0], buff=0, stroke_width=ARROW_STROKE_WIDTH, ) line_block.add(line, arrow) title = Text(self.label, font=self.source.font, size=0.5, slant=ITALIC) title.next_to(line_block) block = VGroup() block.add(line_block, title) block.next_to(source.get_center(), RIGHT) self.add(block)
def _box( self, parent: VGroup, bg_color, color, rounded, shadow, text, wrap_at, border_color, text_attrs: Optional[dict], border_builder: Callable[[Text], Polygon], ) -> VGroup: if wrap_at: text = "\n".join(wrap(text, wrap_at)) title = Text(text, font=self.text_font, color=color, **(text_attrs if text_attrs else {})) border = border_builder(title) border.set_color(border_color) bg_color, bg_opacity = self._color_and_opacity(bg_color, text) border.set_fill(color=bg_color, opacity=bg_opacity) if rounded: border.round_corners(ROUNDED_RADIUS) title.move_to(border) parent.add(border, title) if shadow and bg_opacity: s_rect = border.copy() s_rect.set_color(SHADOW_COLOR) shadow_opacity = SHADOW_OPACITY s_rect.set_stroke(width=0) s_rect.set_background_stroke(color=GREEN, opacity=shadow_opacity, width=0) s_rect.set_fill(opacity=shadow_opacity) s_rect.scale(1 + SHADOW_SHIFT) s_rect.shift(SHADOW_SHIFT * DR) parent.add_to_back(s_rect) return parent
def __init__(self, target: Actor, label: str): super().__init__(target) self.target = target self.label = "\n".join(wrap(label, 30)) line_block = VGroup() spacing = 0.4 distance = 0.8 line = Polygon( [target.get_center()[0], spacing, 0], [target.get_center()[0] + distance, spacing, 0], [target.get_center()[0] + distance, -1 * spacing, 0], [target.get_center()[0] + distance / 2, -1 * spacing, 0], [target.get_center()[0] + distance, -1 * spacing, 0], [target.get_center()[0] + distance, spacing, 0], [target.get_center()[0], spacing, 0], color=WHITE, ) line.set_stroke(width=ARROW_STROKE_WIDTH) arrow = Arrow( start=[target.get_center()[0] + distance, -1 * spacing, 0], end=[target.get_center()[0], -1 * spacing, 0], buff=0, stroke_width=ARROW_STROKE_WIDTH, ) line_block.add(line, arrow) title = Text(self.label, font="Helvetica", size=0.7, slant=ITALIC) title.next_to(line_block) block = VGroup() block.add(line_block, title) block.next_to(target.get_center(), RIGHT) self.add(block)