-
Notifications
You must be signed in to change notification settings - Fork 18
/
Test.py
1960 lines (1800 loc) · 64.7 KB
/
Test.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from manimlib.imports import *
from manim_projects.tony_useful.imports import *
from random import randint
'''
这个文件中是群友问问题时我写的测试代码(2020.02.03开始)
一些目的和效果已经通过文档字符串的形式给出
'''
class Test0(Scene):
def construct(self):
circle = Circle(radius=3)
poly = []
for i in range(3, 11):
po = Polygon(
*[
UP * np.sin(j * 2 * PI / i) + RIGHT * np.cos(j * 2 * PI / i)
for j in range(i)
]
)
poly.append(po.scale(3, about_point=ORIGIN))
self.play(ShowCreation(circle))
self.play(ShowCreation(poly[0]))
self.wait()
for i in range(1, 8):
self.play(Transform(poly[0], poly[i]))
self.wait()
self.wait(2)
class Test1(Scene):
'''Matrix类中间元素的下标布局'''
def construct(self):
mat = Matrix([['0', '-1', '2'], ['1', '0', '12'], ['3', '2', 'x']])
self.add(mat)
debugTeX(self, mat[0])
class Test2(Scene):
'''使用\tt调TextMobject打字机字体'''
def construct(self):
text = VGroup(
TextMobject("\\tt UR=np.array([ 1, 1, 0])", tex_to_color_map={"=":RED, "array":BLUE}),
TextMobject("\\tt UL=np.array([-1, 1, 0])", tex_to_color_map={"=":RED, "array":BLUE}),
TextMobject("\\tt DR=np.array([ 1,-1, 0])", tex_to_color_map={"=":RED, "array":BLUE}),
TextMobject("\\tt DL=np.array([-1,-1, 0])", tex_to_color_map={"=":RED, "array":BLUE})
).arrange_submobjects(DOWN)
self.add(text)
class Test3(Scene):
'''坐标可以用ndarray,也可以用列表'''
def construct(self):
l = Line([0, 0, 0], [3, 3, 0])
self.add(l)
class Test4(Scene):
'''aligned_edge的用法'''
def construct(self):
sq1 = Square().shift(LEFT * 2)
sq2 = Square().next_to(sq1.get_corner(DR), DOWN)
sq3 = Square().shift(RIGHT * 2)
sq4 = Square().next_to(sq3.get_corner(DR), DOWN, aligned_edge=LEFT)
self.add(sq1, sq2, sq3, sq4)
class Test5(Scene):
'''加号强制next_to对齐'''
def construct(self):
text = TextMobject("LOVE\\ DEATH\\ ", "$+$", "\\ ROBOTS", color=RED)
text[1].next_to(text[0], RIGHT)
text[2].next_to(text[1], RIGHT)
self.add(text)
class Test6(Scene):
'''FocusOn和Flash的动画效果'''
def construct(self):
title1 = TextMobject("FocusOn").scale(2).to_corner(UL)
self.add(title1)
dot = Dot(radius=0.5, color=BLUE)
self.play(ShowCreation(dot))
self.wait()
self.play(FocusOn(dot))
self.wait(2)
title2 = TextMobject("Flash").scale(2).to_corner(UL)
self.play(Transform(title1, title2))
self.wait()
self.play(Flash(dot, flash_radius=0.55))
self.wait(3)
class Test7(Scene):
'''白底黑字'''
def construct(self):
txt = TexMobject("0",
fill_color=BLACK,
fill_opacity=1.0,
stroke_color=BLACK,
stroke_opacity=1.0,
).scale(3)
self.add(txt)
class Test8(Scene):
'''使用Rectangle或者Line来强制Brace宽度'''
def construct(self):
rec = Rectangle(width=4)
brac = Brace(rec, DOWN)
self.add(brac)
class Test9(ThreeDScene):
'''立方体三维旋转'''
def construct(self):
self.set_to_default_angled_camera_orientation()
cube = Cube()
self.add(cube)
self.wait()
self.play(Rotating(cube, axis=UP, radians=PI / 6))
self.wait(2)
class Test10(Scene):
'''文字渐变色'''
def construct(self):
text = TextMobject("test").scale(2).set_color_by_gradient(BLUE, RED)
self.add(text)
class Test11(Scene):
'''LaTeX的cases可行'''
def construct(self):
text = TexMobject(
r"""
\begin{cases}
u^3+v^3=-q\\
uv=-\frac{p}{3}\\
\end{cases}
"""
)
self.add(text)
class Test12(Scene):
def construct(self):
circle0 = Circle(color=WHITE,radius=2)
text0 = TextMobject("Gaussian \\\\ Elimination")
vec1 =Vector(1.4*LEFT).move_to(circle0.get_center()+2.8*LEFT)
circle1 = Circle(color=RED,radius=1.6).next_to(vec1, LEFT)
text1 = TextMobject("System of \\\\ linear equation").move_to(circle1.get_center()+ORIGIN).scale(0.8)
vgr1 = VGroup(text1, circle1)
self.add(circle0, text0)
self.add(vec1)
self.add(vgr1)
self.wait(2)
pos = Dot(fill_opacity=0).move_to(circle1.get_center())
def update_text(obj):
obj.move_to(pos)
vgr1.add_updater(update_text)
self.add(vgr1)
self.play(
Rotating(vec1, radians = 6 * PI, about_point = ORIGIN, axis = IN),
Rotating(pos , radians = 6 * PI, about_point = ORIGIN, axis = IN),
run_time=20
)
class Test13(Scene):
'''Uncreate效果,注意不是UnCreate'''
def construct(self):
sq = Square()
self.add(sq)
self.wait()
self.play(Uncreate(sq))
self.wait()
class Test14(Scene):
def construct(self):
rec1 = Rectangle(height=2, width=6)
rec2 = Rectangle(height=1, width=1).shift(LEFT*2)
rec3 = Rectangle(height=1, width=1).shift(RIGHT*2)
rec4 = Rectangle(height=1, width=1)
recs = VGroup(rec1, rec2, rec3, rec4)
self.add(recs)
self.wait()
self.play(recs.shift, UP*2.5)
self.wait()
circle = Circle(radius=0.5).move_to(rec3)
self.play(Transform(rec3, circle))
self.wait()
class Test15(GraphScene):
'''GraphScene的坐标轴可以FadeOut'''
def construct(self):
self.setup_axes(animate=True)
self.wait()
self.play(FadeOut(self.axes))
self.wait()
class Test16(Scene):
def construct(self):
objs = [
Square().shift(LEFT * 3),
Square(),
Square().shift(RIGHT * 3)
]
self.add(*objs)
self.wait()
self.play(
*[
ApplyMethod(obj.shift, UP)
for obj in objs
]
)
self.wait()
class Test17(Scene):
'''使用index_of_submobject_to_align来对齐,注意要get_center()'''
def construct(self):
vg1 = VGroup(
Circle(radius = 0.5).shift(LEFT*2),
Circle(radius = 0.5).shift(LEFT*1),
Circle(radius = 0.5),
Circle(radius = 0.5).shift(RIGHT*1),
Circle(radius = 0.5).shift(RIGHT*2),
)
vg2 = VGroup(
Square(side_length=1).shift(LEFT*1),
Square(side_length=1),
Square(side_length=1).shift(RIGHT*1),
)
vg2.next_to(vg1[3].get_center(), DOWN, index_of_submobject_to_align=1)
self.add(vg1, vg2)
class Test18(Scene):
'''使用tex[0]来对TexMobject的每个字符进行分解'''
def construct(self):
tex = TexMobject("a^2+b^2=c^2")
self.add(tex)
debugTeX(self, tex[0])
class Test19(Scene):
'''用AnimatedBoundary实现Line的颜色变化'''
def construct(self):
l = Line(LEFT * 3, RIGHT * 3)
self.add(l)
self.wait()
l2 = AnimatedBoundary(l, colors=[BLUE])
self.add(l2)
self.wait(3)
class Test20(Scene):
'''使用set_opacity实现闪烁效果'''
def construct(self):
text = TextMobject("颓废最不要脸")
self.add(text)
for i in range(20):
self.play(text.set_opacity, 0, run_time=0.2)
self.play(text.set_opacity, 1, run_time=0.2)
self.wait()
class Test21(Scene):
'''圆弧flip的默认轴'''
def construct(self):
grid = NumberPlane()
arc = Arc(0, PI / 2, color = BLUE)
arc2 = arc.copy().flip().set_color(YELLOW)
self.add(grid, arc, arc2)
class Test22(Scene):
def construct(self):
text = TextMobject("abcd")
self.add(text)
class Test23(Scene):
'''move_arc_center_to和不同run_time的动画同时播放'''
def construct(self):
sq = Square(side_length=4)
ci = Arc(0, PI / 2, color=BLUE, radius=4).move_arc_center_to(sq.get_corner(DL))
self.wait()
self.play(ShowCreation(sq, run_time=2), ShowCreation(ci, run_time=4))
self.wait()
class Test24(Scene):
'''环绕每个字符'''
def construct(self):
text = TextMobject("abcdefgh")
rec = VGroup()
for i in text[0]:
rec.add(SurroundingRectangle(i, buff=0))
self.add(text, rec)
class Test25(Scene):
'''使用LaTeX的表格'''
def construct(self):
tab = TextMobject(
r"""
\begin{table}[]
\begin{tabular}{|l|l|l|l|l|l|}
\hline
a & b & c & d & e & f \\ \hline
\end{tabular}
\end{table}
"""
)
self.add(tab)
debugTeX(self, tab[0])
class Test26(Scene):
'''Succession,其实和多个play没什么区别'''
def construct(self):
group = VGroup(
Circle(radius = 0.5).shift(LEFT*2),
Circle(radius = 0.5).shift(LEFT*1),
Circle(radius = 0.5),
Circle(radius = 0.5).shift(RIGHT*1),
Circle(radius = 0.5).shift(RIGHT*2),
).set_opacity(0)
self.wait()
self.play(
Succession(
*[
ApplyMethod(obj.set_opacity, 1)
for obj in group
]
)
)
self.wait()
class Test27(Scene):
'''UP和TOP在to_corner时的区别'''
def construct(self):
text = TextMobject("to\_corner UP").to_corner(UP)
text2 = TextMobject("to\_corner TOP").to_corner(TOP) # 非标准用法
text3 = TextMobject("move\_to TOP").move_to(TOP).set_color(YELLOW)
self.add(text, text2, text3)
class Test28(Scene):
'''将所有物体都FadeOut,没有add的物体也不会强制add再FadeOut'''
def construct(self):
sq = Square()
ci = Circle()
self.add(sq)
self.wait()
self.play(
*[
FadeOut(obj)
for obj in self.mobjects
]
)
self.wait()
class Test29(Scene):
'''Text不会自动换行'''
def construct(self):
text = Text("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", font="Consolas")
self.add(text)
class Test30(Scene):
'''字符上弧'''
def construct(self):
text = TextMobject("\\overarc{AB}")
self.add(text)
class Test31(Scene):
def construct(self):
Gc = VGroup()
colors = color_gradient([BLACK, WHITE], 9)
Gc.add(Square(side_length=1).shift(LEFT*6).set_fill(colors[0], 1).set_color(colors[0]))
for i in range(1, 9):
Gc.add(Gc[-1].copy().set_fill(colors[i], 1).set_color(colors[i]).shift(RIGHT*1.2))
self.play(Transform(Gc[-2], Gc[-1], rate_func=linear))
self.wait()
class Test32(GraphScene):
'''get_graph必须在setup_axes之后'''
def construct(self):
self.setup_axes(animate=True)
graph = self.get_graph(lambda x : x**2,
color = GREEN,
x_min = None,
x_max = None
)
self.play(
ShowCreation(graph),
run_time = 2
)
self.wait()
class Test33(Scene):
'''用颜色灰度来实现透明度的效果,防止两透明度颜色相叠加,导致亮度突变'''
def construct(self):
colors = color_gradient(["#6C6C00", YELLOW], 9)
sq = Square(side_length=1).shift(LEFT*6).set_fill(colors[0], 1).set_color(colors[0])
for i in range(1, 9):
self.play(
ApplyMethod(sq.shift, RIGHT * 1.2, rate_func=linear),
ApplyMethod(sq.set_color, colors[i]),
ApplyMethod(sq.set_fill, colors[i], 1)
)
self.wait()
class Test34(ThreeDScene):
'''cube的面'''
def construct(self):
self.set_to_default_angled_camera_orientation()
cube = Cube(fill_opacity=0, stroke_width=3).set_fill(opacity=0).set_color(WHITE)
cube[5].set_color(BLUE)
self.add(cube)
debugTeX(self, cube)
class Test35(GraphScene):
'''使用updater来实现graph的更新'''
def construct(self):
self.setup_axes()
line = self.get_graph(lambda x: x + 2)
val = ValueTracker(1)
line.add_updater(lambda m: m.become(self.get_graph(lambda x: val.get_value() * x + 2, color=BLUE)))
self.add(line)
self.play(val.increment_value, 4)
self.wait()
class Test36(ThreeDScene):
'''抛物面'''
def construct(self):
self.set_to_default_angled_camera_orientation()
颓废曲面 = ParametricSurface(
lambda u, v: [u, v, u ** 2 + v ** 2],
u_min=-1, u_max=1, v_min=-1, v_max=1
)
self.add(颓废曲面)
class Test37(Scene):
'''Transfrom前统一方向,使动画更顺滑'''
def construct(self):
ci = Circle()
# sq = Square()
sq = Square().flip()
self.play(ShowCreation(ci))
self.play(Transform(ci, sq))
self.wait()
class Test38(Scene):
'''根式上色'''
def construct(self):
text = TexMobject("\\sqrt{x^2+y^2+z^2}")
text[0][2:4].set_color(RED)
self.add(text)
debugTeX(self, text[0])
class Test39(Scene):
'''上色'''
def construct(self):
text4 = TexMobject(
r"ds=\vert d\vec r \vert=",
r"\sqrt{x^2+y^2+z^2}"
)
VGroup(
text4
).set_color(YELLOW)
VGroup(
text4[1][2:4]
).set_color(RED)
self.add(text4)
debugTeX(self, text4[1])
class Test40(Scene):
'''一个self.play中无法处理两个针对同一物体的ApplyMethod,但不加ApplyMethod可以'''
def construct(self):
dot = Dot(color=BLUE)
up = Dot(color=YELLOW).to_edge(UP)
self.add(dot)
self.wait()
# self.play(
# ApplyMethod(dot.next_to, up, DOWN),
# ApplyMethod(dot.scale, 3)
# )
self.play(
dot.next_to, up, DOWN,
dot.scale, 3
)
self.wait()
class Test41(Scene):
'''replace的作用'''
def construct(self):
sq = Square().scale(2)
ci = Circle().shift(RIGHT*3)
self.add(sq, ci)
self.play(sq.replace, ci)
self.wait()
class Test42(Scene):
'''使用updater时不能使用循环变量i'''
def construct(self):
ups = VGroup(
*[
Dot(color=BLUE).move_to([i, 1, 0])
for i in range(-3, 4)
]
)
downs = VGroup(
*[
Dot(color=YELLOW).move_to([i, -1, 0])
for i in range(-3, 4)
]
)
lines = VGroup(
*[
Line(ups[i], downs[i])
for i in range(0, 7)
]
)
lines.add_updater(
lambda m: m.become(
VGroup(
*[
Line(ups[i], downs[i])
for i in range(0, 7)
]
)
)
)
# for i in range(7):
# lines[i].add_updater(lambda m: m.put_start_and_end_on(ups[i].get_bottom(), downs[i].get_top()))
self.add(ups, downs, lines)
self.wait()
self.play(
ups.shift, LEFT * 2
)
self.play(
downs.shift, RIGHT * 2
)
self.wait()
class Test43(Scene):
'''和Test40同理'''
def construct(self):
dot = Dot(color=BLUE)
self.add(dot)
self.wait()
self.play(
ApplyMethod(dot.scale, 3), # 这个被淹没了
ApplyMethod(dot.set_color, YELLOW)
)
self.wait()
class Test44(ThreeDScene):
def construct(self):
axes = ThreeDAxes()
spheres = VGroup(
*[
Sphere(radius=i, opacity=0.5, resolution=(20, 40))
for i in np.arange(1, 3.1, 0.4)
]
)
self.set_to_default_angled_camera_orientation()
self.add(axes)
old = VGroup()
new = VGroup()
for i in range(len(spheres[0])):
old.add(spheres[randint(1, 5)][i].set_opacity(randint(1, 6) / 10))
new.add(spheres[0][i])
self.wait()
self.wait()
self.play(
FadeIn(old),
*[
Transform(i, j)
for i, j in zip(old, new)
],
run_time=6
)
self.wait()
class Test45(ThreeDScene):
def construct(self):
axes = ThreeDAxes()
self.set_to_default_angled_camera_orientation()
self.add(axes)
surface = ParametricSurface(
lambda y, z: [
-np.sqrt(
1 - 9 * y ** 2 / 4 + (320 * 2 ** (1 / 3) * z ** 3) / ((
99532800 * y ** 2 * z ** 2 + 884736000 * z ** 3 - \
1990656000 * y ** 2 * z ** 3 - 884736000 * z ** 5 + np.sqrt(
(-115964116992000000 * z ** 9 + (99532800 * y ** 2 * z ** 2 + \
884736000 * z ** 3 - 1990656000 * y ** 2 * z ** 3 - 884736000 * z ** 5) ** 2) ** 2)
) ** (1 / 3)) + (1 / 960 * 2 ** (1 / 3)) * (99532800 * y ** 2 * z ** 2 + \
884736000 * z ** 3 - 1990656000 * y ** 2 * z ** 3 - 884736000 * z ** 5 + np.sqrt(
(-115964116992000000 * z ** 9 + (99532800 * y ** 2 * z ** 2 + 884736000 * z ** 3 -\
1990656000 * y ** 2 * z ** 3 - 884736000 * z ** 5) ** 2)
)) ** (1 / 3)
),
y, z
]
)
self.add(surface)
class Test46(Scene):
'''Brace'''
def construct(self):
text = TextMobject("test")
brace = Brace(text, DOWN)
self.play(Write(brace))
self.play(Write(text))
class Test47(Scene):
'''LaTeX的dancers小人,需要下载字体包并且更改ctex_template'''
def construct(self):
Test = VGroup()
for i in range(51):
test = TextMobject("\\Pisymbol{dancers}{%d}" % i, stroke_width=1, fill_opacity=1, stroke_opacity=1).scale(200)
Test.add(test)
self.wait()
self.play(Write(Test[0]))
for i in range(1, 51):
self.wait(0.8)
self.play(Transform(Test[0], Test[i]))
self.wait(2)
class Test48(Scene):
'''plot_depth'''
CONFIG = {
"camera_config": {"use_plot_depth": True}
}
def construct(self):
sq = Square(stroke_width=5).set_plot_depth(1)
sq2 = Square(side_length=1, stroke_width=5).shift(RIGHT).set_color(BLUE).set_plot_depth(0)
self.add(sq, sq2)
self.wait()
self.play(sq2.set_plot_depth, 2)
self.wait()
class Test49(Scene):
'''使用LaTeX的lstlisting写代码,需要改ctex_template'''
def construct(self):
text = TextMobject("""
\\begin{lstlisting}
int main() {
}
\\end{lstlisting}
""")
self.add(text)
class Test50(ThreeDScene):
'''正劈锥体,渲染贼慢'''
def construct(self):
axes = ThreeDAxes()
self.set_camera_orientation(phi=70 * DEGREES, theta=45 * DEGREES)
self.add(axes)
a = VGroup()
b = VGroup()
c = VGroup()
for i in np.arange(-1, 1.00001, 0.0005):
tri = Polygon([i, np.sqrt(1 - i ** 2), 0],
[i, -np.sqrt(1 - i ** 2), 0],
[i, 0, 2], stroke_width=0, fill_color=BLUE, fill_opacity=0.75)
a.add(tri)
cnt = 1
self.begin_ambient_camera_rotation(rate=0.5)
for tri in a:
if cnt % 2 == 0:
self.add(tri.set_fill(color=YELLOW, opacity=0.5))
self.wait(0.01)
tri.set_fill(color=BLUE, opacity=0.75)
else:
self.add(tri)
cnt += 1
self.wait(5)
class Test51(ThreeDScene):
'''棱锥到近似圆锥'''
def construct(self):
axes = ThreeDAxes()
self.set_camera_orientation(phi=70 * DEGREES, theta=45 * DEGREES)
self.add(axes)
circle = Circle(radius=2)
polys = []
faces = []
for i in range(3, 16):
po = Polygon(
*[
UP * np.sin(j * 2 * PI / i) + RIGHT * np.cos(j * 2 * PI / i)
for j in range(i)
], stroke_width=1, stroke_color=BLUE, fill_color=BLUE, fill_opacity=0.75
).scale(2, about_point=ORIGIN)
polys.append(po)
verts = po.get_vertices()
faces_ = VGroup()
for j in range(i):
if j == i - 1:
face = Polygon(verts[j], verts[0], [0, 0, 3])
else:
face = Polygon(verts[j], verts[j + 1], [0, 0, 3])
face.set_stroke(width=1, color=BLUE)
face.set_fill(color=BLUE, opacity=0.75)
faces_.add(face)
faces.append(faces_)
self.play(ShowCreation(circle))
self.play(ShowCreation(polys[0]), ShowCreation(faces[0]))
self.wait()
self.begin_ambient_camera_rotation(rate=0.5)
self.wait()
for i in range(1, 13):
self.play(
Transform(polys[0], polys[i]),
Transform(faces[0], faces[i])
)
self.wait()
self.wait(2)
class Test52(SpecialThreeDScene):
'''Boxes类的test'''
CONFIG = {
"default_angled_camera_position": {
"phi": 70 * DEGREES,
"theta": -45 * DEGREES,
"distance": 50,
},
}
def construct(self):
self.set_camera_to_default_position()
axes = self.get_axes()
boxes = MyBoxes(fill_color=GRAY, resolution=(20, 20), bottom_size=(0.25, 0.25), gap=0.05)
self.var_phi = 0
func_01 = lambda x, y: np.sin(x ** 2 / 2.4 + y ** 2 / 2.4 + self.var_phi) * 1.
func_02 = lambda x, y: np.sin(x ** 2 / 2.4 + y ** 2 / 2.4 + self.var_phi) * 1. - 0.25
boxes.update_top_and_bottom_by_func(func_01, func_02)
boxes.update_color_by_func(func_01)
def update_boxes(b, dt):
b.update_top_and_bottom_by_func(func_01, func_02)
b.update_color_by_func(func_01)
self.var_phi += 1 * DEGREES
self.add(boxes)
boxes.add_updater(update_boxes)
# self.wait(2)
# boxes.remove_updater(update_boxes)
self.wait(12)
class Test53(ThreeDScene):
'''MyBoxes的序号分布'''
def construct(self):
axes = ThreeDAxes()
# self.set_camera_orientation(phi=70 * DEGREES, theta=225 * DEGREES)
self.add(axes)
boxes = MyBoxes(fill_color=GRAY, resolution=(9, 18), bottom_size=(0.5, 0.7), gap=0.2, box_height=0.5)
self.add(boxes)
debugTeX(self, boxes)
class Test54(ThreeDScene):
'''测试元素周期表'''
CONFIG = {
"camera_config": {
"background_color": WHITE,
"should_apply_shading": False,
}
}
def construct(self):
self.set_camera_orientation(phi=50 * DEGREES, theta=240 * DEGREES, distance=50)
boxes = ChemicalBoxes(fill_color=BLUE_E).add_label().set_block_color()
self.add(boxes)
self.begin_ambient_camera_rotation(rate=1)
# self.wait(10)
class Test55(Scene):
'''无法像这样获取圆上某一方向的点'''
def construct(self):
ci = Circle()
self.add(ci)
dot = Dot().move_to(ci.get_boundary_point(UP * 2 * np.sqrt(5) / 5 + RIGHT * np.sqrt(5) / 5))
self.add(dot)
class Test56(Scene):
'''带dt的updater'''
def construct(self):
dot = Dot().to_edge(UP)
dot.add_updater(lambda m, dt: m.shift(0.1 * DOWN))
self.add(dot)
self.wait(6)
class Test57(Scene):
'''文字上下标'''
def construct(self):
text = TextMobject("正文A$_{\\text{下标B}}^{\\text{上标C}}$").scale(3)
self.add(text)
class Test58(Scene):
'''rate_func'''
def construct(self):
func = ParametricFunction(
lambda x: [x, smooth(x), 0],
t_min=0, t_max=1
).scale(3, about_point=ORIGIN)
self.add(func)
class Test59(Scene):
'''save_image'''
def construct(self):
sq = Square()
sq.save_image()
self.add(sq)
class Test60(Scene):
'''根据等号对齐'''
def construct(self):
tex1 = TexMobject("A=\\frac{\\displaystyle\\sum^n_{i=0}}{x}")
tex2 = TexMobject("=", "\\frac{x}{\\displaystyle\\sum^n_{i=0}}")
tex2.next_to(tex1, RIGHT)
tex2.next_to(tex1[0][1].get_center(), RIGHT, index_of_submobject_to_align=0, coor_mask=np.array([0, 1, 1]))
self.add(tex1, tex2)
texs = [
"A=\\frac{\\displaystyle\\sum^n_{i=0}}{x}",
"=\\frac{x}{\\displaystyle\\sum^n_{i=0}}"
]
tex = TexMobject(*texs)
self.add(tex)
class Test61(Scene):
def construct(self):
for1 = TexMobject(r"G(x)=\displaystyle\sum_{p=0}^{\infty}{\left( \frac{S^{p}(n)}{p!}x^p\right)}").scale(0.7).to_edge(UP+LEFT)
for1_bg = SurroundingRectangle(for1, fill_opacity = .2)
for2 = TexMobject(r"G(x) = \left( \frac{e^{(n+1)x}-1}{x} \right) \left( \frac{x}{e^x-1} \right)")
forrs = [
r"\frac{e^{(n+1)x}-1}{x}", # for3
r"= \frac{ \left( \displaystyle\sum_{p=0}^{\infty}{\frac{{((n+1)x)}^p}{p!}} \right) -1}{x}}", #for4
r"=\frac{1+\left( \displaystyle\sum_{p=1}^{\infty}{\frac{{((n+1)x)}^p}{p!}} \right) -1}{x}}",#for5
r"=\displaystyle\sum_{p=1}^{\infty}{\frac{(n+1)^p}{p!}x^{p-1}}",#for6
r"=\displaystyle\sum_{p=0}^{\infty}{\frac{(n+1)^{p+1}}{(p+1)!}x^{p}}"#for7
]
forr = TexMobject(*forrs).scale(0.9)
self.add(forr)
class Test62(Scene):
'''三角形绕边翻转'''
def construct(self):
tri = Triangle()
vert = tri.get_vertices()
tri.generate_target()
tri.target.flip(axis=vert[0]-vert[1], about_point=(vert[0]+vert[1])/2)
self.add(tri)
self.wait()
self.play(MoveToTarget(tri))
self.wait()
class Test63(Scene):
'''文字渐变色的区别'''
def construct(self):
vg = VGroup(
TextMobject("abcde").set_color([RED, BLUE, WHITE]),
TextMobject("abcde").set_color_by_gradient(RED, BLUE, WHITE),
TextMobject("abcde")
).arrange(DOWN)
vg[2].shuffle(True)
vg[2].set_color_by_gradient(RED, BLUE, WHITE)
self.add(vg)
class Test64(Scene):
'''CubicBezier的points只有四个点,即锚点和控制点,但ParametricFunction是好多贝塞尔曲线,好多点'''
def construct(self):
# line = CubicBezier([np.array([ -3, -1.5, 0]), np.array([-3.6, 1.5, 0]), np.array([ 0, 1.5, 0]), np.array([ 3, -1.5, 0])])
line = ParametricFunction(
bezier([np.array([ -3, -1.5, 0]), np.array([-3.6, 1.5, 0]), np.array([ 0, 1.5, 0]), np.array([ 3, -1.5, 0])]),
t_min=0, t_max=1
)
self.add(line)
points = line.get_points()
debugTeX(self, points)
class Test65(Scene):
'''渐变色的方向,用sheen_direction来设定'''
def construct(self):
sq = Square()
sq.set_color([RED, BLUE])
# sq.set_opacity([0, 1])
# sq.set_fill([RED, BLUE], [0, 1])
sq.set_sheen_direction(UP)
self.add(sq)
# self.wait()
# self.play(sq.flip)
# self.wait()
class Test66(Scene):
'''digest_config的很愚蠢的用法'''
CONFIG = {
"stroke_width": 15,
}
def construct(self):
line = Line()
digest_config(line, self.CONFIG)
self.add(line)
class Test67(Scene):
'''arc的points,用好多贝塞尔曲线来拟合的'''
def construct(self):
arc = Arc().scale(3)
self.add(arc)
points = arc.get_points()
debugTeX(self, points, 0.3)
class Test68(Scene):
def construct(self):
tex = TexMobject("{\\sin\\alpha\\over\\sin\\gamma}={n_1\\over n_2}")
self.add(tex)
debugTeX(self, tex[0])
class Test69(ThreeDScene):
'''无法将三维物体Transform到fixed_in_frame_mobjects的二维物体,但可以通过z_to_vector等变换得到类似的效果'''
def construct(self):
self.set_camera_orientation(phi=60*DEGREES, theta=45*DEGREES)
vec = [
np.cos(45*DEGREES) * np.sin(60*DEGREES),
np.sin(45*DEGREES) * np.sin(60*DEGREES),
np.cos(60*DEGREES)
]
n = z_to_vector(vec)
tex = TexMobject("a").apply_matrix(n).rotate(PI/2, vec)
# self.camera.add_fixed_in_frame_mobjects(tex)
# tex.to_corner(UL)
surface = Cube()
self.add(surface)
self.play(Transform(surface, tex), run_time=2)
self.wait()
class Test70(Scene):
'''无法通过get_points获取TexMobject的点'''
def construct(self):
tex = TexMobject("S").scale(2)
self.add(tex)
p = tex.get_points()
print(p)
class Test71(Scene):
def construct(self):
grid = NumberPlane()
vector = np.array([1, 2, 0])
matrix = np.identity(3) - np.outer(vector, vector)
self.add(grid, Dot([1, 2, 0], color=RED))
self.wait()
self.play(grid.apply_matrix, matrix, run_time=3)
self.wait()
class Test72(Scene):
'''光源'''
def construct(self):
light = AmbientLight()
self.add(light)
class Test73(Scene):
'''running_start的写法是六次贝塞尔曲线'''
def construct(self):
grid = NumberPlane().scale(3)
func = ParametricFunction(
lambda x: [x, running_start(x), 0],
t_min=0, t_max=1
).scale(3, about_point=ORIGIN)
func2 = ParametricFunction(
bezier([
np.array([0/6, 0, 0]),
np.array([1/6, 0, 0]),
np.array([2/6, -0.5, 0]),
np.array([3/6, -0.5, 0]),
np.array([4/6, 1, 0]),
np.array([5/6, 1, 0]),
np.array([6/6, 1, 0]),
]),
t_min=0, t_max=1, color=RED
).scale(3, about_point=ORIGIN)
self.add(grid, func, func2)
class Test74(Scene):
'''幼儿园小练习1'''
CONFIG = {
"camera_config": {
"use_plot_depth": True,
},
}
def setup(self):
self.A = np.array([1, 0, 0])
self.B = np.array([-1, 0, 0])
self.C = np.array([-0.3, 1.3, 0])
self.main_tri = Polygon(
self.A, self.B, self.C,
color=BLUE, fill_color=BLUE, fill_opacity=0.8
)
label_a = TexMobject("a").scale(0.7).next_to((self.B+self.C)/2, UL, buff=0.08)
label_b = TexMobject("b").scale(0.7).next_to((self.A+self.C)/2, UR, buff=0.08)
label_c = TexMobject("c").scale(0.7).next_to((self.B+self.A)/2, DOWN, buff=0.08)
self.labels = VGroup(label_a, label_b, label_c).set_plot_depth(5)
sq_a = Polygon(self.B, self.C, np.array([-1.6, 2, 0]), np.array([-2.3, 0.7, 0]), color=WHITE)
sq_b = Polygon(self.C, self.A, np.array([2.3, 1.3, 0]), np.array([1, 2.6, 0]), color=WHITE)
sq_c = Polygon(self.A, self.B, np.array([-1, -2, 0]), np.array([1, -2, 0]), color=WHITE)
self.sq = VGroup(sq_a, sq_b, sq_c).set_plot_depth(-1)
tri_a = Polygon(self.A, np.array([1, -2, 0]), np.array([2.3, 1.3, 0]), color=RED, fill_color=RED, fill_opacity=0.8)
tri_b = Polygon(self.B, np.array([-2.3, 0.7, 0]), np.array([-1, -2, 0]), color=YELLOW, fill_color=YELLOW, fill_opacity=0.8)
tri_c = Polygon(self.C, np.array([1, 2.6, 0]), np.array([-1.6, 2, 0]), color=GREEN, fill_color=GREEN, fill_opacity=0.8)
self.tri = VGroup(tri_a, tri_b, tri_c)
equation = TexMobject("S_{\\ } = S_{\\ } = S_{\\ } = S_{\\ }").scale(1.5).to_corner(UR, buff=1.1)