aboutsummaryrefslogtreecommitdiffstats
path: root/scenes/v18_dados.py
blob: fb5d72e7024aa833358b661d676cec83dd0d8122 (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
# -*- coding: utf-8 -*-
"""Non-transitive dice: A beats B, B beats C and C beats A, always 5 out of 9."""
import sys, os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from manim import *
from tiktok import *

RED_C = C_PINK
FACES = {"rojo": [2, 4, 9], "celeste": [1, 6, 8], "verde": [3, 5, 7]}
COL = {"rojo": RED_C, "celeste": C_SKY, "verde": C_GREEN}
CELL = 1.30


def die(obj_name, s=0.62):
    vals = FACES[obj_name] * 2
    col = COL[obj_name]
    face_list = VGroup()
    for v in vals:
        f = RoundedRectangle(corner_radius=0.11, width=s, height=s, stroke_width=3,
                             stroke_color=col, fill_color="#121C33", fill_opacity=1)
        face_list.add(VGroup(f, Text(str(v), font=FONT, font_size=32, weight=BOLD,
                                 color=col).move_to(f)))
    face_list.arrange_in_grid(rows=2, cols=3, buff=0.08)
    rot = Text(obj_name, font=FONT, font_size=30, weight=BOLD, color=col)
    rot.next_to(face_list, DOWN, buff=0.18)
    return VGroup(face_list, rot)


def duel(na, nb, center_pt):
    """3x3 table with the nine duels. Each cell painted with the winner's color."""
    av, bv = FACES[na], FACES[nb]
    ca, cb = COL[na], COL[nb]
    cells, wins = VGroup(), 0
    for i, a in enumerate(av):
        for j, bq in enumerate(bv):
            ok = a > bq
            wins += ok
            r = RoundedRectangle(corner_radius=0.1, width=CELL, height=CELL, stroke_width=4,
                                 stroke_color="#0B0F1A", fill_color=ca if ok else cb,
                                 fill_opacity=0.92)
            t = Text(str(max(a, bq)), font=FONT, font_size=42, weight=BOLD, color="#0B0F1A")
            c = VGroup(r, t.move_to(r))
            c.move_to(center_pt + np.array([(j - 1) * (CELL + 0.07),
                                         -(i - 1) * (CELL + 0.07), 0.0]))
            cells.add(c)
    d = CELL + 0.07
    outside = d + CELL / 2 + 0.36            # the caption goes outside the table
    header = VGroup(*[Text(str(v), font=FONT, font_size=38, weight=BOLD, color=cb)
                   .move_to(center_pt + np.array([(j - 1) * d, outside, 0.0]))
                   for j, v in enumerate(bv)])
    lat = VGroup(*[Text(str(v), font=FONT, font_size=38, weight=BOLD, color=ca)
                   .move_to(center_pt + np.array([-outside, -(i - 1) * d, 0.0]))
                   for i, v in enumerate(av)])
    return cells, header, lat, wins


class Dice(TikTok):
    NAME = "dados"

    def construct(self):
        self.prepare()
        chip = self.chip("Intuición rota", RED_C)

        dds = VGroup(*[die(n) for n in ("rojo", "celeste", "verde")])
        dds.arrange(RIGHT, buff=0.55).move_to(UP * 4.15)

        with self.beat(0) as b:
            b.play(FadeIn(chip, shift=DOWN * 0.3), run_time=0.4)
            b.play(LaggedStart(*[FadeIn(d, shift=DOWN * 0.3) for d in dds],
                               lag_ratio=0.25), run_time=1.1)
            finger = Triangle(stroke_width=0, fill_color=WHITE, fill_opacity=1).scale(0.22)
            finger.rotate(PI).next_to(dds[1], DOWN, buff=0.35)
            b.play(FadeIn(finger, shift=UP * 0.3), run_time=0.5)

        you_side = Text("vos", font=FONT, font_size=34, weight=BOLD, color=WHITE)
        you_side.next_to(finger, DOWN, buff=0.15)
        me_txt = Text("yo elijo después", font=FONT, font_size=40, weight=BOLD, color=AMBER)
        me_txt.move_to(UP * 1.45)

        with self.beat(1) as b:
            b.play(FadeIn(you_side), run_time=0.35)
            b.play(FadeIn(me_txt, shift=UP * 0.2), run_time=0.6)

        big_txt = Text("56%", font=FONT, font_size=190, weight=BOLD, color=AMBER)
        big_txt.move_to(DOWN * 0.55)
        footer = Text("gano yo", font=FONT, font_size=46, weight=BOLD, color=WHITE)
        footer.next_to(big_txt, DOWN, buff=0.25)

        with self.beat(2) as b:
            b.play(FadeOut(VGroup(finger, you_side, me_txt)), run_time=0.3)
            b.play(FadeIn(big_txt, scale=0.6), run_time=0.7)
            b.play(FadeIn(footer, shift=UP * 0.2), run_time=0.4)
            b.play(Flash(big_txt, color=AMBER, flash_radius=2.6, num_lines=20), run_time=0.8)

        with self.beat(3) as b:
            b.play(FadeOut(VGroup(big_txt, footer)), run_time=0.35)
            b.play(dds[1].animate.set_opacity(0.25), dds[2].animate.set_opacity(0.25),
                   run_time=0.4)
            big_one = dds[0].copy().scale(2.0).move_to(UP * 1.2)
            b.play(TransformFromCopy(dds[0], big_one), run_time=0.8)

        with self.beat(4) as b:
            b.play(FadeOut(big_one), dds[1].animate.set_opacity(1),
                   dds[2].animate.set_opacity(1), run_time=0.5)
            g2 = dds[1].copy().scale(1.7).move_to(UP * 1.5 + LEFT * 1.9)
            g3 = dds[2].copy().scale(1.7).move_to(UP * 1.5 + RIGHT * 1.9)
            b.play(TransformFromCopy(dds[1], g2), run_time=0.6)
            b.play(TransformFromCopy(dds[2], g3), run_time=0.6)

        # --- the three match-ups ------------------------------------------------
        center_pt = np.array([0.0, 0.42, 0.0])

        def crossing(beat, na, nb, previous):
            cells, header, lat, wins = duel(na, nb, center_pt)
            tick_m = VGroup(
                Text(na, font=FONT, font_size=42, weight=BOLD, color=COL[na]),
                Text(f"gana {wins} de 9", font=FONT, font_size=42, weight=BOLD, color=WHITE),
            ).arrange(RIGHT, buff=0.25).move_to(DOWN * 2.45)
            if previous:
                beat.play(FadeOut(previous), run_time=0.3)
            beat.play(FadeIn(VGroup(header, lat)), run_time=0.35)
            beat.play(LaggedStart(*[FadeIn(c, scale=0.6) for c in cells],
                                  lag_ratio=0.09), run_time=max(1.0, beat.floor - 1.5))
            beat.play(FadeIn(tick_m, shift=UP * 0.2), run_time=0.45)
            return VGroup(cells, header, lat, tick_m)

        with self.beat(5) as b:
            b.play(FadeOut(VGroup(g2, g3)), run_time=0.3)
            t1 = crossing(b, "rojo", "celeste", None)

        with self.beat(6) as b:
            t2 = crossing(b, "celeste", "verde", t1)

        with self.beat(7) as b:
            b.play(FadeOut(t2), run_time=0.35)
            question = VGroup(
                Text("rojo", font=FONT, font_size=54, weight=BOLD, color=RED_C),
                Text("→", font=FONT, font_size=54, weight=BOLD, color=WHITE),
                Text("verde", font=FONT, font_size=54, weight=BOLD, color=C_GREEN),
                Text("?", font=FONT, font_size=70, weight=BOLD, color=AMBER),
            ).arrange(RIGHT, buff=0.3).move_to(UP * 1.6)
            b.play(FadeIn(question, shift=UP * 0.25), run_time=0.6)
            b.play(Indicate(question[3], color=AMBER, scale_factor=1.4), run_time=0.7)

        with self.beat(8) as b:
            no = Text("NO", font=FONT, font_size=96, weight=BOLD, color=C_PINK).move_to(UP * 1.6)
            b.play(FadeOut(question), run_time=0.25)
            b.play(FadeIn(no, scale=1.8), run_time=0.4)
            b.play(FadeOut(no), run_time=0.3)
            t3 = crossing(b, "verde", "rojo", None)

        # --- the circle ----------------------------------------------------------
        with self.beat(9) as b:
            b.play(FadeOut(t3), FadeOut(dds), run_time=0.4)
            vert = {"rojo": np.array([0.0, 4.1, 0.0]),
                    "celeste": np.array([-2.55, 0.5, 0.0]),
                    "verde": np.array([2.55, 0.5, 0.0])}
            node_list = VGroup()
            for n, p in vert.items():
                node_list.add(VGroup(
                    Circle(radius=0.86, stroke_width=6, color=COL[n],
                           fill_color="#121C33", fill_opacity=1).move_to(p),
                    Text(n, font=FONT, font_size=27, weight=BOLD, color=COL[n]).move_to(p)))
            arrows = VGroup()
            for a, c in (("rojo", "celeste"), ("celeste", "verde"), ("verde", "rojo")):
                pa, pb = vert[a], vert[c]
                d = (pb - pa) / np.linalg.norm(pb - pa)
                f = CurvedArrow(pa + d * 1.02, pb - d * 1.02, angle=-0.55,
                                color=COL[a], stroke_width=7, tip_length=0.26)
                arrows.add(f)
            b.play(LaggedStart(*[GrowFromCenter(n) for n in node_list], lag_ratio=0.2),
                   run_time=0.8)
            b.play(LaggedStart(*[Create(f)