-
Notifications
You must be signed in to change notification settings - Fork 0
/
manimce.py
371 lines (327 loc) · 12.4 KB
/
manimce.py
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
from manim import *
import numpy as np
from scipy import signal
import scipy.stats
import scipy
# basique
## latex
class latex_formules(Scene):
def construct(self):
latex = MathTex(r"\sum_{n=1}^\infty \frac{1}{n^2} = \frac{\pi^2}{6}")
self.play(FadeInFrom(latex))
self.wait()
## identité remarquable (a-b)^2
class IR_amoinsb_2(Scene): # (a-b)^2
def construct(self):
formula = TexMobject(
"(", # 0
"a", # 1
"-", # 2
"b", # 3
")", # 4
"^{2}", # 5
"=", # 6
"a", # 7
"^{2}", # 8
"-", # 9
"2", # 10
"a", # 11
"b", # 12
"+", # 13
"b", # 14
"^{2}" # 15
)
formula.scale(2)
self.play(Write(formula[0:7])) # 0 à 6
formula[7].set_color(RED)
formula[14].set_color(BLUE)
formula[11].set_color(RED)
formula[12].set_color(BLUE)
self.wait()
self.play(
ReplacementTransform(formula[1].copy(), formula[7]),
Write(formula[8]),
ReplacementTransform(formula[2].copy(), formula[9]),
ReplacementTransform(formula[3].copy(), formula[9]),
Write(formula[13]),
ReplacementTransform(formula[3].copy(), formula[14]),
Write(formula[15]),
run_time=2
)
self.wait(1.5)
self.play(
Write(formula[10]),
ReplacementTransform(formula[1].copy(), formula[11]),
ReplacementTransform(formula[3].copy(), formula[12]),
run_time=2
)
self.wait()
## identité remarquable a^2-b^2
class IR_a_b_2(Scene): # a^2-b^2
def construct(self):
formula = Tex(
"a", # 0
"^{2}", # 1
"-", # 2
"b", # 3
"^{2}", # 4
"=", # 5
"(", # 6
"a", # 7
"-", # 8
"b", # 9
")", # 10
"(", # 11
"a", # 12
"+", # 13
"b", # 14
")" # 15
)
formula.scale(2)
self.play(Write(formula[0:6])) # 0 à 5
formula[7].set_color(RED)
formula[9].set_color(BLUE)
formula[14].set_color(RED)
formula[12].set_color(BLUE)
self.wait()
self.play(
ReplacementTransform(formula[0].copy(), formula[7]),
ReplacementTransform(formula[0].copy(), formula[12]),
Write(formula[6]), Write(formula[11]),
Write(formula[10]), Write(formula[15]),
run_time=1.5
)
self.wait(0.5)
self.play(
Write(formula[8]), Write(formula[13]),
ReplacementTransform(formula[3].copy(), formula[9]),
ReplacementTransform(formula[3].copy(), formula[14]),
run_time=1.5
)
self.wait()
## Texte en couleur et entouré
class TextColor(Scene): # f(x)=ax+b
def construct(self):
text = Tex("f(x)", "=", "a", "x", "+", "b")
text[0].set_color(WHITE)
text[1].set_color(WHITE)
text[2].set_color(BLUE)
text[3].set_color(WHITE)
text[4].set_color("#FFFFFF") # Hexadecimal color
text[5].set_color(RED)
# text.to_corner(DL)
frameBoxa = SurroundingRectangle(text[2], buff=0.8 * SMALL_BUFF)
frameBoxa.set_stroke(BLUE, 3)
boxtextea = Tex("{\\normalsize On fait varier a}")
boxtextea.set_color(BLUE, 3)
boxtextea.next_to(text[2].get_center(), UP, buff=0.7)
frameBoxb = SurroundingRectangle(text[5], buff=0.8 * SMALL_BUFF)
frameBoxb.set_stroke(RED)
boxtexteb = Tex("{\\normalsize et b}")
boxtexteb.set_color(RED)
boxtexteb.next_to(text[5].get_center(), UP, buff=0.7)
self.play(Write(text))
self.wait(.1)
self.play(ShowCreation(frameBoxa))
self.play(Write(boxtextea))
self.wait(0.4)
self.remove(frameBoxa)
self.wait(0.1)
self.remove(boxtextea)
self.play(ShowCreation(frameBoxb))
self.play(Write(boxtexteb))
self.wait(0.4)
self.remove(frameBoxb)
self.wait(0.1)
self.remove(boxtexteb)
self.wait(0.5)
## aligner text
class Aligner_text(Scene):
def construct(self):
text1 = Tex("text1").shift(2 * UL) # UpLeft
text2 = Tex("text2")
text3 = Tex("text3").shift(2 * DR) # DownRight
group = VGroup(text1, text2, text3).scale(1.1)
self.add(group)
self.play(group.animate.arrange(RIGHT, .25, center=False))
## ligne couleur gradient
class LigneGradient(Scene):
def construct(self):
line_gradient = Line(LEFT * 4, RIGHT * 4)
line_gradient.set_color(color=[PURPLE, BLUE, YELLOW, GREEN, RED])
self.add(line_gradient)
self.wait()
## deformer carré
class WarpSquare(Scene):
def construct(self):
square = Square()
self.add(square)
self.play(ApplyPointwiseFunction(
lambda point: complex_to_R3(np.exp(R3_to_complex(point))),
square
))
self.wait()
## déplacement disque
class movecircle(Scene):
def construct(self):
sphere = Sphere().set_color(RED)
self.add(sphere)
self.play(ApplyMethod(sphere.shift, UP), run_time=2)
self.play(ApplyMethod(sphere.scale, 0.4), run_time=2)
self.wait(1)
# 2D
## polygone
class polygon(GraphScene):
def construct(self):
self.setup_axes(animate=True)
polyg = [self.coords_to_point(0,0), #P1
self.coords_to_point(0,3.5), #P2
self.coords_to_point(3.5,1.75), #P3
self.coords_to_point(3.5,0), #P4
self.coords_to_point(0,0)] #P1 pour fermer la figure
plol = Polygon(*polyg).move_to(UP+DOWN)
self.play(ShowCreation(plol))
## ligne verticale sous courbe
class Plot_AO(GraphScene):
def construct(self):
self.setup_axes()
self.v_graph = self.get_graph(lambda x: 4 * x - x ** 2, x_min=0, x_max=4)
self.variable_point_label = "x_0"
self.add_T_label(x_val=1)
self.add(self.v_graph)
self.wait()
# 3D
## Animation surfaces
class SurfacesAnimation(ThreeDScene): ####### Surface
def construct(self):
axes = ThreeDAxes()
cylinder = ParametricSurface(
lambda u, v: np.array([
np.cos(TAU * v),
np.sin(TAU * v),
2 * (1 - u)
]),
resolution=(6, 32)).fade(0.5) # Resolution of the surfaces
paraboloid = ParametricSurface(
lambda u, v: np.array([
np.cos(v) * u,
np.sin(v) * u,
u ** 2
]), v_max=TAU,
checkerboard_colors=[PURPLE_D, PURPLE_E],
resolution=(10, 32)).scale(2)
para_hyp = ParametricSurface(
lambda u, v: np.array([
u,
v,
u ** 2 - v ** 2
]), v_min=-2, v_max=2, u_min=-2, u_max=2, checkerboard_colors=[BLUE_D, BLUE_E],
resolution=(15, 32)).scale(1)
cone = ParametricSurface(
lambda u, v: np.array([
u * np.cos(v),
u * np.sin(v),
u
]), v_min=0, v_max=TAU, u_min=-2, u_max=2, checkerboard_colors=[GREEN_D, GREEN_E],
resolution=(15, 32)).scale(1)
hip_one_side = ParametricSurface(
lambda u, v: np.array([
np.cosh(u) * np.cos(v),
np.cosh(u) * np.sin(v),
np.sinh(u)
]), v_min=0, v_max=TAU, u_min=-2, u_max=2, checkerboard_colors=[YELLOW_D, YELLOW_E],
resolution=(15, 32))
ellipsoid = ParametricSurface(
lambda u, v: np.array([
1 * np.cos(u) * np.cos(v),
2 * np.cos(u) * np.sin(v),
0.5 * np.sin(u)
]), v_min=0, v_max=TAU, u_min=-PI / 2, u_max=PI / 2, checkerboard_colors=[TEAL_D, TEAL_E],
resolution=(15, 32)).scale(2)
sphere = ParametricSurface(
lambda u, v: np.array([
1.5 * np.cos(u) * np.cos(v),
1.5 * np.cos(u) * np.sin(v),
1.5 * np.sin(u)
]), v_min=0, v_max=TAU, u_min=-PI / 2, u_max=PI / 2, checkerboard_colors=[RED_D, RED_E],
resolution=(15, 32)).scale(2)
self.set_camera_orientation(phi=75 * DEGREES)
self.begin_ambient_camera_rotation(rate=0.2)
self.add(axes)
self.play(Write(sphere))
self.wait()
self.play(ReplacementTransform(sphere, ellipsoid))
self.wait()
self.play(ReplacementTransform(ellipsoid, cone))
self.wait()
self.play(ReplacementTransform(cone, hip_one_side))
self.wait()
self.play(ReplacementTransform(hip_one_side, para_hyp))
self.wait()
self.play(ReplacementTransform(para_hyp, paraboloid))
self.wait()
self.play(ReplacementTransform(paraboloid, cylinder))
self.wait()
self.play(FadeOut(cylinder))
# SVG
## stickman
class SVGStickMan(GraphScene, MovingCameraScene):
def construct(self):
start_man = SVGMobject("/Users/antoninlefevre/Downloads/ManimCE/SVG_files/stick_man_plain.svg").set_color(WHITE)
start_man_2 = SVGMobject("/Users/antoninlefevre/Downloads/ManimCE/SVG_files/stick_man_plain.svg").set_color(WHITE)
wave_man = SVGMobject("/Users/antoninlefevre/Downloads/ManimCE/SVG_files/stick_man_wave.svg").set_color(WHITE)
wave_man_2 = SVGMobject("/Users/antoninlefevre/Downloads/ManimCE/SVG_files/stick_man_wave.svg").set_color(WHITE)
base = SVGMobject("/Users/antoninlefevre/Downloads/ManimCE/SVG_files/stick_man_plain.svg").set_color(WHITE)
self.camera.frame.save_state()
self.add(start_man.move_to(2*LEFT))
self.add(start_man_2.move_to(2*RIGHT))
self.play(self.camera.frame.animate.move_to(start_man.get_top()).scale(0.4))
self.wait(0.5)
salut = Tex("Salut !").set_color(YELLOW).next_to(start_man, UP+LEFT, buff=0.5)
self.play(Transform(start_man, wave_man.move_to(2*LEFT)), FadeIn(salut))
self.wait()
self.play(self.camera.frame.animate.move_to(start_man_2.get_top()))
self.wait(0.5)
salut_2 = Tex("Hey !").set_color(YELLOW).next_to(start_man_2, UP+RIGHT, buff=0.5)
self.play(Transform(start_man_2, wave_man_2.move_to(2*RIGHT)), FadeIn(salut_2))
self.play(FadeOut(salut), FadeOut(salut_2), run_time=0.01)
self.play(Transform(start_man, base.move_to(2 * LEFT)), Transform(start_man_2, base.copy().move_to(2 * RIGHT)))
self.play(Restore(self.camera.frame))
## Zoom sur un neurone
class ZoomOnNeuron(ZoomedScene, MovingCameraScene):
def __init__(self, **kwargs):
ZoomedScene.__init__(
self,
zoom_factor=0.4,
zoomed_display_height=3,
zoomed_display_width=4,
image_frame_stroke_width=15,
zoomed_camera_config={
"default_frame_stroke_width": 2,
},
**kwargs
)
def construct(self):
# image du neurone
neurone_1 = SVGMobject("/Users/antoninlefevre/Downloads/ManimCE/SVG_files/neuron_video.svg").set_color(WHITE).scale(3).rotate(PI/2)
self.play(Write(neurone_1))
# texte du zoom
#zoomed_camera_text = Text("Dendrites", color=WHITE).scale(1.1) # box qui suit le zoom
zoomed_camera = self.zoomed_camera
zoomed_display = self.zoomed_display
frame = zoomed_camera.frame
zoomed_display_frame = zoomed_display.display_frame
frame.move_to(neurone_1[0].get_left())
frame.set_color(PURPLE)
# placer le cadre du zoom
zd_rect = BackgroundRectangle(zoomed_display.move_to(neurone_1[0].get_left()+DOWN), fill_opacity=0, buff=MED_SMALL_BUFF)
self.add_foreground_mobject(zd_rect)
unfold_camera = UpdateFromFunc(zd_rect, lambda rect: rect.replace(zoomed_display))
self.play(ShowCreation(frame), direction=DOWN)
self.activate_zooming()
self.play(self.get_zoomed_display_pop_out_animation(), unfold_camera)
self.wait(2)
# dezoom et fin de l'animation
self.play(self.get_zoomed_display_pop_out_animation(), unfold_camera, rate_func=lambda t: smooth(1 - t))
self.play(Uncreate(zoomed_display_frame), FadeOut(frame))
self.wait()