manim
manim copied to clipboard
Problem determining the table address row
Problem determining the table address row
Describe the bug
Hello, I have a problem. When adding a table header selection, the issue arises when moving the table using the ``
self.play(self.table_binary.animate().shift(UP * 1.5))
element; the table loses its position. Is there a solution to ensure the selection remains attached to the table header?
The element used for selection is
SurroundingRectangle()
Code:
from components.BoxRectangle_ref import BoxRectangle as Box_C
from manim import *
from components import *
class Introduction(Scene):
def construct(self):
self.table_binary = self.table_Bibary().scale(0.5)
# table_decimal = self.table_Decimal().scale(0.5)
# self.table_binary = VGroup(self.table_binary)
subject_decimal = Text(
"Decimal Number System.", font_size=36
) # تحديد حجم النص للتناسق
self.play(Create(self.table_binary))
self.wait(1)
index_row = ValueTracker(0)
Tiltle_Section(self=self, title="جدول النظام الثنائى", scale=0.8)
inner = Text("binary system", color=WHITE, font_size=55, slant=ITALIC)
mob = SurroundingRectangle(inner, corner_radius=0.1, buff=0.3).set_stroke(width=2)
g_title = VGroup(inner, mob).scale(0.5).next_to(self.table_binary, UP * 1.3)
backgroundRectangle1 = BackgroundRectangle(
g_title, color=WHITE, fill_opacity=0.15
)
title = VGroup(g_title, backgroundRectangle1)
self.play(Create(title))
self.wait(1)
header_row = self.table_binary.get_rows()[0]
# أنشئ المستطيل وقم بتحريكه مباشرة إلى مكان الصف بعد النقل
row_bit = SurroundingRectangle(header_row, buff=0.1, color=YELLOW).stretch_to_fit_width(self.table_binary.get_rows()[0].width +0.5)
row_bit.move_to(header_row.get_center()) # ضمان تزامن الموقع بعد الحركة
self.play(Create(row_bit))
self.play(self.table_binary.animate.shift(UP * 1.5))
self.wait(1)
self.play(index_row.animate.set_value(0))
self.wait(2)
def remove_highlight(self):
if hasattr(self, "highlight"):
self.remove(self.highlight)
def highlight_box(self, box_or_cell_table, color=YELLOW):
"""
Highlights a given table cell or box with a surrounding rectangle.
Parameters:
box_or_cell_table: Mobject
Example: table.get_cell((row, col))
color: Manim color
Highlight color (default: YELLOW)
"""
new_highlight = always_redraw(
lambda: SurroundingRectangle(box_or_cell_table, color=color, buff=0)
)
# If highlight already exists, replace it
if hasattr(self, "highlight"):
self.play(ReplacementTransform(self.highlight, new_highlight))
self.play(Create(new_highlight))
self.highlight = new_highlight # Store for future updates
def changeCell(
self,
table: Table,
target_row,
target_col,
new_value=0,
replec=False,
fromReplec: Mobject = None,
):
# , = 3, 6 # Change "B2" to "NEW"
# Get the old cell mobject
old_cell = table.get_entries((target_row, target_col))
# Create a new cell mobject with the new value
new_cell = table.get_cell((target_row, target_col)).copy()
new_text = Tex(new_value, font_size=42, color=GREEN_E)
new_text.move_to(new_cell.get_center())
# Animate replacing the text
table.remove(old_cell)
if replec:
if not fromReplec:
raise ValueError("argm fromReplec is requrde")
self.play(Transform(old_cell, new_text))
self.play(TransformFromCopy(fromReplec, new_text))
self.wait(1)
self.remove(new_text)
else:
self.play(Transform(old_cell, new_text))
def table_Bibary(self) -> DecimalTable:
x_vals = sorted([2, 2, 2, 2, 2, 2, 2, 2], reverse=True)
sx_vals2 = [0] * len(x_vals)
reversed_x_vals2 = list(reversed(sx_vals2))
digit_number_binary = list(reversed([2**x for x in range(len(x_vals))]))
col_labels = [
MathTex(f"{2}^{{{i+1}}}", font_size=84).set_color(RED)
for i in range(len(x_vals))
]
col_labels = reversed(col_labels)
binary_Number_System_table = DecimalTable(
[digit_number_binary, reversed_x_vals2],
row_labels=[MathTex("size"), MathTex("bit")],
col_labels=[*col_labels],
include_outer_lines=True,
element_to_mobject_config={"num_decimal_places": 0, "font_size": 48},
)
# binary_Number_System_table.get_outer_lines().set_stroke(width=2)
# for self.line in binary_Number_System_table.get_family():
# if isinstance(self.line, Line):
# self.line.set_stroke(width=1)
return binary_Number_System_table
def table_Decimal(self) -> DecimalTable:
x_vals = sorted([2, 2, 2, 2, 2, 2, 2, 2], reverse=True)
digit_number_decimal = list(reversed([10**x for x in range(len(x_vals))]))
sx_vals2 = [0] * len(x_vals)
reversed_x_vals2 = list(reversed(sx_vals2))
col_labels_decimal = [
MathTex(f"{10}^{{{i+1}}}", font_size=84).set_color(RED)
for i in range(len(x_vals))
]
col_labels_decimal = reversed(col_labels_decimal)
decimal_Number_System_table = DecimalTable(
[digit_number_decimal, reversed_x_vals2],
row_labels=[MathTex("Place Value"), MathTex("Digit Row")],
col_labels=[*col_labels_decimal],
include_outer_lines=True,
element_to_mobject_config={"num_decimal_places": 0, "font_size": 55},
)
return decimal_Number_System_table
# . القيمة العددية (Place Value)
# 3
# class AND(VGroup):
# def __init__(self, **kwargs):
# super().__init__(**kwargs)
# rect = Rectangle(width=2, height=2)
# arc = Arc(radius=1, angle=PI, start_angle=-PI/2).shift(RIGHT)
# self.add(rect, arc)
Wrong display or Error traceback: