-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathradians.py
1591 lines (1264 loc) · 63.7 KB
/
radians.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 manim import *
# for scenes 'Circles0to6Rad', 'RadianExplanation101' and 'EndAnimation' in manim CE v0.11.0
# navigate to /manim/mobject/geometry.py
# comment out line 142 "self.reset_endpoints_based_on_top(tip, at_start)"
# for scenes that render slowly due to updaters, modify the following code
# manim>Mobject>types>vectorized_mobject.py>VMobject>align_points
# for n in range(n_subpaths):
# sp1 = get_nth_subpath(subpaths1, n)
# sp2 = get_nth_subpath(subpaths2, n)
# diff1 = max(0, (len(sp2) - len(sp1)) // nppcc)
# diff2 = max(0, (len(sp1) - len(sp2)) // nppcc)
# if max(len(sp1), len(sp2)) < 350:
# sp1 = self.insert_n_curves_to_point_list(diff1, sp1)
# sp2 = self.insert_n_curves_to_point_list(diff2, sp2)
# new_path1 = np.append(new_path1, sp1, axis=0)
# new_path2 = np.append(new_path2, sp2, axis=0)
config.tex_template = TexTemplate()
segoe_template = TexTemplate(
tex_compiler="xelatex",
output_format=".xdv",
preamble=r"""
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{fontspec}
\setmainfont{Segoe UI Light}
\usepackage[defaultmathsizes]{mathastext}
""")
ANIM_ORANGE = "#C55A11"
ANIM_BLACK = "#404040"
ANIM_YELLOW = "#FFC000"
ANIM_BLUE = "#41719C"
ANIM_GREEN = "#A9D18E"
ANIM_AQUA = "#42A996"
ANIM_PURPLE = "#CC0099"
TEXT_COLOR = "#463622"
def get_background():
"""Returns a gradient rectangle to be used as the background."""
background = Rectangle(
width=config["frame_width"],
height=config["frame_width"],
stroke_width=0,
fill_color=color.color_gradient(["#D5D4C9","#DBD7CF"], 32), #astroparche_light
#fill_color=color.color_gradient(["#ECECEC", "#F2F2F2"], 32), #whiteboard_modern
fill_opacity=1, z_index=-1000)
return background
class RadianCircle():
"""An updating circle, labels and objects."""
@classmethod
def get_circle_and_objs(
self,
radius: float = 1,
label_radius: int = 1,
use_letters: bool = False,
coords: np.array = ORIGIN,
tracker: ValueTracker = ValueTracker(0),
show_angle_label: bool = True,
x_tracker: ValueTracker = None,
radius_tracker: ValueTracker = None,
simplified: bool = False,
segment_color: str = ANIM_ORANGE,
circle_stroke_width: float = 3
):
"""Returns a circle with extra objects.
Parameters
-----------
radius : float
The radius of the circle.
label_radius : int
The radius to be shown in a label.
use_letters : bool
Whether to use letters or not for the equations, radius & distance labels.
coords : np.array
The position of the circle.
tracker : ValueTracker
The ValueTracker to track the current angle.
show_angle_label : bool
Whether to show or not the equation above the circle that displays the angle.
x_tracker : ValueTracker
The ValueTracker to track the x coordinate and move everything accordingly.
radius_tracker : ValueTracker
The ValueTracker to track the circle's radius.
simplified : bool
If set to True, returns only the circle, fixed and rotating segment and center dot.
segment_color : str
The color of the segments forming the angle.
circle_stroke_width : float
The stroke width of the circle.
Returns
-----------
VGroup
A group of all the objects.
"""
def get_distance():
return str(round(label_radius * tracker.get_value(), 1))
def get_angle_label():
distance = float(get_distance())
if use_letters and distance <= label_radius:
theta = r"{{\theta}} \, = \, "
else:
theta = r"\theta\,=\,"
if use_letters:
if distance <= label_radius:
return MathTex(theta + r"{ {{s}} \over {{r}} }").set_color(TEXT_COLOR)
tmp_frac = MathTex(theta + r"{", "6.28", r"r \over r}").set_color(TEXT_COLOR)
text_rad = DecimalNumber(tracker.get_value(), num_decimal_places=2).move_to(tmp_frac[1]).set_color(TEXT_COLOR)
return VGroup(tmp_frac[0], text_rad, tmp_frac[2])
tmp_frac = MathTex(theta, r"{", str(float(label_radius)), r"\over " + str(label_radius) + r"} \, = ")
text_distance = DecimalNumber(distance, num_decimal_places=1).move_to(tmp_frac[2])
text_rad = DecimalNumber(tracker.get_value(), num_decimal_places=2).next_to(tmp_frac, buff=0.2)
return VGroup(tmp_frac[:2], text_distance, tmp_frac[3], text_rad).set_color(TEXT_COLOR)
def get_distance_label():
label = None
if use_letters:
if float(get_distance()) <= label_radius:
label = MathTex("s")
else:
label = VGroup(DecimalNumber(round(tracker.get_value(), 2), num_decimal_places=2),
MathTex("r")).arrange(buff=0.05)
label[-1].align_to(label[0], DOWN)
else:
dist = float(get_distance())
label = DecimalNumber(dist, num_decimal_places=1)
label.scale(0.5).set_color(ANIM_ORANGE)
label.move_to(Arc(radius=radius_func()+0.3, angle=tracker.get_value(), arc_center=center()).point_from_proportion(0.5))
return label if tracker.get_value() > 0.5 else VMobject()
center = lambda: coords
if x_tracker is not None:
center = lambda: (x_tracker.get_value(), 0, 0)
radius_func = (lambda: radius) if not radius_tracker else (lambda: radius_tracker.get_value())
initial_angle = tracker.get_value()
circle = Circle(radius=radius_func(), color=ANIM_BLACK, stroke_width=circle_stroke_width).move_to(center()).add_updater(
lambda m: m.become(Circle(radius=radius_func(), color=ANIM_BLACK, stroke_width=circle_stroke_width)).move_to(center())
)
fixed_segment = Line(start=center(), end=circle.get_right(), color=segment_color).add_updater(
lambda m: m.become(Line(start=center(), end=circle.get_right(), color=segment_color))
)
rotating_segment = Line(
start=center(),
end=(center()[0] + np.cos(initial_angle)*radius_func(), center()[1] + np.sin(initial_angle)*radius_func(), 0),
color=segment_color).add_updater(
lambda m: m.become(
Line(
start=center(),
end=(center()[0] + np.cos(tracker.get_value())*radius_func(), center()[1] + np.sin(tracker.get_value())*radius_func(), 0),
color=segment_color))
)
center_dot = Dot(circle.get_center(), radius=DEFAULT_DOT_RADIUS/2, color=segment_color).add_updater(lambda m: m.move_to(center()))
if simplified:
return VGroup(circle, fixed_segment, rotating_segment, center_dot)
theta = MathTex(r"\theta", color=ANIM_ORANGE).scale(0.5).move_to(circle.get_right())
theta.add_updater(lambda m: m.move_to(
Angle(
fixed_segment, rotating_segment, radius=0.1 + 3 * SMALL_BUFF, other_angle=False
).point_from_proportion(0.5)
)if tracker.get_value() != 0 else lambda m: m)
theta.add_updater(lambda m: m.set_opacity(tracker.get_value()*2))
dot = Dot(circle.get_right(), color=ANIM_ORANGE).add_updater(lambda m: m.move_to(circle.get_right()))
arc_arrow = Arc(radius=radius_func(), angle=tracker.get_value(), color=ANIM_ORANGE, arc_center=center()).add_updater(
lambda m: m.become(Arc(radius=radius_func(), angle=tracker.get_value(), color=ANIM_ORANGE, arc_center=center()).add_tip(tip_length=0.1)
if tracker.get_value() > 0.1 else Arc(radius=radius_func(), angle=tracker.get_value(), color=ANIM_ORANGE, arc_center=center()))
)
label_radius_tex = MathTex("r \ = \ " + str(label_radius) if not use_letters else "r", color=ANIM_ORANGE).scale(0.43).add_updater(
lambda m, dt: m.next_to(fixed_segment, DOWN), call_updater=True
)
label_distance = get_distance_label().add_updater(lambda m: m.become(get_distance_label()))
group = VGroup(circle, fixed_segment, rotating_segment, theta, dot, center_dot, arc_arrow, label_radius_tex, label_distance)
if not show_angle_label:
return group
angle_label = get_angle_label().set_color(ANIM_BLACK).scale(0.6).next_to(circle, UP, buff=0.6).add_updater(lambda m: m.become(
get_angle_label().set_color(ANIM_BLACK).scale(0.6).next_to(circle, UP, buff=0.6)
))
group.add(angle_label)
return group
class Compass():
"""A compass with two perpendicular double arrows and labels."""
@classmethod
def create_compass(self, labels=None, coords=ORIGIN, rotation=0):
"""Returns a compass.
Parameters
-----------
labels : list
A list of the labels to be used. They will be placed in the following order: UP, DOWN, LEFT, RIGHT
coords : np.array
The position of the compass.
rotation : float
The angle to rotate the compass and labels.
Returns
-----------
VGroup
A group of the compass and labels.
"""
arrows = VGroup(*[
DoubleArrow(start=LEFT*1.3, end=RIGHT*1.35, tip_length=.2, color=ANIM_BLACK, stroke_width=6, z_index=-10).rotate(
PI/2 if i else 0)
for i in range(2)])
arrows.move_to(coords)
for a in arrows:
for tip in a.get_tips():
tip.z_index=-10
if labels is None:
return arrows.rotate(rotation)
labels_text = VGroup()
for text, coord in zip(labels, [UP, DOWN, LEFT, RIGHT]):
label = Text(text, color=ANIM_BLACK, stroke_width=1).scale(0.5).next_to(arrows.get_edge_center(coord), coord)
labels_text.add(label)
compass = VGroup(arrows, labels_text).rotate(rotation)
return compass
class Timer():
"""A timer with animating functionality."""
@classmethod
def create_timer(self, seconds=5):
"""Creates the timer.
Parameters
-----------
seconds : int
The amount of seconds of the timer.
Returns
-----------
VGroup
A group containing the circle and numbers.
"""
numbers = [Text(str(i), color=TEXT_COLOR, font="Segoe UI Light").scale(0.4) for i in range(seconds+1)]
numbers.reverse()
circle = Circle(radius=max(numbers[0].height, numbers[0].width)*1.5, color=ANIM_ORANGE, stroke_width=2).to_corner(DL).rotate(PI/2)
for i in numbers:
i.move_to(circle.get_center())
return VGroup(VGroup(*numbers), circle)
@classmethod
def animate(self, renderer, timer):
"""Animates the timer counting down. The timer should already be on screen before
calling this method.
Parameters
-----------
renderer : Scene
The Scene where the timer will be animated.
timer : Timer
The timer to be animated.
Returns
-----------
None
"""
global time_passed
time_passed = 0
numbers = timer[0]
circle = timer[1]
def get_number(dt):
global time_passed
index = min(int(time_passed), len(numbers) - 1)
time_passed += dt
return numbers[index]
numbers[0].add_updater(lambda m, dt: m.become(get_number(dt)))
renderer.play(Uncreate(circle, rate_func=lambda t: 1-t), run_time=5)
numbers[0].clear_updaters()
numbers[0].become(numbers[-1])
renderer.play(FadeOut(numbers[0]), run_time=0.5)
class TicksAndLabelsFromCircle():
"""A set of ticks/dashes and labels constructed around a circle."""
@classmethod
def create_ticks_and_labels(self, circle, label_angles, inner_labels, outer_labels, full_turn_labels, scale_factor=0.4):
"""Returns the ticks and labels from a passed circle.
Parameters
-----------
circle : Circle
The circle to place the ticks and labels around.
label_angles : list
A list of angles where a tick and labels should be placed.
innner_labels : list
A list of tex strings to make a label positioned inside the circle.
outer_labels : list
A list of tex strings to make a label positioned outside the circle.
full_turn_labels : tuple
The two labels (tex strings) that represent a full turn. The inner one should go first. For example: 360° or 2pi.
scale_factor : float
The scale factor for the labels.
Returns
-----------
tuple
A tuple containing the ticks, inner labels and outer labels.
"""
p = circle.get_center()
r = circle.radius - 0.06
ticks = VGroup()
inner_labels_tex = VGroup()
outer_labels_tex = VGroup()
for i, (a, inner, outer) in enumerate(zip(label_angles, inner_labels, outer_labels)):
tick = Line(stroke_width=2, color=ANIM_BLACK)
tick.set_length(0.15).rotate(a)
tick.move_to((p[0] + np.cos(a) * r, p[1] + np.sin(a) * r, 0))
ticks.add(tick)
inner_label = MathTex(inner, tex_template=segoe_template, color=TEXT_COLOR).scale(scale_factor)
direction = DOWN*round(np.sin(a), 10)+LEFT*round(np.cos(a), 10)
inner_label.next_to(tick.get_start(), direction, aligned_edge=direction, buff=0.23)
inner_labels_tex.add(inner_label)
outer_label = MathTex(outer, tex_template=segoe_template, color=TEXT_COLOR).scale(scale_factor)
direction = UP*round(np.sin(a), 10)+RIGHT*round(np.cos(a), 10)
outer_label.next_to(tick, direction, aligned_edge=ORIGIN, buff=0.1)
outer_labels_tex.add(outer_label)
inner_full_turn, outer_full_turn = [
MathTex(i, tex_template=segoe_template, color=TEXT_COLOR).scale(scale_factor)
for i in full_turn_labels
]
inner_full_turn.next_to(inner_labels_tex[0], DOWN, buff=0.1).align_to(inner_labels_tex[0], RIGHT)
outer_full_turn.next_to(outer_labels_tex[0], DOWN, buff=0.1).align_to(outer_labels_tex[0], LEFT)
VGroup(inner_full_turn, inner_labels_tex[0]).next_to(ticks[0], LEFT, buff=0.15)
VGroup(outer_full_turn, outer_labels_tex[0]).next_to(ticks[0], RIGHT, buff=0.15)
inner_labels_tex.add(inner_full_turn)
outer_labels_tex.add(outer_full_turn)
return (ticks, inner_labels_tex, outer_labels_tex)
class GridCompass(Scene):
def construct(self):
buff = 1.7
vertical_lines = VGroup(*[Line(UP*3.2, DOWN*3.2, color=ANIM_BLACK) for _ in range(4)]).arrange(buff=buff)
horizontal_lines = VGroup(*[Line(LEFT*3.2, RIGHT*3.2, color=ANIM_BLACK) for _ in range(4)]).arrange(DOWN, buff=buff)
grid = VGroup(vertical_lines, horizontal_lines)
red_dot = Dot(point=(-buff*1.5, -buff*1.5, 0), radius=DEFAULT_DOT_RADIUS * 3, color=ANIM_ORANGE, z_index=1000)
green_dot = Dot(point=(buff*1.5, buff*1.5, 0), radius=DEFAULT_DOT_RADIUS * 3, color=ANIM_AQUA, z_index=1000)
fifth = Tex("5th", color=TEXT_COLOR).scale(0.5).next_to(red_dot, LEFT, buff=0.75).shift(UP*0.01)
roosevelt = Tex("Roosevelt", color=TEXT_COLOR).scale(0.5).next_to(red_dot, DOWN, buff=0.6)#.shift(LEFT*0.1)
eighth = Tex("8th", color=TEXT_COLOR).scale(0.5).next_to(green_dot, RIGHT, buff=0.6)#.shift(UP*0.2)
columbus = Tex("Columbus", color=TEXT_COLOR).scale(0.5).next_to(green_dot, UP, buff=0.5).shift(UP*0.1)
grid_objs = VGroup(grid, green_dot, red_dot, fifth, eighth, roosevelt, columbus)
shift_factor = LEFT*2
compass1 = Compass.create_compass(labels=["N", "S", "W", "E"], coords=RIGHT*4)
vectors = VGroup()
for i in range(6):
k = int(i/2) if i != 1 else 3
direction_h = [(buff*(k-1.5), buff*(k-1.5), 0), (buff*(k-0.5), buff*(k-1.5), 0)]
direction_v = [(buff*(k-1.5), buff*(k-2.5), 0), (buff*(k-1.5), buff*(k-1.5), 0)]
direction_to_use = direction_h if i % 2 == 0 else direction_v
arrow = Arrow(*direction_to_use , tip_length=0.2, color=ANIM_ORANGE, z_index=100, buff=0).shift(shift_factor)
vectors.add(arrow)
vectors.sort()
self.add(get_background())
self.play(Create(grid), run_time=4)
self.wait(0.5)
self.play(Write(red_dot), run_time=0.5)
self.wait(0.2)
self.play(FadeIn(VGroup(fifth, roosevelt)))
self.wait(0.3)
self.play(Write(green_dot), run_time=0.5)
self.wait(0.2)
self.play(FadeIn(VGroup(eighth, columbus)))
self.wait(0.5)
self.play(
Write(compass1),
grid_objs.animate.shift(shift_factor)
)
self.wait(0.5)
for i in vectors:
self.play(GrowArrow(i), run_time=0.7)
self.wait(0.2)
self.wait(0.5)
self.play(FadeOut(grid_objs, compass1, vectors))
self.wait(0.5)
class BigGridCompasses(Scene):
def construct(self):
buff = 0.5
vertical_lines = VGroup(*[Line(UP*3.1, DOWN*3.1, color=ANIM_BLACK, stroke_width=2) for _ in range(16)]).arrange(buff=buff)
horizontal_lines = VGroup(*[Line(LEFT*4, RIGHT*4, color=ANIM_BLACK, stroke_width=2) for _ in range(12)]).arrange(DOWN, buff=buff)
grid = VGroup(vertical_lines, horizontal_lines)
red_dot = Dot(point=(-buff*4.5, -buff*4.5, 0), radius=DEFAULT_DOT_RADIUS * 2, color=ANIM_ORANGE, z_index=1000)
green_dot = Dot(point=(buff*6.5, buff*3.5, 0), radius=DEFAULT_DOT_RADIUS * 2, color=ANIM_AQUA, z_index=1000)
panama = Tex("Panama", color=TEXT_COLOR).scale(0.5).next_to(red_dot, DOWN, buff=0.8)#.shift(LEFT*0.5)
rotterdam = Tex("Rotterdam", color=TEXT_COLOR).scale(0.5).next_to(green_dot, UP, buff=1.5)#.shift(LEFT*0.5)
grid_objs = VGroup(grid, green_dot, red_dot, panama, rotterdam)
shift_factor = LEFT*2
compass1 = Compass.create_compass(labels=["N", "S", "W", "E"], coords=RIGHT*4)
compass2 = Compass.create_compass(labels=["NE", "SW", "NW", "SE"], coords=compass1[0])
compasses = [Compass.create_compass(coords=compass1[0]) for _ in range(100)]
vector = Arrow(start=red_dot.get_center(), end=(buff*5.5, buff*4.5, 0), tip_length=.2, color=ANIM_ORANGE,
z_index=100, buff=0).shift(shift_factor)
final_vector = Arrow(start=red_dot.get_center(), end=green_dot.get_corner(DL), tip_length=.2, color=ANIM_ORANGE,
z_index=100, buff=0).shift(shift_factor)
final_vector.get_tip().z_index = 100
radius = 0.925
circle = Circle(radius=compasses[-1].width/2, color=ANIM_BLACK, fill_opacity=1, z_index=-1).move_to(RIGHT*4)
outer_circle = Circle(radius=radius, color=ANIM_BLACK, z_index=-1).move_to(RIGHT*4)
tracker = ValueTracker(PI/4)
radius += 0.025
vec = Arrow(start=RIGHT*4, end=(4 + np.cos(tracker.get_value())*radius, np.sin(tracker.get_value())*radius, 0), color=ANIM_ORANGE, buff=0, z_index=1000,
stroke_width=6, tip_length=0.2, max_stroke_width_to_length_ratio=6)
vec.add_updater(lambda m: m.become(
Arrow(start=outer_circle.get_center(), end=(outer_circle.get_x() + np.cos(tracker.get_value())*radius, np.sin(tracker.get_value())*radius, 0),
color=ANIM_ORANGE, buff=0, z_index=1000, stroke_width=6, tip_length=0.2, max_stroke_width_to_length_ratio=6)))
vec.suspend_updating()
rectangle = Rectangle(height=buff*12.5, width=buff*16.5, color=ANIM_BLACK, fill_opacity=1, z_index=0).shift(shift_factor)
grid_copy = grid.copy().shift(shift_factor)
earth = Tex("Earth", color=TEXT_COLOR).scale(0.5).move_to(panama).shift(shift_factor).shift(0.2*DOWN)
moon = Tex("Moon", color=TEXT_COLOR).scale(0.5).move_to(rotterdam).shift(shift_factor)
copy_dots = [green_dot.copy().shift(UP*DEFAULT_DOT_RADIUS*3*i+LEFT*DEFAULT_DOT_RADIUS*3*i+shift_factor) for i in range(1, 3)]
copy_dots = VGroup(*copy_dots)
self.add(get_background())
self.play(Create(vertical_lines), Create(horizontal_lines), run_time=3)
self.wait(0.5)
self.play(Create(red_dot), run_time=0.5)
self.wait(0.2)
self.play(Write(panama))
self.wait(0.3)
self.play(Create(green_dot), run_time=0.5)
self.wait(0.2)
self.play(Write(rotterdam))
self.wait(0.5)
self.play(Write(compass1), grid_objs.animate.shift(shift_factor))
self.wait(0.5)
self.add(compass2[0])
self.play(
Write(compass2[1]),
compass2[0].animate.rotate(-PI/4),
compass2[1].animate.rotate(-PI/4)
)
self.wait(0.5)
self.play(GrowArrow(vector), GrowArrow(vec))
self.wait(0.5)
for j, c in enumerate(compasses[:2]):
self.add(c.rotate(-PI/4))
angle = PI/8
angle *= -1 if j else 1
self.play(c.animate.rotate(angle), run_time=0.5, rate_func=rate_functions.ease_in_out_sine)
to_add = []
for j, c in enumerate(compasses[2:7]):
initial_angle = -PI/4 - (PI/8) * (j+1)
self.add(c.rotate(initial_angle))
angle = PI/16
angle *= -1 if j else 1
c.rotate(angle)
to_add.append(c)
self.play(FadeIn(*to_add))
self.wait(0.1)
to_add = []
for j, c in enumerate(compasses[7:16]):
initial_angle = -PI/4 - (PI/16) * (j+1)
self.add(c.rotate(initial_angle))
angle = PI/32
angle *= -1 if j else 1
c.rotate(angle)
to_add.append(c)
self.play(FadeIn(*to_add))
self.wait(0.05)
to_add = []
for j, c in enumerate(compasses[16:33]):
initial_angle = -PI/4 - (PI/32) * (j+1)
self.add(c.rotate(initial_angle))
angle = PI/64
angle *= -1 if j else 1
c.rotate(angle)
to_add.append(c)
self.play(FadeIn(*to_add), run_time=1.1)
self.wait(0.2)
self.play(FadeOut(compass1[1], compass2[1]), FadeIn(circle))
self.remove(*compasses, compass1[0], compass2[0])
self.play(TransformMatchingShapes(circle, outer_circle))
self.wait(0.5)
vec.resume_updating()
self.wait(0.5)
self.play(tracker.animate.set_value(36*DEGREES), ReplacementTransform(vector, final_vector))
self.wait(0.5)
self.play(
grid.animate.scale(0.4),
FadeTransform(grid_copy, rectangle),
ReplacementTransform(panama, earth),
ReplacementTransform(rotterdam, moon),
run_time=2)
self.wait(0.5)
self.play(
tracker.animate.set_value(43*DEGREES),
final_vector.animate.rotate(7*DEGREES, about_point=final_vector.get_start())
)
self.wait(0.5)
self.play(Create(copy_dots))
self.wait(2.0)
self.remove(grid, grid_copy)
self.play(
FadeOut(earth, moon, final_vector, copy_dots, red_dot, green_dot, rectangle),
outer_circle.animate.center())
vec.clear_updaters()
self.play(FadeOut(vec))
self.wait(0.5)
class DashedCircles(ZoomedScene):
def __init__(self, **kwargs):
ZoomedScene.__init__(
self,
zoom_factor=0.3,
zoomed_display_height=2,
zoomed_display_width=3,
image_frame_stroke_width=20,
zoomed_camera_config={
"default_frame_stroke_width": 3,
},
**kwargs
)
def construct(self):
config.disable_caching = True
def show_dashes_and_labels(angle, dt):
if dt == 0:
return
angle_int = int(angle)
angles_to_show = [i % 360 for i in range(self.last_dashed_angle + 1, angle_int + 1)]
label_to_show = np.intersect1d(angles_to_show, angles_w_labels)
if label_to_show.size > 0:
label_to_show = angles_w_labels.index(label_to_show[0])
labels[label_to_show].add_updater(lambda m, dt: m.set_opacity(m.get_fill_opacity() + dt *2))
labels[label_to_show].set_opacity(0)
self.add(labels[label_to_show])
self.last_dashed_angle = angle_int
if angles_to_show:
for i in angles_to_show:
self.add(dashes[i])
zoomed_camera = self.zoomed_camera
zoomed_display = self.zoomed_display
frame = zoomed_camera.frame
zoomed_display_frame = zoomed_display.display_frame
radius_tracker = ValueTracker(0.925)
angle_tracker = ValueTracker(40)
end_radius = 2.4
self.last_dashed_angle = int(angle_tracker.get_value()) - 1
angles_w_labels = [0, 90, 180, 270]
circle = Circle(radius=radius_tracker.get_value(), color=ANIM_BLACK).add_updater(
lambda m: m.become(Circle(radius=radius_tracker.get_value(), color=ANIM_BLACK))
)
tmp_circ = Circle(radius=1.5)
dashes = self.get_dashes(Circle(radius=end_radius))
labels = self.get_labels(Circle(radius=end_radius))
small_dashes = self.get_dashes(tmp_circ)
small_labels = self.get_labels(tmp_circ, labels=["0°, 360°", "45°", "90°", "135°", "180°", "225°", "270°", "315°"])
labels1 = self.get_labels(Circle(radius=end_radius), labels=["0, 100", "25", "50", "75"])
labels2 = self.get_labels(Circle(radius=end_radius), labels=["0, 400", "100", "200", "300"])
timer = Timer.create_timer()
zd_rect = BackgroundRectangle(zoomed_display, fill_opacity=0, buff=MED_SMALL_BUFF)
self.add_foreground_mobject(zd_rect)
unfold_camera = UpdateFromFunc(zd_rect, lambda rect: rect.replace(zoomed_display))
dasher = VMobject().add_updater(lambda m, dt: show_dashes_and_labels(angle_tracker.get_value(), dt), call_updater=True)
dasher.suspend_updating()
circle_r = Circle(radius=1.5, color=ANIM_BLACK).shift(RIGHT*4.5+DOWN*1.2)
labels_r = self.get_labels(tmp_circ, labels=["0, 100", "12.5", "25", "37.5", "50", "52.5", "75", "87.5"])
dashes_r = self.get_dashes(circle_r)
vec_r = self.get_arrow(circle_r, angle_tracker)
angle_label_r = VMobject().add_updater(lambda m, dt: m.become(self.get_angle_label(vec_r, angle_tracker.get_value(), 100,
False, 2, 1.3, m=m)), call_updater=True)
group_r = VGroup(circle_r, labels_r, vec_r, angle_label_r, dashes_r)
circle_l = Circle(radius=1.5, color=ANIM_BLACK).shift(LEFT*4.5+DOWN*1.2)
labels_l = self.get_labels(tmp_circ, labels=["0, 400", "50", "100", "150", "200", "250", "300", "350"])
dashes_l = self.get_dashes(circle_l)
vec_l = self.get_arrow(circle_l, angle_tracker)
angle_label_l = VMobject().add_updater(lambda m, dt: m.become(self.get_angle_label(vec_l, angle_tracker.get_value(), 400,
False, 2, 1.3, m=m)), call_updater=True)
group_l = VGroup(circle_l, labels_l, vec_l, angle_label_l, dashes_l)
##########################
self.add(get_background())
self.add(circle, dasher)
self.wait(0.5)
self.play(radius_tracker.animate.set_value(end_radius), run_time=2)
vec = self.get_arrow(circle, angle_tracker=angle_tracker, radius_tracker=radius_tracker)
angle_label = self.get_angle_label(vec, angle_tracker.get_value(), 360).add_updater(lambda m: m.become(
self.get_angle_label(vec, angle_tracker.get_value(), 360)
))
vec.update()
vec.suspend_updating()
vec.resume_updating()
circle.update()
angle_label.update()
self.wait(0.5)
vec.suspend_updating()
self.play(GrowArrow(vec))
vec.resume_updating()
self.wait(0.5)
self.play(FadeIn(timer[0][0], timer[1]), run_time=2)
self.wait(0.3)
Timer.animate(self, timer)
self.wait(0.5)
angle_label.suspend_updating()
angle_label_copy = angle_label.copy().clear_updaters()
self.play(Write(angle_label_copy))
self.wait()
self.play(FadeOut(angle_label_copy))
self.wait(0.5)
dasher.resume_updating()
self.play(angle_tracker.animate().set_value(0), run_time=5.3)
self.wait(0.5)
angle_label.resume_updating()
angle_label.update()
angle_label.suspend_updating()
self.play(FadeIn(angle_label))
angle_label.resume_updating()
self.wait(0.7)
self.play(angle_tracker.animate().set_value(400), run_time=5.3)
self.wait(0.5)
dasher.clear_updaters()
self.play(angle_tracker.animate.set_value(360), run_time=2)
angle_tracker.set_value(0)
self.wait(0.5)
self.play(angle_tracker.animate().set_value(-40), run_time=2)
self.wait(0.5)
self.play(angle_tracker.animate().set_value(0), run_time=2)
self.wait(0.5)
self.play(angle_tracker.animate().set_value(40), run_time=2.5)
self.wait(0.5)
for i in labels:
i.suspend_updating()
dasher.clear_updaters()
angle_label.clear_updaters()
self.wait(0.5)
# angle_tracker.set_value(40)
angle_label.add_updater(lambda m: m.become(
self.get_angle_label(vec, angle_tracker.get_value(), 360, decimal_places=2, scaling=1.3, m=m)))
# Move zooming frame to the angle_label
frame.move_to(angle_label).shift(DOWN*0.065+LEFT*0.07)
self.play(Create(frame))
self.activate_zooming()
self.play(self.get_zoomed_display_pop_out_animation(), unfold_camera)
self.wait(0.5)
self.play(angle_tracker.animate(rate_func=linear).set_value(39), run_time=2)
self.play(angle_tracker.animate(rate_func=linear).set_value(40), run_time=2)
self.wait(0.5)
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(0.5)
objs_w_updaters = [vec_l, vec_r, angle_label_l, angle_label_r]
for i in objs_w_updaters:
i.suspend_updating()
tmp_label = labels.copy()
tmp_ang_label = angle_label.copy()
tmp_ang_r = Text("11.11", font="Segoe UI Light", color=ANIM_ORANGE, stroke_width=1).scale(0.3).move_to(angle_label)
tmp_ang_l = Text("44.44", font="Segoe UI Light", color=ANIM_ORANGE, stroke_width=1).scale(0.3).move_to(angle_label)
for i in [tmp_ang_l, tmp_ang_r, *objs_w_updaters]:
i.resume_updating()
i.update()
i.suspend_updating()
i.update()
self.play(ReplacementTransform(labels, labels1), ReplacementTransform(angle_label, tmp_ang_r), run_time=1.5)
self.wait(0.5)
self.play(ReplacementTransform(labels1, labels2), ReplacementTransform(tmp_ang_r, tmp_ang_l), run_time=1.5)
self.wait(0.5)
self.play(ReplacementTransform(labels2, tmp_label), ReplacementTransform(tmp_ang_l, tmp_ang_label), run_time=1.5)
self.wait(0.5)
tmp_ang_label.clear_updaters()
tmp_ang_label.add_updater(lambda m, dt: m.become(self.get_angle_label(vec, angle_tracker.get_value(), 360, m=m)),
call_updater=True)
labels_r.shift(RIGHT*4.5+DOWN*1.2)
labels_l.shift(LEFT*4.5+DOWN*1.2)
labels.clear_updaters()
small_labels.clear_updaters()
for i in [tmp_ang_l, tmp_ang_r, *objs_w_updaters]:
i.update()
i.resume_updating()
i.update()
i.suspend_updating()
i.update()
self.play(FadeIn(group_r, group_l), radius_tracker.animate.set_value(1.5), TransformMatchingShapes(dashes, small_dashes),
ReplacementTransform(tmp_label, small_labels), run_time=2)
self.play(VGroup(
circle.clear_updaters(),
small_dashes,
small_labels
).animate.shift(UP*1.2),
run_time=1.2
)
self.wait(0.5)
for i in objs_w_updaters:
i.resume_updating()
self.play(angle_tracker.animate.set_value(400), run_time=4)
self.wait(0.5)
self.play(angle_tracker.animate.set_value(360), run_time=2)
angle_label.clear_updaters()
angle_label_l.clear_updaters()
angle_label_r.clear_updaters()
tmp_ang_label.clear_updaters()
# self.play(FadeOut(angle_label_l, angle_label_r, tmp_ang_label), run_time=0.8)
self.wait(0.5)
#############
def get_arrow(self, circle, angle_tracker=None, radius_tracker=None):
r = radius_tracker.get_value if radius_tracker else lambda: circle.radius
angle = angle_tracker.get_value() * DEGREES if angle_tracker else 0
p = circle.get_center
a = angle_tracker.get_value
arrow = Arrow(
start=p(),
end=(p()[0] + np.cos(angle)*r(), p()[1] + np.sin(angle)*r(), 0),
color=ANIM_ORANGE,
tip_length=0.2,
buff=0)
arrow.add_updater(lambda m: m.become(
Arrow(
start=p(),
end=(p()[0] + np.cos(a()*DEGREES)*r(), p()[1] + np.sin(a()*DEGREES)*r(), 0),
color=ANIM_ORANGE,
tip_length=0.2,
buff=0))
)
return arrow
def get_dashes(self, circle, offset1=0.05, offset2=0.06):
p = circle.get_center()
r = circle.radius
r -= offset1
dashes = VGroup()
thick_angles = [0, 90, 180, 270]
middle_angles = range(0, 360, 10)
for i in range(0, 360):
a = i * DEGREES
thick = int(i in thick_angles)
middle = int(i in middle_angles)
if thick:
r -= offset2
elif middle:
r -= offset2 / 2
dash = Line(stroke_width=1 + max(thick*1.5, middle/2), color=ANIM_BLACK, z_index=-10)
dash.set_length(0.07 + max(0.18*thick, 0.1*middle)).rotate(a)
dash.move_to((p[0] + np.cos(a) * r, p[1] + np.sin(a) * r, 0))
dashes.add(dash)
r = circle.radius - offset1
return dashes
def get_labels(self, circle, labels=["0°, 360°", "90°", "180°", "270°"]):
labels_text = VGroup()
dirs = [RIGHT, UP, LEFT, DOWN] if len(labels) == 4 else [RIGHT, UR, UP, UL, LEFT, DL, DOWN, DR]
for n, (i, d) in enumerate(zip(labels, dirs)):
negative_buff = len(labels) != 4 and n % 2 != 0
labels_text.add(
Text(i, font="Segoe UI Light", color=TEXT_COLOR, stroke_width=1).scale(0.34).next_to(circle.get_edge_center(d), d, buff=-0.35 if negative_buff else 0.22)
)
return labels_text
def get_angle_label(self, arrow, angle, max_angle, is_degree=True, decimal_places=0, scaling=1.29, m=None):
angle_cut = angle % 360
custom_angle = round(angle_cut * max_angle / 360, decimal_places if decimal_places else None)
if angle < 0:
custom_angle = round(angle, decimal_places if decimal_places else None)
if custom_angle == 0:
return VMobject()
string = str(custom_angle)
if is_degree:
string += "°"
pos = arrow.copy().scale(scaling).get_end()
label = Text(string, font="Segoe UI Light", color=ANIM_ORANGE, stroke_width=1).scale(0.32)
if decimal_places == 2 and int(custom_angle) == 40:
self.pos = pos + LEFT * label[:3].width
if decimal_places == 2 and int(custom_angle) == 39:
return label.move_to(self.pos, aligned_edge=LEFT)
return label.move_to(pos)
class RadianWarning(Scene):
def construct(self):
scale_factor = 0.4
warning = Text("Warning:", font="Segoe UI Light", color="#FF0000").scale(scale_factor)
rads = Text("Radians Are Counter-Intuitive", font="Segoe UI Light", color="#FF0000").scale(scale_factor)
group = VGroup(warning, rads).arrange(DOWN, buff=0.1).center()
self.add(get_background())
self.play(GrowFromCenter(group))
self.wait(0.5)
self.play(FadeOut(group))
self.play(FadeIn(group))
self.play(FadeOut(group))
#self.play(ShrinkToCenter(group))
#self.wait(2)
class RadianExplanation101(Scene):
def construct(self):
angle_tracker_1 = ValueTracker(0)
angle_tracker_2 = ValueTracker(1)
x_tracker = ValueTracker(0)
x_tracker_2 = ValueTracker(1.5)
rad_tracker = ValueTracker(1)
circle_1 = RadianCircle.get_circle_and_objs(1, 5, False, ORIGIN, angle_tracker_1, False, x_tracker)
circle_2 = RadianCircle.get_circle_and_objs(1, 10, False, RIGHT*1.5, angle_tracker_2, False, x_tracker_2, radius_tracker=rad_tracker)
circles = VGroup(circle_1, circle_2)
timer = Timer.create_timer()
arcs_1, labels_1 = self.get_radian_arcs(1, (-3, 0, 0))
arcs_2, labels_2 = self.get_radian_arcs(2, (2.5, 0, 0), scale_factor=1.2)
change_color = ANIM_AQUA
arcs_1_copy = arcs_1.copy().set_color(change_color)
arcs_2_copy = arcs_2.copy().set_color(change_color)
arcs_1_copy.z_index = 1000
arcs_2_copy.z_index = 1000
equation = MathTex(r"C\,=\,2\pi r\,=\, \pi d", color=TEXT_COLOR).scale(0.9).to_edge(DOWN, buff=0.8)
self.add(get_background())
circle, fixed_segment, rotating_segment, theta, dot, center_dot, arc_arrow, label_radius_tex, label_distance = circle_1
circle.suspend_updating()
self.play(FadeIn(circle))
circle.resume_updating()
self.wait(0.5)
dot.suspend_updating()
arc_arrow.suspend_updating()
self.play(FadeIn(dot, arc_arrow), run_time=0.7)
arc_arrow.resume_updating()
self.play(angle_tracker_1.animate.set_value(1), run_time=4)
self.wait(0.5)
for i in [rotating_segment, fixed_segment, center_dot, theta]:
i.update()
i.set_opacity(0)
self.add(i)
i.suspend_updating()
circle_2[0].suspend_updating()
self.play(VGroup(center_dot, fixed_segment, theta, rotating_segment).animate.set_opacity(1))