正切函数的作图展示——manim

视频效果

代码

from manim import *
import math

class TangentFunctionDemo(Scene):
    def construct(self):
        # 创建坐标轴
        axes = Axes(
            x_range=[-3*PI/2, 3*PI/2, PI/2],
            y_range=[-5,5,1],
            x_length=10,
            y_length=6,
            tips=False,
            axis_config={"include_ticks": True, "font_size": 24},
        ).shift(DOWN*0.5)

        # 标记刻度
        labels = [
            ( -3*PI/2, r"-\frac{3\pi}{2}" ),
            ( -PI,      r"-\pi"           ),
            ( -PI/2,    r"-\frac{\pi}{2}" ),
            ( 0,        "0"               ),
            ( PI/2,     r"\frac{\pi}{2}"  ),
            ( PI,       r"\pi"            ),
            ( 3*PI/2,   r"\frac{3\pi}{2}" )
        ]
        x_labels = VGroup(
            *[ MathTex(tex).next_to(axes.c2p(x_val,0), DOWN) for x_val, tex in labels ]
        )

        self.play(Create(axes), Write(x_labels))

        # 创建渐近线:tan(x)在 x=±pi/2, ±3pi/2 处有垂直渐近线
        asymptote_positions = [-3*PI/2, -PI/2, PI/2, 3*PI/2]
        asymptotes = VGroup()
        for pos in asymptote_positions:
            line = DashedLine(
                start=axes.c2p(pos, -5),
                end=axes.c2p(pos, 5),
                color=GRAY
            )
            asymptotes.add(line)
        self.play(Create(asymptotes))
        self.wait(1)

        # 找出主区间对应的渐近线 x = -pi/2 和 x = pi/2
        # 使用 math.isclose 确保浮点比较安全
        boundary_lines = []
        for line in asymptotes:
            x_coord = axes.p2c(line.get_start())[0]
            if math.isclose(x_coord, -PI/2, rel_tol=1e-9) or math.isclose(x_coord, PI/2, rel_tol=1e-9):
                boundary_lines.append(line)

        # Step 1: 对称性演示
        # 1.1 仅绘制(0, pi/2)区间的tan(x) (蓝色)
        pos_graph = axes.plot(
            lambda x: math.tan(x),
            x_range=(0.01, PI/2 - 0.1),
            color=BLUE
        )
        self.play(Create(pos_graph))
        self.wait(1)

        # 1.2 将这段曲线复制并绕原点旋转180度,使用黄色
        neg_graph = pos_graph.copy().set_color(YELLOW)
        self.play(Rotate(neg_graph, angle=PI, about_point=axes.c2p(0,0)))
        self.wait(1)

        # 合并得到(-pi/2, pi/2)区间的完整图像
        main_graph = VGroup(neg_graph, pos_graph)

        # 突出主周期区间的渐近线(x=-pi/2和x=pi/2)
        self.play(*[Indicate(line) for line in boundary_lines])
        self.wait(1)

        # Step 2: 周期性演示(不动坐标轴)
        # 将main_graph的复制品向右平移π,从[-pi/2, pi/2]到[pi/2, 3pi/2]
        shift_amount = axes.x_axis.unit_size * PI
        shift_graph = main_graph.copy()

        # 在动画中实现平移和变色(红色)
        self.play(shift_graph.animate.shift(RIGHT * shift_amount).set_color(RED), run_time=2)
        self.wait(1)

        # 原图像main_graph保留在原位,shift_graph为位移后的红色曲线

        # Step 3: 延展性演示
        # 在两侧添加更多周期单元(绿色)
        # 左侧:[-3pi/2, -pi/2], 右侧:[3pi/2, 5pi/2]
        left_extension = main_graph.copy().set_color(GREEN).shift(LEFT * shift_amount)
        right_extension = main_graph.copy().set_color(GREEN).shift(RIGHT * 2 * shift_amount)
        self.play(Create(left_extension), Create(right_extension))
        self.wait(2)

        # 这里根据需要可以添加收尾动画
        self.play(
            FadeOut(left_extension), 
            FadeOut(right_extension), 
            FadeOut(shift_graph), 
            FadeOut(main_graph),
            FadeOut(asymptotes),
            FadeOut(x_labels), 
            FadeOut(axes)
        )
        self.wait(1)

评论

  1. hgj
    Android Chrome
    6 天前
    2025-3-03 8:51:03

    请问作者这个视频是怎么嵌入博客中的啊

    • 博主
      hgj
      Windows Chrome
      3 天前
      2025-3-05 23:31:28

      WordPress自带了,图片

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