from manim import *
import numpy as np
import sympy as sp
def head(cn, en):
    t = Text(cn, font="STLiti", font_size=27).to_edge(UP)
    s = Text(en, font="Consolas", font_size=20, color=YELLOW).next_to(t, DOWN, buff=0.10)
    return VGroup(t, s)
def foot(cn):
    return Text(cn, font="STLiti", font_size=20, color=YELLOW).to_edge(DOWN)
def bg_image(h=60, w=100, mode=0):
    a = np.zeros((h, w, 4), dtype=np.uint8)
    a[:, :, 3] = 255
    if mode == 0:
        a[:, :, 0] = np.linspace(10, 90, w, dtype=np.uint8)[None, :]
        a[:, :, 2] = np.linspace(60, 160, w, dtype=np.uint8)[None, :]
    elif mode == 1:
        a[:, :, 1] = np.linspace(20, 110, h, dtype=np.uint8)[:, None]
        a[:, :, 2] = np.linspace(40, 130, h, dtype=np.uint8)[:, None]
    else:
        a[:, :, 0] = 120
        a[:, :, 1] = 70
        a[:, :, 2] = np.linspace(30, 140, w, dtype=np.uint8)[None, :]
    return ImageMobject(a)
class RateCustom(Scene):
    def construct(self):
        self.add(head("速率曲线：自定义函数", "rate_func = t ** 3"))
        axes = Axes(x_range=[0, 1, 0.25], y_range=[0, 1, 0.25], x_length=5, y_length=4,
                    axis_config={"color": GREY_B}).shift(LEFT * 3)
        g1 = axes.plot(lambda t: t, color=BLUE)
        g2 = axes.plot(lambda t: t**3, color=YELLOW)
        self.add(axes, g1, g2)
        d = Dot(color=RED, radius=0.16).shift(RIGHT * 3 + DOWN * 1.5)
        self.add(d)
        self.play(d.animate(rate_func=lambda t: t**3).shift(UP * 3), run_time=4.2)
        self.play(FadeIn(foot("t 的三次幂：起步极慢，末段突然加速")), run_time=0.4)
        self.wait(1.4)
