三角函数摩天轮动画

Tạo vào: 21 tháng 1, 2025

Trả lời bằng O1 bởi Chat01

Câu hỏi

请使用 manim community v0.19.0 制作一个小学生能看的,关于三角函数的可视化动画,可视化要求贴近现实拟物

Suy nghĩ

Piecing together a script

I'm crafting a Manim script for a "triangle function" animation, focusing on sines, cosines, and tangents, with realistic, real-world-like visuals for middle-school or elementary students.

Envisioning scenes

I'm thinking through creating engaging scenes with a rotating "toy" object on a circle, visually linking it to waves, and integrating a Ferris wheel or a boat on waves for a dynamic learning experience.

Mapping the animation

I’m working on a Manim script to animate a Ferris wheel. It includes a circle, a rotating car, and a sine wave showing vertical position over time, all explained in Chinese.

Laying out the scene

I’m crafting a Manim script for a Ferris wheel animation. It involves a circle, a rotating car, a line for vertical position, a sine wave graph, and realistic textures, all in Chinese.

Adding background elements

I’m crafting a gradient sky background for the Ferris wheel scene, using a rectangle with a sky blue to white gradient, and planning to incorporate realistic ground elements.

Crafting the scene

I’m building a Ferris wheel scene with a grass rectangle, a large circle for the Ferris wheel, a rotating seat, a sine wave graph, and a label for the y coordinate, all in Chinese.

Crafting the Ferris wheel

I’m highlighting the Ferris wheel scene: the step-by-step process involves setting the sky, integrating the Ferris wheel, and sequentially adding crucial elements like rotating seats and sine waves, all clearly explained in Chinese.

Updating and animating

I'm setting up the seat's y-position with ValueTracker, linking the wave's function to the angle, and ensuring the animation is engaging for children, coded in Chinese.

Thought about 三角函数可视化动画,持续 51 秒

Trả lời

下面给出一个示例代码,使用 Manim Community v0.19.0 来演示三角函数(以正弦函数为例)和生活中“摩天轮”运动之间的关系。此示例较贴近现实场景,同时通过动画的形式帮助小学生理解正弦函数与圆周运动之间的联系。你可以在此基础上根据自己的需求进行修改,或者补充文字解释、背景音乐等。


动画效果概述

  1. 背景与地面:使用简单的纯色背景,底部加一条“草地”矩形来模拟地面。
  2. 标题:顶端显示“三角函数与摩天轮”的标题。
  3. 摩天轮:在左侧绘制一个圆形代表摩天轮,并用一个小圆点(代表座舱)在圆上匀速转动。
  4. 正弦曲线坐标系:在右侧绘制一个坐标系,并实时描点出正弦波,展示座舱在摩天轮上随时间变化的“高度”对应如何映射到正弦函数曲线上。
  5. 动态演示:让座舱围绕圆心旋转一周,同时在右侧画出对应的正弦波。这样学生可以直观地看到“圆周运动的高度变化”就是正弦函数的波动。

使用方法

  1. 将下面的代码保存为 ferris_wheel_sin.py(或你喜欢的名字)。

  2. 在命令行 / 终端中使用 Manim v0.19.0 运行,例如:

    bash
    manim -pql ferris_wheel_sin.py FerrisWheelScene
    • -pql 表示使用低分辨率快速渲染并自动播放(可以根据需要调整为 -pqh 以高分辨率渲染,或移除 p 防止自动播放)。
    • 如果你使用的命令或参数有所不同,请根据实际情况调整。
  3. 执行后,会生成一段动画视频,让你看到摩天轮和正弦函数的对应关系。


完整代码示例

