aboutsummaryrefslogtreecommitdiffstats
path: root/scenes/v17_cicloide.py
blob: 79b92819369c05c051b07aea9c99907f55c687b9 (plain)
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
# -*- coding: utf-8 -*-
"""The brachistochrone: the straight line loses to the cycloid.
All motion comes from real physics (frictionless free fall)."""
import sys, os, math
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from manim import *
from tiktok import *

TH1 = 4.0553                  # final angle: gives D/H = 3, the curve dips below
DX = 7.0
RR = DX / (TH1 - math.sin(TH1))
HH = RR * (1 - math.cos(TH1))
X0, Y0 = -3.5, 3.15
T_C = 1.55                                     # how long the cycloid takes on screen
G = RR * (TH1 / T_C) ** 2                      # gravity tuned to that time
LL = math.hypot(DX, HH)
AA = G * HH / LL
T_L = math.sqrt(2 * LL / AA)                   # 2.18 s: the straight line arrives 40% later
P0 = np.array([X0, Y0, 0.0])
P1 = np.array([X0 + DX, Y0 - HH, 0.0])
UU = (P1 - P0) / LL
VMAX = math.sqrt(4 * G * RR)

RW = 1.12                                      # bike wheel
XW = -PI * RW
YW = 0.55
R2, BX, BY = 1.22, 0.0, 0.30                   # tautochrone bowl
T_TAU = 2.0
WW = PI / (2 * T_TAU)


def cyc(th):
    return np.array([X0 + RR * (th - math.sin(th)), Y0 - RR * (1 - math.cos(th)), 0.0])


def tick_m(th):
    return np.array([XW + RW * (th - math.sin(th)), YW + RW * (1 - math.cos(th)), 0.0])


def bowl(th):
    return np.array([BX + R2 * (th - math.sin(th)) - R2 * PI,
                     BY + 2 * R2 - R2 * (1 - math.cos(th)), 0.0])


def poli(f, a, b, col, w=8, n=200):
    m = VMobject(stroke_color=col, stroke_width=w)
    m.set_points_as_corners([f(t) for t in np.linspace(a, b, n)])
    return m


