今天我在互联网找到一个极为令我震惊的 Manim 演示效果,如果不是眼见为实的看到了实际效果,断然不敢相信这是真的,毕竟初中数学课本上没有讲过这个数学知识,同样是九年义务教育,别人怎么这么优秀。

1.引入库文件

Manim 代码的实现,是通过 Python 的代码库,所以代码开始的时候,需要引入相关的函数库:

1
2
from manim import * 
import numpy as np

2.引入相关场景

1
2
class RadialDotsAnimation(Scene):
def construct(self):

3.定义小球数量

1
2
3
4
# 小球数量
num_dots = 48
text_num_dots = Text(f"Number of Dots: {num_dots}").scale(0.5).to_corner(UL)
self.play(Write(text_num_dots))

4.定义小球半径和颜色

1
2
3
4
5
6
7
8
9
10
11
12
13
# 最大半径
max_radius = 2
# 小球颜色列表
colors = [
RED, ORANGE, YELLOW, GREEN, BLUE, PURPLE,
PINK, TEAL, MAROON, GOLD, DARK_BLUE, LIGHT_GRAY,
RED, ORANGE, YELLOW, GREEN, BLUE, PURPLE,
PINK, TEAL, MAROON, GOLD, DARK_BLUE, LIGHT_GRAY,
RED, ORANGE, YELLOW, GREEN, BLUE, PURPLE,
PINK, TEAL, MAROON, GOLD, DARK_BLUE, LIGHT_GRAY,
RED, ORANGE, YELLOW, GREEN, BLUE, PURPLE,
PINK, TEAL, MAROON, GOLD, DARK_BLUE, LIGHT_GRAY
]

5.创建小球和径向直线

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
# 创建小球和它们的径向直线
dots = []
lines = []

for i in range(num_dots):
# 计算每个小球的角度(均匀分布)
angle = i * PI / num_dots

# 创建径向直线(从中心到边缘)
line = Line(
max_radius * np.array([-2*np.cos(angle), -2*np.sin(angle), 0]),
max_radius * np.array([2*np.cos(angle), 2*np.sin(angle), 0]),
color=GRAY,
stroke_opacity=1
)
lines.append(line)

# 小球初始位置(在直线右端点)
initial_pos = line.point_from_proportion(0.5 + 0.5 * np.sin(TAU - angle))
dot = Dot(point=initial_pos, color=colors[i], radius=0.1)
dots.append(dot)

# 先画出所有径向直线
self.play(
LaggedStart(
*[Create(line) for line in lines],
lag_ratio=0.03,
run_time=1.5
)
)

# 显示小球
self.play(
LaggedStart(
*[GrowFromCenter(dot) for dot in dots],
lag_ratio=0.03,
run_time=1.5
)
)
self.wait(0.5)
# 让小球沿径向直线运动,同时整体形成旋转滚动效果
total_cycles = 2 # 总共完成4个周期
animation_time = 6 # 动画总时间

6.创建小球动画

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 创建每个小球的动画
dot_anims = []
for i, (dot, line) in enumerate(zip(dots, lines)):
# 计算每个小球的相位差,实现旋转效果
phase = i * PI / num_dots

def update_dot(dot, alpha, line=line, phase=phase):
# 使用正弦函数计算位置比例,实现往复运动
# 同时添加相位差,使整体产生旋转效果(alpha(0->1))
pos_ratio = 0.5 + 0.5 * np.sin(alpha * total_cycles * TAU - phase)
new_pos = line.point_from_proportion(pos_ratio)
dot.move_to(new_pos)

dot_anims.append(
UpdateFromAlphaFunc(
dot,
update_dot,
run_time=animation_time,
rate_func=linear
)
)

7.播放动画

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
# -*- coding: utf-8 -*-
# 示例:LaggedStart + UpdateFromAlphaFunc 驱动 48 个小球做径向正弦运动(视频 20240902063049000)
# 本地复现:manim -qh 20240902063049000.py RadialDotsAnimation
# 知识点:LaggedStart 让一组动画按 lag_ratio 依次起步;UpdateFromAlphaFunc 把 0→1 的
# alpha 交给自己算位置,所以能写出「正弦往复 + 逐球相位差」的旋转滚动效果。
# 注意:不要改 config.pixel_width/height 或 frame_width/height,-qh 负责 1920x1080/60fps;
# 原文附的竖屏 1080x1440 设置会覆盖它,横屏下直线端点也要相应收进画面。
from manim import *
import numpy as np
class RadialDotsAnimation(Scene):
def construct(self):
num_dots = 48
max_radius = 1.6
colors = [RED, ORANGE, YELLOW, GREEN, BLUE, PURPLE,
PINK, TEAL, MAROON, GOLD, DARK_BLUE, LIGHT_GRAY]
title = Text("Number of Dots: %d" % num_dots, font_size=28).to_corner(UL)
self.play(Write(title), run_time=0.6)
# 48 条过原点的直线,方向角均分半个圆周(PI),端点 ±3.2 收在 1080p 画幅内。
dots, lines = [], []
for i in range(num_dots):
angle = i * PI / num_dots
line = Line(max_radius * np.array([-2 * np.cos(angle), -2 * np.sin(angle), 0]),
max_radius * np.array([2 * np.cos(angle), 2 * np.sin(angle), 0]),
color=GRAY, stroke_opacity=0.35)
lines.append(line)
start = line.point_from_proportion(0.5 + 0.5 * np.sin(TAU - angle))
dots.append(Dot(point=start, color=colors[i % len(colors)], radius=0.085))
self.play(LaggedStart(*[Create(l) for l in lines], lag_ratio=0.01, run_time=1.5))
self.play(LaggedStart(*[GrowFromCenter(d) for d in dots], lag_ratio=0.01, run_time=1.5))
self.wait(0.4)
total_cycles = 2
animation_time = 6.0
dot_anims = []
for i, (dot, line) in enumerate(zip(dots, lines)):
phase = i * PI / num_dots
# 默认参数把 line / phase 绑进闭包,否则循环结束后所有小球共用最后一条直线。
def update_dot(mob, alpha, line=line, phase=phase):
pos_ratio = 0.5 + 0.5 * np.sin(alpha * total_cycles * TAU - phase)
mob.move_to(line.point_from_proportion(pos_ratio))
dot_anims.append(UpdateFromAlphaFunc(dot, update_dot,
run_time=animation_time, rate_func=linear))
# lag_ratio=0:48 个球同时起步,旋转感来自 phase 相位差,而不是起步延迟。
self.play(LaggedStart(*dot_anims, lag_ratio=0, run_time=animation_time))
self.play(LaggedStart(*[FadeOut(d) for d in dots],
*[FadeOut(l) for l in lines],
lag_ratio=0.01, run_time=1.2))
self.wait(0.6)