python
from manim import * class FerrisWheelScene(Scene): def construct(self): # 设置背景颜色(天空蓝) self.camera.background_color = "#87CEEB" # 标题 title = Text("三角函数与摩天轮", font="思源黑体", color=WHITE).to_edge(UP) self.play(Write(title)) self.wait(0.5) # 绘制地面 ground = Rectangle( width=config.frame_width, height=1.0, fill_color=GREEN, fill_opacity=1.0 ).to_edge(DOWN) self.play(FadeIn(ground)) self.wait(0.5) # 摩天轮参数 wheel_center = LEFT * 3 + DOWN * 1.2 # 圆心位置(左下方) wheel_radius = 2 # 摩天轮半径 # 绘制摩天轮(圆形) ferris_wheel = Circle( radius=wheel_radius, color=WHITE, stroke_width=4 ).move_to(wheel_center) self.play(Create(ferris_wheel)) self.wait(0.5) # 在摩天轮圆中增加几条简单的“辐条”,让其更像摩天轮 spokes = VGroup() for angle_deg in [0, 45, 90, 135, 180, 225, 270, 315]: rad = angle_deg * DEGREES line = Line( start=wheel_center, end=wheel_center + wheel_radius * np.array([np.cos(rad), np.sin(rad), 0]), stroke_color=WHITE, stroke_width=2 ) spokes.add(line) self.play(Create(spokes)) self.wait(0.5) # 座舱(小圆点) cabin = Dot(color=RED) cabin.move_to(wheel_center + np.array([wheel_radius, 0, 0])) # 初始在圆的最右侧 self.play(FadeIn(cabin)) # 在右侧建立坐标系,用于展示正弦波 axes = Axes( x_range=[0, 2 * PI, 1], y_range=[-2, 2, 1], x_length=5, y_length=4, axis_config={"include_tip": True, "stroke_width": 2}, tips=False ) axes.shift(RIGHT * 2.5 + DOWN * 0.3) # 移动到右侧 x_label = axes.get_x_axis_label("θ(弧度)") y_label = axes.get_y_axis_label("sin(θ)") self.play(Create(axes), FadeIn(x_label), FadeIn(y_label)) self.wait(0.5) # 用 ValueTracker 来跟踪旋转角度(从 0 -> 2π) theta_tracker = ValueTracker(0) # 绘制正弦函数曲线(动态更新) # 为了让曲线可以跟随 theta 的进度绘制,我们使用 ParametricFunction 并限制 domain sin_curve = always_redraw( lambda: axes.plot( lambda x: np.sin(x), x_range=[0, theta_tracker.get_value()], color=BLUE ) ) self.add(sin_curve) # 在正弦曲线上的追踪点 moving_dot = always_redraw( lambda: Dot( axes.coords_to_point( theta_tracker.get_value(), np.sin(theta_tracker.get_value()) ), color=YELLOW ) ) self.add(moving_dot) # 为座舱和正弦点建立位置更新函数 # 座舱:沿摩天轮做圆周运动 def update_cabin(mob): current_theta = theta_tracker.get_value() x = wheel_center[0] + wheel_radius * np.cos(current_theta) y = wheel_center[1] + wheel_radius * np.sin(current_theta) mob.move_to([x, y, 0]) cabin.add_updater(update_cabin) # 开始让 theta 从 0 逐渐变到 2π,观察动画 self.play(theta_tracker.animate.set_value(2 * PI), run_time=6, rate_func=linear) # 停止更新器 cabin.remove_updater(update_cabin) self.wait(1) self.play(FadeOut(cabin), FadeOut(sin_curve), FadeOut(moving_dot), FadeOut(spokes), FadeOut(ferris_wheel), FadeOut(axes), FadeOut(x_label), FadeOut(y_label), FadeOut(title), FadeOut(ground)) self.wait(0.5)

代码说明(关键步骤)

  1. 背景与地面

    • self.camera.background_color = "#87CEEB" 用于设置天空蓝的背景色。
    • 使用 Rectangle 作为“地面”并放在屏幕底部。
  2. 标题

    • Text("三角函数与摩天轮", font="思源黑体") 创建文字对象,并通过 to_edge(UP) 放在顶端。
  3. 摩天轮与辐条

    • 使用 Circle 绘制摩天轮外圈。
    • for 循环生成多条“辐条”(Line),让它看起来更像摩天轮。
  4. 座舱(小圆点)

    • 用一个 Dot 代表座舱。
    • 初始位置在圆的最右侧(与圆心同水平线)。
  5. 坐标系与正弦曲线

    • Axes 对象用于创建一个坐标系,x 轴范围设为 [0,2π][0, 2\pi],y 轴范围设为 [2,2][-2, 2]
    • 在坐标系内用 plot(lambda x: np.sin(x)) 来画正弦曲线。
    • 通过 theta_trackerValueTracker)实现动态更新正弦曲线的绘制范围和“追踪点”的位置(moving_dot)。
  6. 核心动画原理

    • theta_tracker 的值从 0 匀速增加到
    • 座舱的位置由 (cos(θ),sin(θ))(\cos(\theta), \sin(\theta)) 决定,并实时更新,使之在圆上旋转。
    • 同时在右侧坐标系中,用红色点和蓝色正弦曲线反映“高度(即 sin(θ)\sin(\theta))”随 θ\theta 的变化。

通过这个动画,小学生可以直观地看到:

  • 座舱在摩天轮上随着时间转动时,其“上下移动”正好对应了一条正弦波。
  • 圆周运动和三角函数之间存在直接的几何联系。

你可以根据需要加上更多的文字说明、箭头标记、图形对齐、美化等,让动画更符合小学生的兴趣和理解习惯。祝你创作愉快,教学顺利!

Chia sẻ Q&A này