class Cycloid(TikTok):
    NAME = "cicloide"

    def construct(self):
        self.prepare()
        chip = self.chip("¿Cuál gana?", AMBER)

        money = DashedLine([X0 - 0.8, Y0 + 0.66, 0], [X0 + 1.7, Y0 + 0.66, 0],
                           stroke_width=3, color=C_GRAY, dash_length=0.1)
        pc = Dot(radius=0.19, color=AMBER).move_to([X0 - 0.4, Y0 + 0.66, 0])
        pr = Dot(radius=0.19, color=C_SKY).move_to([X0 + 0.08, Y0 + 0.66, 0])
        meta = DashedLine(P1 + UP * 1.35, P1 + DOWN * 0.95, stroke_width=6,
                          color=C_GREEN, dash_length=0.14)
        rotm = Text("META", font=FONT, font_size=32, weight=BOLD, color=C_GREEN)
        rotm.next_to(meta, UP, buff=0.14)

        legend = VGroup(
            VGroup(Dot(radius=0.15, color=AMBER),
                   Text("curva", font=FONT, font_size=38, weight=BOLD,
                        color=AMBER)).arrange(RIGHT, buff=0.22),
            VGroup(Dot(radius=0.15, color=C_SKY),
                   Text("recta", font=FONT, font_size=38, weight=BOLD,
                        color=C_SKY)).arrange(RIGHT, buff=0.22),
        ).arrange(RIGHT, buff=1.3).move_to([0, -1.3, 0])

        with self.beat(0) as b:
            b.play(FadeIn(chip, shift=DOWN * 0.3), run_time=0.4)
            b.play(Create(money), run_time=0.4)
            b.play(FadeIn(pc, scale=0.4), FadeIn(pr, scale=0.4), run_time=0.5)
            b.play(Create(meta), FadeIn(rotm), FadeIn(legend), run_time=0.7)

        line_m = Line(P0, P1, stroke_width=8, color=C_SKY)
        curve_obj = poli(cyc, 0, TH1, AMBER)

        with self.beat(1) as b:
            b.play(Create(line_m), run_time=0.9)
            b.play(Create(curve_obj), run_time=max(0.9, b.floor - 1.4))

        inter = Text("?", font=FONT, font_size=115, weight=BOLD,
                     color=WHITE).move_to([0.0, 1.62, 0])

        with self.beat(2) as b:
            b.play(FadeIn(inter, scale=0.5), run_time=0.5)
            b.play(Indicate(inter, scale_factor=1.25, color=AMBER), run_time=0.9)

        rot = Text("el más corto", font=FONT, font_size=40, weight=BOLD,
                   color=C_SKY).move_to([-0.15, 3.02, 0])

        with self.beat(3) as b:
            b.play(FadeOut(inter), run_time=0.3)
            b.play(line_m.animate.set_stroke(width=12), FadeIn(rot, shift=UP * 0.2),
                   run_time=0.55)
            b.play(ShowPassingFlash(line_m.copy().set_stroke(WHITE, 13), time_width=0.5),
                   run_time=1.0)
            b.play(line_m.animate.set_stroke(width=8), run_time=0.3)

        # --- the race ----------------------------------------------------------
        T = ValueTracker(0.0)

        def up_c(m):
            t = min(T.get_value(), T_C)
            m.move_to(cyc(min(t * math.sqrt(G / RR), TH1)))

        def up_r(m):
            t = min(T.get_value(), T_L)
            m.move_to(P0 + UU * (0.5 * AA * t * t))

        med = VGroup(Circle(radius=0.34, stroke_width=0, fill_color=AMBER, fill_opacity=1),
                     Text("1º", font=FONT, font_size=34, weight=BOLD, color="#0B0F1A"))
        med.move_to(P1 + UP * 0.72).set_opacity(0)
        med.add_updater(lambda m: m.set_opacity(1.0 if T.get_value() >= T_C else 0.0))

        with self.beat(4) as b:
            b.play(FadeOut(rot), FadeOut(money), pc.animate.move_to(P0),
                   pr.animate.move_to(P0), run_time=0.5)
            ya = Text("¡LARGA!", font=FONT, font_size=66, weight=BOLD,
                      color=C_GREEN).move_to([0, 1.7, 0])
            b.play(FadeIn(ya, scale=1.7), run_time=0.3)
            b.play(FadeOut(ya), run_time=0.22)
            pc.add_updater(up_c)
            pr.add_updater(up_r)
            b.add(med)
            b.play(T.animate.set_value(T_L), run_time=T_L, rate_func=linear)
            pc.clear_updaters(); pr.clear_updaters(); med.clear_updaters()

        level = DashedLine([X0 - 0.2, Y0 - HH, 0], [X0 + DX + 0.2, Y0 - HH, 0],
                           stroke_width=4, color=C_GRAY, dash_length=0.12)
        background = cyc(PI)
        note_txt = Text("pasa por debajo de la meta", font=FONT, font_size=36, weight=BOLD,
                    color=C_PINK).move_to([0.3, -0.95, 0])
        arrow_m = Arrow(note_txt.get_top() + RIGHT * 0.9, background + DOWN * 0.22, buff=0.06,
                       color=C_PINK, stroke_width=6, max_tip_length_to_length_ratio=0.2)
        gap = VGroup(
            Text("la recta tardó", font=FONT, font_size=38, weight=BOLD, color=WHITE),
            Text("40% más", font=FONT, font_size=64, weight=BOLD, color=C_SKY),
        ).arrange(RIGHT, buff=0.3).move_to([0.0, -2.15, 0])

        with self.beat(5) as b:
            b.play(FadeOut(legend), Create(level), run_time=0.45)
            b.play(FadeIn(note_txt, shift=UP * 0.2), GrowArrow(arrow_m), run_time=0.6)
            b.play(FadeIn(gap, shift=UP * 0.2), run_time=0.55)

        # --- why it wins: early speed ------------------------------------------
        riel_c = Rectangle(width=6.2, height=0.32, stroke_width=0, fill_color="#1B2233",
                           fill_opacity=1).move_to([0.0, -1.30, 0])
        riel_r = riel_c.copy().move_to([0.0, -2.20, 0])
        bar_c = Rectangle(width=0.02, height=0.32, stroke_width=0, fill_color=AMBER,
                          fill_opacity=1).move_to(riel_c.get_left(), LEFT)
        bar_r = Rectangle(width=0.02, height=0.32, stroke_width=0, fill_color=C_SKY,
                          fill_opacity=1).move_to(riel_r.get_left(), LEFT)
        lab_c = Text("curva", font=FONT, font_size=30, weight=BOLD, color=AMBER)
        lab_r = Text("recta", font=FONT, font_size=30, weight=BOLD, color=C_SKY)
        lab_c.next_to(riel_c, LEFT, buff=0.18)
        lab_r.next_to(riel_r, LEFT, buff=0.18)
        left_c, left_r = riel_c.get_left()[0], riel_r.get_left()[0]

        def bar_m(m, v, x0, y):
            w = max(0.02, 6.2 * v / VMAX)
            m.stretch_to_fit_width(w)
            m.move_to([x0 + w / 2, y, 0])

        def ub_c(m):
            t = min(T.get_value(), T_C)
            th = t * math.sqrt(G / RR)
            bar_m(m, math.sqrt(max(0.0, 2 * G * RR * (1 - math.cos(th)))), left_c, -1.30)

        def ub_r(m):
            bar_m(m, AA * min(T.get_value(), T_L), left_r, -2.20)

        with self.beat(6) as b:
            b.play(FadeOut(VGroup(level, arrow_m, note_txt, gap, med)), run_time=0.35)