forked from 3b1b/manim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoser_intro.py
295 lines (260 loc) · 8.31 KB
/
moser_intro.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
#!/usr/bin/env python
import numpy as np
import itertools as it
import operator as op
from copy import deepcopy
from manimlib.imports import *
RADIUS = FRAME_Y_RADIUS - 0.1
CIRCLE_DENSITY = DEFAULT_POINT_DENSITY_1D*RADIUS
def logo_to_circle():
from .generate_logo import DARK_BROWN, LOGO_RADIUS
sc = Scene()
small_circle = Circle(
density = CIRCLE_DENSITY,
color = 'skyblue'
).scale(LOGO_RADIUS).set_color(
DARK_BROWN, lambda x_y_z : x_y_z[0] < 0 and x_y_z[1] > 0
)
big_circle = Circle(density = CIRCLE_DENSITY).scale(RADIUS)
sc.add(small_circle)
sc.wait()
sc.animate(Transform(small_circle, big_circle))
return sc
def count_sections(*radians):
sc = Scene()
circle = Circle(density = CIRCLE_DENSITY).scale(RADIUS)
sc.add(circle)
points = [
(RADIUS * np.cos(angle), RADIUS * np.sin(angle), 0)
for angle in radians
]
dots = [Dot(point) for point in points]
interior = Region(lambda x, y : x**2 + y**2 < RADIUS**2)
for x in range(1, len(points)):
if x == 1:
sc.animate(ShowCreation(dots[0]), ShowCreation(dots[1]))
sc.add(dots[0], dots[1])
else:
sc.animate(ShowCreation(dots[x]))
sc.add(dots[x])
new_lines = Mobject(*[
Line(points[x], points[y]) for y in range(x)
])
sc.animate(Transform(deepcopy(dots[x]), new_lines, run_time = 2.0))
sc.add(new_lines)
sc.wait()
regions = plane_partition_from_points(*points[:x+1])
for reg in regions:
reg.intersect(interior)
regions = [reg for reg in regions if reg.bool_grid.any()]
last_num = None
for reg, count in zip(regions, it.count(1)):
number = TexMobject(str(count)).shift((RADIUS, 3, 0))
sc.set_color_region(reg)
rt = 1.0 / (x**0.8)
sc.add(number)
sc.remove(last_num)
last_num = number
sc.wait(rt)
sc.reset_background()
sc.remove(last_num)
sc.animate(Transform(last_num, deepcopy(last_num).center()))
sc.wait()
sc.remove(last_num)
return sc
def summarize_pattern(*radians):
sc = Scene()
circle = Circle(density = CIRCLE_DENSITY).scale(RADIUS)
sc.add(circle)
points = [
(RADIUS * np.cos(angle), RADIUS * np.sin(angle), 0)
for angle in radians
]
dots = [Dot(point) for point in points]
last_num = None
for x in range(len(points)):
new_lines = Mobject(*[
Line(points[x], points[y]) for y in range(x)
])
num = TexMobject(str(moser_function(x + 1))).center()
sc.animate(
Transform(last_num, num) if last_num else ShowCreation(num),
FadeIn(new_lines),
FadeIn(dots[x]),
run_time = 0.5,
)
sc.remove(last_num)
last_num = num
sc.add(num, dots[x], new_lines)
sc.wait()
return sc
def connect_points(*radians):
sc = Scene()
circle = Circle(density = CIRCLE_DENSITY).scale(RADIUS)
sc.add(circle)
points = [
(RADIUS * np.cos(angle), RADIUS * np.sin(angle), 0)
for angle in radians
]
dots = [Dot(point) for point in points]
sc.add(*dots)
anims = []
all_lines = []
for x in range(len(points)):
lines = [Line(points[x], points[y]) for y in range(len(points))]
lines = Mobject(*lines)
anims.append(Transform(deepcopy(dots[x]), lines, run_time = 3.0))
all_lines.append(lines)
sc.animate(*anims)
sc.add(*all_lines)
sc.wait()
return sc
def interesting_problems():
sc = Scene()
locales = [(6, 2, 0), (6, -2, 0), (-5, -2, 0)]
fermat = Mobject(*TexMobjects(["x^n","+","y^n","=","z^n"]))
fermat.scale(0.5).shift((-2.5, 0.7, 0))
face = SimpleFace()
tb = ThoughtBubble().shift((-1.5, 1, 0))
sb = SpeechBubble().shift((-2.4, 1.3, 0))
fermat_copies, face_copies, tb_copies, sb_copies = (
Mobject(*[
deepcopy(mob).scale(0.5).shift(locale)
for locale in locales
])
for mob in [fermat, face, tb, sb]
)
sc.add(face, tb)
sc.animate(ShowCreation(fermat, run_time = 1))
sc.add(fermat)
sc.wait()
sc.animate(
Transform(
deepcopy(fermat).repeat(len(locales)),
fermat_copies
),
FadeIn(face_copies, run_time = 1.0)
)
sc.animate(FadeIn(tb_copies))
sc.wait()
sc.animate(
Transform(tb, sb),
Transform(tb_copies, sb_copies)
)
return sc
def response_invitation():
sc = Scene()
video_icon = VideoIcon()
mini_videos = Mobject(*[
deepcopy(video_icon).scale(0.5).shift((3, y, 0))
for y in [-2, 0, 2]
])
comments = Mobject(*[
Line((-1.2, y, 0), (1.2, y, 0), color = 'white')
for y in [-1.5, -1.75, -2]
])
sc.add(video_icon)
sc.wait()
sc.animate(Transform(deepcopy(video_icon).repeat(3), mini_videos))
sc.add(mini_videos)
sc.wait()
sc.animate(ShowCreation(comments, run_time = 1.0))
return sc
def different_points(radians1, radians2):
sc = Scene()
circle = Circle(density = CIRCLE_DENSITY).scale(RADIUS)
sc.add(circle)
points1, points2 = (
[
(RADIUS * np.cos(angle), RADIUS * np.sin(angle), 0)
for angle in radians
]
for radians in (radians1, radians2)
)
dots1, dots2 = (
Mobject(*[Dot(point) for point in points])
for points in (points1, points2)
)
lines1, lines2 = (
[
Line(point1, point2)
for point1, point2 in it.combinations(points, 2)
]
for points in (points1, points2)
)
sc.add(dots1, *lines1)
sc.animate(
Transform(dots1, dots2, run_time = 3),
*[
Transform(line1, line2, run_time = 3)
for line1, line2 in zip(lines1, lines2)
]
)
sc.wait()
return sc
def next_few_videos(*radians):
sc = Scene()
circle = Circle(density = CIRCLE_DENSITY).scale(RADIUS)
points = [
(RADIUS * np.cos(angle), RADIUS * np.sin(angle), 0)
for angle in radians
]
dots = Mobject(*[
Dot(point) for point in points
])
lines = Mobject(*[
Line(point1, point2)
for point1, point2 in it.combinations(points, 2)
])
thumbnail = Mobject(circle, dots, lines)
frame = VideoIcon().set_color(
"black",
lambda point : get_norm(point) < 0.5
)
big_frame = deepcopy(frame).scale(FRAME_X_RADIUS)
frame.shift((-5, 0, 0))
sc.add(thumbnail)
sc.wait()
sc.animate(
Transform(big_frame, frame),
Transform(
thumbnail,
deepcopy(thumbnail).scale(0.15).shift((-5, 0, 0))
)
)
sc.add(frame, thumbnail)
sc.wait()
last = frame
for x in [-2, 1, 4]:
vi = VideoIcon().shift((x, 0, 0))
sc.animate(
Transform(deepcopy(last), vi),
Animation(thumbnail)#Keeps it from getting burried
)
sc.add(vi)
last = vi
return sc
if __name__ == '__main__':
radians = [1, 3, 5, 2, 4, 6]
more_radians = radians + [10, 13, 20, 17, 15, 21, 18.5]
different_radians = [1.7, 4.8, 3.2, 3.5, 2.1, 5.5]
# logo_to_circle().write_to_movie("moser/LogoToCircle")
# count_sections(*radians).write_to_movie("moser/CountingSections")
# summarize_pattern(*radians).write_to_movie("moser/SummarizePattern")
# interesting_problems().write_to_movie("moser/InterestingProblems")
# summarize_pattern(*more_radians).write_to_movie("moser/ExtendedPattern")
# connect_points(*radians).write_to_movie("moser/ConnectPoints")
# response_invitation().write_to_movie("moser/ResponseInvitation")
# different_points(radians, different_radians).write_to_movie("moser/DifferentPoints")
# next_few_videos(*radians).write_to_movie("moser/NextFewVideos")
# summarize_pattern(*different_radians).write_to_movie("moser/PatternWithDifferentPoints")
#Images
# TexMobject(r"""
# \Underbrace{1, 2, 4, 8, 16, 31, \dots}_\text{What?}
# """).save_image("moser/NumberList31")
# TexMobject("""
# 1, 2, 4, 8, 16, 32, 63, \dots
# """).save_image("moser/NumberList63")
# TexMobject("""
# 1, 2, 4, 8, 15, \dots
# """).save_image("moser/NumberList15")