原谅我简单的分析一些,凑个篇幅,下面来看演示的效果

下载 Python 脚本

8.感谢作者的源代码

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
# -*- coding: utf-8 -*-
# 示例:LaggedStart + UpdateFromAlphaFunc 驱动 48 个小球做径向正弦运动(视频 20240902063049000)
# 本地复现:manim -qh 20240902063049000.py RadialDotsAnimation
# 知识点:LaggedStart 让一组动画按 lag_ratio 依次起步;UpdateFromAlphaFunc 把 0→1 的
# alpha 交给自己算位置,所以能写出「正弦往复 + 逐球相位差」的旋转滚动效果。
# 注意:不要改 config.pixel_width/height 或 frame_width/height,-qh 负责 1920x1080/60fps;
# 原文附的竖屏 1080x1440 设置会覆盖它,横屏下直线端点也要相应收进画面。
from manim import *
import numpy as np
class RadialDotsAnimation(Scene):
def construct(self):
num_dots = 48
max_radius = 1.6
colors = [RED, ORANGE, YELLOW, GREEN, BLUE, PURPLE,
PINK, TEAL, MAROON, GOLD, DARK_BLUE, LIGHT_GRAY]
title = Text("Number of Dots: %d" % num_dots, font_size=28).to_corner(UL)
self.play(Write(title), run_time=0.6)
# 48 条过原点的直线,方向角均分半个圆周(PI),端点 ±3.2 收在 1080p 画幅内。
dots, lines = [], []
for i in range(num_dots):
angle = i * PI / num_dots
line = Line(max_radius * np.array([-2 * np.cos(angle), -2 * np.sin(angle), 0]),
max_radius * np.array([2 * np.cos(angle), 2 * np.sin(angle), 0]),
color=GRAY, stroke_opacity=0.35)
lines.append(line)
start = line.point_from_proportion(0.5 + 0.5 * np.sin(TAU - angle))
dots.append(Dot(point=start, color=colors[i % len(colors)], radius=0.085))
self.play(LaggedStart(*[Create(l) for l in lines], lag_ratio=0.01, run_time=1.5))
self.play(LaggedStart(*[GrowFromCenter(d) for d in dots], lag_ratio=0.01, run_time=1.5))
self.wait(0.4)
total_cycles = 2
animation_time = 6.0
dot_anims = []
for i, (dot, line) in enumerate(zip(dots, lines)):
phase = i * PI / num_dots
# 默认参数把 line / phase 绑进闭包,否则循环结束后所有小球共用最后一条直线。
def update_dot(mob, alpha, line=line, phase=phase):
pos_ratio = 0.5 + 0.5 * np.sin(alpha * total_cycles * TAU - phase)
mob.move_to(line.point_from_proportion(pos_ratio))
dot_anims.append(UpdateFromAlphaFunc(dot, update_dot,
run_time=animation_time, rate_func=linear))
# lag_ratio=0:48 个球同时起步,旋转感来自 phase 相位差,而不是起步延迟。
self.play(LaggedStart(*dot_anims, lag_ratio=0, run_time=animation_time))
self.play(LaggedStart(*[FadeOut(d) for d in dots],
*[FadeOut(l) for l in lines],
lag_ratio=0.01, run_time=1.2))
self.wait(0.6)

我个人观点,代码之中对屏幕的设定可以省略,最后面的代码主要是预防报错,我个人不熟悉代码的作用,知道的朋友可以留言告诉我,谢谢大家,谢谢原作者提供的代码,互联网偶得之,如果侵犯了您的权益,请留言告诉我,再次感谢!

每个小球在做直线运动的同时,整体系统因多个小球的运动方向差异,导致观察者看到的是连续的圆周轨迹。这种现象类似于“视觉暂留”效果。