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
|
# -*- coding: utf-8 -*-
import sys, os, math
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from manim import *
from tiktok import *
D = 1.1 # line spacing = needle length
Y0 = 0.35 # lowest line
NLIN = 5
SEED, N1, N2 = 36, 60, 200
def throw(n):
rng = np.random.default_rng(SEED)
x = rng.uniform(-3.9, 3.9, n)
y = rng.uniform(Y0 + 0.1, Y0 + (NLIN - 1) * D - 0.1, n)
th = rng.uniform(0, PI, n)
dx, dy = D / 2 * np.cos(th), D / 2 * np.sin(th)
p1 = np.stack([x - dx, y - dy, np.zeros(n)], axis=1)
p2 = np.stack([x + dx, y + dy, np.zeros(n)], axis=1)
crosses = np.floor((p1[:, 1] - Y0) / D) != np.floor((p2[:, 1] - Y0) / D)
return p1, p2, crosses
class Buffon(TikTok):
NAME = "buffon"
def construct(self):
self.prepare()
chip = self.chip("π sin círculos", C_PINK)
p1, p2, crosses = throw(N2)
stripes = VGroup(*[Line([-4.2, Y0 + k * D, 0], [4.2, Y0 + k * D, 0],
stroke_width=4, color="#33405C") for k in range(NLIN)])
with self.beat(0) as b:
hook = VGroup(
self.fit(Text("tiro agujas al piso", font=FONT, font_size=52,
weight=BOLD, color=WHITE), 8.2),
self.fit(Text("y sale π", font=FONT, font_size=64,
weight=BOLD, color=AMBER), 8.2),
).arrange(DOWN, buff=0.32).move_to(UP * 2.4)
b.play(FadeIn(chip, shift=DOWN * 0.3), run_time=0.45)
b.play(FadeIn(hook[0], shift=UP * 0.15), run_time=0.6)
b.play(Write(hook[1]), run_time=0.8)
with self.beat(1) as b:
b.play(FadeOut(hook), run_time=0.35)
b.play(LaggedStart(*[Create(r) for r in stripes], lag_ratio=0.12), run_time=0.9)
demo = Line([-1.0, Y0 + D, 0], [-1.0, Y0 + 2 * D, 0],
stroke_width=9, color=AMBER)
med = DoubleArrow([0.2, Y0 + D, 0], [0.2, Y0 + 2 * D, 0], buff=0,
color=C_GREEN, stroke_width=6, tip_length=0.2)
lab = Text("mismo largo", font=FONT, font_size=32, weight=BOLD, color=C_GREEN)
lab.next_to(med, RIGHT, buff=0.25)
b.play(Create(demo), run_time=0.5)
b.play(GrowArrow(med), FadeIn(lab), run_time=0.6)
needles = VGroup(*[Line(p1[i], p2[i], stroke_width=4.5, color="#7C8AA8")
for i in range(N2)])
with self.beat(2) as b:
b.play(FadeOut(VGroup(demo, med, lab)), run_time=0.3)
b.play(LaggedStart(*[FadeIn(needles[i]) for i in range(N1)],
lag_ratio=0.012), run_time=max(1.1, b.floor - 0.5))
with self.beat(3) as b:
hit = [needles[i] for i in range(N1) if crosses[i]]
miss = [needles[i] for i in range(N1) if not crosses[i]]
b.play(*[a.animate.set_stroke(AMBER, 6) for a in hit],
*[a.animate.set_stroke("#46526E", 3.5) for a in miss], run_time=1.0)
c1 = int(crosses[:N1].sum())
def marker(n, c, y=-0.75):
g = VGroup(
VGroup(Text("tiradas", font=FONT, font_size=30, color=C_GRAY),
Text(str(n), font=FONT, font_size=62, weight=BOLD, color=WHITE)
).arrange(DOWN, buff=0.1),
VGroup(Text("cruzan", font=FONT, font_size=30, color=C_GRAY),
Text(str(c), font=FONT, font_size=62, weight=BOLD, color=AMBER)
).arrange(DOWN, buff=0.1),
).arrange(RIGHT, buff=1.5).move_to([0, y, 0])
return g
marker_m = marker(N1, c1)
with self.beat(4) as b:
b.play(FadeIn(marker_m, shift=UP * 0.15), run_time=0.8)
with self.beat(5) as b:
f = MathTex(r"\frac{2 \times \text{tiradas}}{\text{cruces}}",
color=C_SKY).scale(1.25).move_to(DOWN * 2.45)
b.play(Write(f), run_time=1.1)
with self.beat(6) as b:
estimate = 2 * N1 / c1
res = MathTex(r"= %s" % f"{estimate:.4f}".replace(".", "{,}"),
color=C_GREEN).scale(1.45)
res.next_to(f, RIGHT, buff=0.4)
grp = VGroup(f, res)
b.play(Write(res), run_time=0.8)
b.play(grp.animate.move_to(DOWN * 2.45), run_time=0.4)
b.play(Flash(res, color=C_GREEN, line_length=0.5, num_lines=16,
flash_radius=1.4), run_time=0.6)
c2 = int(crosses.sum())
with self.beat(7) as b:
hit = [needles[i] for i in range(N1, N2) if crosses[i]]
miss = [needles[i] for i in range(N1, N2) if not crosses[i]]
for a in hit:
a.set_stroke(AMBER, 6)
for a in miss:
a.set_stroke("#46526E", 3.5)
b.play(LaggedStart(*[FadeIn(needles[i]) for i in range(N1, N2)],
lag_ratio=0.004), run_time=max(1.0, b.floor - 1.3))
est2 = 2 * N2 / c2
nm = marker(N2, c2)
nres = MathTex(r"= %s" % f"{est2:.4f}".replace(".", "{,}"),
color=C_GREEN).scale(1.45).next_to(f, RIGHT, buff=0.4)
b.play(Transform(marker_m, nm), Transform(res, nres), run_time=0.9)
with self.beat(8) as b:
pi = MathTex(r"\pi = 3{,}14159\ldots", color=AMBER).scale(1.5)
pi.move_to(UP * 5.5)
b.play(Write(pi), run_time=1.0)
b.play(Circumscribe(pi, color=AMBER, buff=0.25, stroke_width=6), run_time=0.8)
with self.beat(9) as b:
b.play(FadeOut(VGroup(marker_m, pi, f, res)), run_time=0.45)
t = VGroup(
self.fit(Text("no hay ningún círculo", font=FONT, font_size=48,
weight=BOLD, color=WHITE), 8.2),
self.fit(Text("y π aparece igual", font=FONT, font_size=48,
weight=BOLD, color=AMBER), 8.2),
).arrange(DOWN, buff=0.35).move_to(DOWN * 1.5)
b.play(FadeIn(t[0], shift=UP * 0.15), run_time=0.6)
b.play(FadeIn(t[1], shift=UP * 0.15), run_time=0.7)
with self.beat(10) as b:
b.play(FadeOut(t), run_time=0.35)
count_txt = VGroup(
self.fit(Text("aguja de Buffon", font=FONT, font_size=58,
weight=BOLD, color=AMBER), 8.0),
self.fit(Text("hacelo con fósforos", font=FONT, font_size=44,
weight=BOLD, color=WHITE), 8.0),
).arrange(DOWN, buff=0.35).move_to(DOWN * 1.5)
b.play(Write(count_txt[0]), run_time=0.8)
b.play(FadeIn(count_txt[1], shift=UP * 0.15), run_time=0.6)
self.finish()
|