StreamLines¶
全限定名称: manim.mobject.vector\_field.StreamLines
- class StreamLines(func, color=None, color_scheme=None, min_color_scheme_value=0, max_color_scheme_value=2, colors=[ManimColor('#236B8E'), ManimColor('#83C167'), ManimColor('#FFFF00'), ManimColor('#FC6255')], x_range=None, y_range=None, z_range=None, three_dimensions=False, noise_factor=None, n_repeats=1, dt=0.05, virtual_time=3, max_anchors_per_line=100, padding=3, stroke_width=1, opacity=1, **kwargs)[source]¶
基类:
VectorField
StreamLines (流线) 通过移动粒子的轨迹来表示
VectorField
(矢量场) 的流动。矢量场总是基于一个函数,该函数定义了每个位置的向量。通过让许多粒子沿着矢量场移动并显示其轨迹来展示该函数的值。
- 参数:
func (Callable[[np.ndarray], np.ndarray]) – 定义矢量场中每个位置变化率的函数。
color (ParsableManimColor | None) – 矢量场的颜色。如果设置,则禁用位置特定着色。
color_scheme (Callable[[np.ndarray], float] | None) – 一个将向量映射到单个值的函数。该值表示在使用 min_color_scheme_value、max_color_scheme_value 和 colors 定义的颜色梯度中的位置。
min_color_scheme_value (*float*) – 要映射到 colors 中第一个颜色的 color_scheme 函数的值。较低的值也会导致渐变显示第一个颜色。
max_color_scheme_value (*float*) – 要映射到 colors 中最后一个颜色的 color_scheme 函数的值。较高的值也会导致渐变显示最后一个颜色。
colors (Sequence[ParsableManimColor]) – 定义矢量场颜色梯度的颜色。
x_range (Sequence[float]) – x_min、x_max、delta_x 的序列
y_range (Sequence[float]) – y_min、y_max、delta_y 的序列
z_range (Sequence[float]) – z_min、z_max、delta_z 的序列
three_dimensions (bool) – 启用三维。默认为 False,如果 z_range 不为 None 则自动变为 True。
noise_factor (float | None) – 每个粒子的起始位置沿每个轴线改变的量。如果未定义,默认为
delta_y / 2
。n_repeats – 在每个起始点生成的粒子数量。
dt – 每个步骤中粒子移动距离的拉伸因子。值越低,对矢量场中轨迹的近似越好。
virtual_time – 粒子在矢量场中移动的虚拟时间。因此,值越高会导致流线越长。但是,在创建时会模拟整个时间。
max_anchors_per_line – 每条线上的最大锚点数。锚点更多的线条会降低复杂度,而不是长度。
padding – 粒子在被终止之前可以移出生成区域的距离。
stroke_width – 流线的描边宽度。
opacity – 流线的不透明度。
示例
示例: BasicUsage ¶
from manim import * class BasicUsage(Scene): def construct(self): func = lambda pos: ((pos[0] * UR + pos[1] * LEFT) - pos) / 3 self.add(StreamLines(func))
class BasicUsage(Scene): def construct(self): func = lambda pos: ((pos[0] * UR + pos[1] * LEFT) - pos) / 3 self.add(StreamLines(func))
示例: SpawningAndFlowingArea ¶
from manim import * class SpawningAndFlowingArea(Scene): def construct(self): func = lambda pos: np.sin(pos[0]) * UR + np.cos(pos[1]) * LEFT + pos / 5 stream_lines = StreamLines( func, x_range=[-3, 3, 0.2], y_range=[-2, 2, 0.2], padding=1 ) spawning_area = Rectangle(width=6, height=4) flowing_area = Rectangle(width=8, height=6) labels = [Tex("Spawning Area"), Tex("Flowing Area").shift(DOWN * 2.5)] for lbl in labels: lbl.add_background_rectangle(opacity=0.6, buff=0.05) self.add(stream_lines, spawning_area, flowing_area, *labels)
class SpawningAndFlowingArea(Scene): def construct(self): func = lambda pos: np.sin(pos[0]) * UR + np.cos(pos[1]) * LEFT + pos / 5 stream_lines = StreamLines( func, x_range=[-3, 3, 0.2], y_range=[-2, 2, 0.2], padding=1 ) spawning_area = Rectangle(width=6, height=4) flowing_area = Rectangle(width=8, height=6) labels = [Tex("Spawning Area"), Tex("Flowing Area").shift(DOWN * 2.5)] for lbl in labels: lbl.add_background_rectangle(opacity=0.6, buff=0.05) self.add(stream_lines, spawning_area, flowing_area, *labels)
方法
流线的创建动画。
平滑地结束流线动画。
使用更新器为流线添加动画。
属性
animate (动画)
用于动画化
self
的任何方法的应用。animation_overrides (动画覆盖)
颜色
depth (深度)
mobject 的深度。
fill_color (填充颜色)
如果存在多种颜色(用于渐变),则返回第一种颜色
height (高度)
mobject 的高度。
n_points_per_curve (每条曲线的点数)
sheen_factor (光泽因子)
stroke_color (描边颜色)
width (宽度)
mobject 的宽度。
- _original__init__(func, color=None, color_scheme=None, min_color_scheme_value=0, max_color_scheme_value=2, colors=[ManimColor('#236B8E'), ManimColor('#83C167'), ManimColor('#FFFF00'), ManimColor('#FC6255')], x_range=None, y_range=None, z_range=None, three_dimensions=False, noise_factor=None, n_repeats=1, dt=0.05, virtual_time=3, max_anchors_per_line=100, padding=3, stroke_width=1, opacity=1, **kwargs)¶
初始化自身。有关准确签名,请参阅 help(type(self))。
- 参数:
func (*Callable*[[*np.ndarray*], *np.ndarray*])
color (ParsableManimColor | None)
color_scheme (*Callable*[[*np.ndarray*], *float*] *|* *None*)
min_color_scheme_value (*float*)
max_color_scheme_value (*float*)
colors (*Sequence*[[ParsableManimColor]])
x_range (Sequence[float])
y_range (Sequence[float])
z_range (Sequence[float])
three_dimensions (bool)
noise_factor (float | None)
- create(lag_ratio=None, run_time=None, **kwargs)[source]¶
流线的创建动画。
流线以随机顺序出现。
- 参数:
lag_ratio (float | None) – 动画的滞后比例。如果未定义,将选择该比例,使总动画长度是每条流线创建运行时间的 1.5 倍。
run_time (Callable[[float], float] | None) – 每条流线创建的运行时间。由于 lag_ratio,整个动画的运行时间可能会更长。如果未定义,则使用流线的虚拟时间作为运行时间。
- 返回:
流线的创建动画。
- 返回类型:
示例
示例: StreamLineCreation ¶
from manim import * class StreamLineCreation(Scene): def construct(self): func = lambda pos: (pos[0] * UR + pos[1] * LEFT) - pos stream_lines = StreamLines( func, color=YELLOW, x_range=[-7, 7, 1], y_range=[-4, 4, 1], stroke_width=3, virtual_time=1, # use shorter lines max_anchors_per_line=5, # better performance with fewer anchors ) self.play(stream_lines.create()) # uses virtual_time as run_time self.wait()
class StreamLineCreation(Scene): def construct(self): func = lambda pos: (pos[0] * UR + pos[1] * LEFT) - pos stream_lines = StreamLines( func, color=YELLOW, x_range=[-7, 7, 1], y_range=[-4, 4, 1], stroke_width=3, virtual_time=1, # use shorter lines max_anchors_per_line=5, # better performance with fewer anchors ) self.play(stream_lines.create()) # uses virtual_time as run_time self.wait()
- end_animation()[source]¶
平滑地结束流线动画。
返回一个动画,使流线完全显示而没有明显的截断。
- 返回:
正在运行的流动画淡出。
- 返回类型:
- 抛出:
ValueError – 如果没有流线动画正在运行
示例
示例: EndAnimation ¶
from manim import * class EndAnimation(Scene): def construct(self): func = lambda pos: np.sin(pos[0] / 2) * UR + np.cos(pos[1] / 2) * LEFT stream_lines = StreamLines( func, stroke_width=3, max_anchors_per_line=5, virtual_time=1, color=BLUE ) self.add(stream_lines) stream_lines.start_animation(warm_up=False, flow_speed=1.5, time_width=0.5) self.wait(1) self.play(stream_lines.end_animation())
class EndAnimation(Scene): def construct(self): func = lambda pos: np.sin(pos[0] / 2) * UR + np.cos(pos[1] / 2) * LEFT stream_lines = StreamLines( func, stroke_width=3, max_anchors_per_line=5, virtual_time=1, color=BLUE ) self.add(stream_lines) stream_lines.start_animation(warm_up=False, flow_speed=1.5, time_width=0.5) self.wait(1) self.play(stream_lines.end_animation())
- start_animation(warm_up=True, flow_speed=1, time_width=0.3, rate_func=<function linear>, line_animation_class=<class 'manim.animation.indication.ShowPassingFlash'>, **kwargs)[source]¶
使用更新器为流线添加动画。
流线将持续流动
- 参数:
warm_up (bool) – 如果为 True,则动画逐行初始化。否则,它将从显示所有线条开始。
flow_speed (float) – 当 flow_speed=1 时,每秒流动的距离等于矢量场沿其路径的幅度。速度值会缩放此流动的速度。
time_width (float) – 动画时显示的流线比例
rate_func (Callable[[float], float]) – 每条流线闪烁的速率函数
line_animation_class (type[ShowPassingFlash]) – 正在使用的动画类
- 返回类型:
无
示例
示例: ContinuousMotion ¶
from manim import * class ContinuousMotion(Scene): def construct(self): func = lambda pos: np.sin(pos[0] / 2) * UR + np.cos(pos[1] / 2) * LEFT stream_lines = StreamLines(func, stroke_width=3, max_anchors_per_line=30) self.add(stream_lines) stream_lines.start_animation(warm_up=False, flow_speed=1.5) self.wait(stream_lines.virtual_time / stream_lines.flow_speed)
class ContinuousMotion(Scene): def construct(self): func = lambda pos: np.sin(pos[0] / 2) * UR + np.cos(pos[1] / 2) * LEFT stream_lines = StreamLines(func, stroke_width=3, max_anchors_per_line=30) self.add(stream_lines) stream_lines.start_animation(warm_up=False, flow_speed=1.5) self.wait(stream_lines.virtual_time / stream_lines.flow_speed)