from manim import *
import numpy as np
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)
class NumberLineN2P(Scene):
    def construct(self):
        self.add(head("NumberLine：n2p 与 p2n 互转", "add_updater / ValueTracker"))
        line = NumberLine(x_range=[-6, 6, 1], length=10).shift(DOWN * 0.2)
        line.add_numbers(font_size=20)
        x = ValueTracker(-4.0)
        dot = always_redraw(lambda: Dot(line.n2p(x.get_value()), color=YELLOW, radius=0.14))
        txt = always_redraw(lambda: Text("x = %.2f" % line.p2n(dot.get_center()),
                                         font="Consolas", font_size=26, color=YELLOW).to_edge(UP).shift(DOWN * 1.0))
        self.add(line, dot, txt)
        self.play(x.animate.set_value(4.0), run_time=3.6, rate_func=there_and_back)
        self.play(FadeIn(foot("n2p 把数值转坐标，p2n 把坐标读回数值")), run_time=0.4)
        self.wait(1.9)
