-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfracbin_scene.py
More file actions
251 lines (186 loc) · 8.92 KB
/
Copy pathfracbin_scene.py
File metadata and controls
251 lines (186 loc) · 8.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
from manim import *
import numpy as np
# --- CONFIGURATION ---
# Change these values to alter the scene's dimensions
SCENE_WIDTH = 14.22 # MUnits
SCENE_HEIGHT = 8 # MUnits
GRID_STEP = 0.5 # The spacing for the finer grid lines and labels
class FractionToBinary(MovingCameraScene):
def construct(self):
#self.grade()
self.intro()
self.operacao()
def grade(self):
# 1. Set up camera based on configuration
self.camera.frame_width = SCENE_WIDTH
self.camera.frame_height = SCENE_HEIGHT
# 2. Define dynamic ranges based on scene dimensions
# The grid will extend from the center to the edges of the frame
x_axis_range = np.floor(SCENE_WIDTH / 2)
y_axis_range = np.floor(SCENE_HEIGHT / 2)
# 3. Define visual properties
grid_color = GRAY
grid_stroke_width = 1.0
label_font_size = 12
# 4. Create the grid that adapts to the scene size
# This NumberPlane will act as our comprehensive grid
grid = NumberPlane(
x_range=(-x_axis_range, x_axis_range, GRID_STEP),
y_range=(-y_axis_range, y_axis_range, GRID_STEP),
x_length=SCENE_WIDTH,
y_length=SCENE_HEIGHT,
background_line_style={
"stroke_color": grid_color,
"stroke_width": grid_stroke_width,
},
axis_config={"stroke_opacity": 0} # Hide default axes
)
# 5. Create dynamic labels for the grid
# The labels are generated based on the calculated ranges
# Create labels for the x-axis
x_labels = VGroup()
for i in np.arange(-x_axis_range, x_axis_range + GRID_STEP, GRID_STEP):
if i != 0: # Avoid label at origin
x_labels.add(
Text(str(i), font_size=label_font_size)
.next_to(grid.c2p(i, 0), DOWN, buff=0.1)
)
# Create labels for the y-axis
y_labels = VGroup()
for i in np.arange(-y_axis_range, y_axis_range + GRID_STEP, GRID_STEP):
if i != 0: # Avoid label at origin
y_labels.add(
Text(str(i), font_size=label_font_size)
.next_to(grid.c2p(0, i), LEFT, buff=0.1)
)
# 6. Add all elements to the scene
self.add(grid, x_labels, y_labels)
self.wait(2)
def intro(self):
titulo = Text("Formação de dízima na conversão de fracionário decimal para binário", font_size=28)
sublinhado1 = Underline(titulo,color=RED,stroke_width=6)
self.play(Write(titulo), runtime = 0.3)
self.play(Create(sublinhado1), runtime = 0.3)
self.wait(2)
self.clear()
def retangulo(self, grupo_p, grupo_aux, ite,tempo):
cor = RED if ite >= 4 else YELLOW
retangulo = SurroundingRectangle(grupo_p[2:], color=cor, buff=0.2)
retangulo_aux = SurroundingRectangle(grupo_aux[2:], color=cor, buff=0.2)
if ite == -1:
self.play(Create(retangulo), runtime = 0.05)
explanation_text = Text("Multiplica apenas a parte fracionária", font_size = 16)
explanation_text.next_to(retangulo, RIGHT, buff=0.8)
arrow = Arrow(start=retangulo.get_right(), end=explanation_text.get_left(), color=cor,buff=0.1)
self.play(Create(arrow), Write(explanation_text), runtime = 0.2)
self.play(FadeOut(retangulo), FadeOut(arrow), FadeOut(explanation_text), runtime = 0.2)
elif ite == 4:
self.play(Create(retangulo), Create(retangulo_aux), runtime = 0.05)
explanation_text = Text("Se há repetição da parte fracionária, haverá formação de dízima", font_size = 16)
explanation_text.next_to(retangulo, RIGHT, buff=0.8)
arrow = Arrow(start=retangulo.get_right(),end=explanation_text.get_left(), color=cor,buff=0.1 )
self.play(Create(arrow), Write(explanation_text), runtime = 0.2)
self.wait(0.3)
self.play(FadeOut(retangulo), FadeOut(retangulo_aux), FadeOut(arrow), FadeOut(explanation_text), runtime = 0.2)
elif ite > 4:
self.play(Create(retangulo), Create(retangulo_aux), runtime =tempo)
self.play(FadeOut(retangulo), FadeOut(retangulo_aux), runtime = tempo)
else:
self.play(Create(retangulo), runtime = 0.05)
self.play(FadeOut(retangulo), runtime = 0.05)
def operacao(self):
# Valor inicial (parte fracionária)
num = 0.1
fractional_part = num
# O número decimal completo para a primeira linha
current_num = fractional_part
# Inicia a posição vertical para a primeira linha da operação
y_position = 2.5
# Primeira linha
line1_text = Text(f"{current_num:.1f}", font_size = 20).move_to(np.array([-5.5, y_position, 0]))
self.play(Write(line1_text), run_time=0.2)
self.wait(1)
grupo = VGroup(line1_text)
self.retangulo(line1_text, grupo, -1, 0)
num_ite = 0
tempo = 0.1
y = 0
MAX_ITERATIONS = 10 # Limita o loop para 10 iterações para evitar vídeos longos
# Loop para a multiplicação sucessiva enquanto a parte fracionária for diferente de zero
while num_ite <= MAX_ITERATIONS:
line1_text = Text(f"{current_num:.1f}", font_size = 20).move_to(np.array([-5.5, y_position, 0]))
foco = line1_text
# Símbolo de multiplicação (×) e o multiplicador (2)
multiplier_text = Text("× 2", font_size = 20).next_to(line1_text, DOWN, buff=0.25, aligned_edge= LEFT)
# Linha de separação (resultado)
line_separator = Line(LEFT, RIGHT).next_to(line1_text, DOWN, buff=0.8)
# O resultado da multiplicação
result = current_num * 2
# Formata o resultado para exibição
# Separa o bit binário (parte inteira) com cor diferente (amarelo)
result_text = Text(f"{result:.1f}", font_size = 20)
result_text.next_to(line_separator, DOWN, buff=0.25)
result_text.align_to(line1_text, LEFT)
# Agrupa os mobjects desta etapa para animar e posicionar
current_step_group = VGroup(
multiplier_text,
line_separator,
result_text
)
# Grupo com os resultados
grupo.add(result_text)
for linha in current_step_group:
self.play(Write(linha), run_time=tempo)
linha_aux = num_ite -3 if num_ite >= 4 else 0
self.retangulo(current_step_group[2], grupo[linha_aux], num_ite, tempo)
# 1. MOVIMENTO DA CÂMERA: Desloca o quadro da câmera para baixo
#self.play(self.camera.frame.animate.move_to(foco))
self.play(
self.camera.frame.animate.shift(np.array([0, y, 0])),
run_time= 0.05
)
# Atualiza o valor para o próximo loop (apenas a parte fracionária)
parte_int = int(result)
fractional_part = result - parte_int
current_num = fractional_part
y_position -= 1.25
num_ite += 1
tempo = tempo**(2) if num_ite >=4 else tempo
y = y - 0.2
continua = Text(f'...', font_size = 28)
continua.next_to(result_text, DOWN, buff=0.4)
self.play(Write(continua), run_time=0.5)
self.wait(1)
self.play(self.camera.frame.animate.shift(np.array([0, 11, 0])), run_time=0.6)
self.play(
# Encadear .scale() e .shift() no mesmo .animate
self.camera.frame.animate.scale(2).shift(np.array([6, -4, 0])),
run_time=0.6
)
self.wait(2)
self.resultado(grupo)
def resultado(self, grupo):
seta = Arrow(
end= np.array([-3.5, -12 , 0]),
start= np.array([-3.5, 2.5, 0]), # Cria um ponto (0.01 UP)
color=GREEN,
buff=0.1
)
self.play(Create(seta))
conclusao = Text(f'0.1₁₀ =', font_size = 32)
conclusao.next_to(seta, RIGHT, buff=4)
conclusao.align_to(seta, UP)
self.play(Write(conclusao), run_time=0.5)
resposta = Text('0.00011001100₂...', font_size = 32)
resposta.next_to(conclusao, RIGHT, buff=0.2)
i = 0
for linha in grupo:
retangulo = SurroundingRectangle(linha[0], color= GREEN, buff=0.1)
self.play(Create(retangulo), run_time=0.005)
self.play(Write(resposta[i]), run_time=0.005)
if i == 0:
self.play(Write(resposta[i + 1]), run_time=0.005)
self.play(FadeOut(retangulo))
i = i + 1 if i != 0 else i + 2
self.play(Write(resposta[-4:]), run_time=0.005)
self.wait(3)