流线¶
限定名称: 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)[来源]¶
基础:
VectorFieldStreamLines 表示使用移动代理的轨迹来表示
VectorField的流动。向量场总是基于一个定义每个位置向量的函数。 通过沿着向量场移动许多代理并显示它们的轨迹来展示这个函数的值。
- Parameters:
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) – color_scheme 函数的值将被映射到 colors 中的第一个颜色。较低的值也会导致渐变的第一个颜色。
max_color_scheme_value (float) – color_scheme 函数的值,将被映射到 colors 中的最后一个颜色。较高的值也会导致渐变的最后一个颜色。
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 – 流线的不透明度。
示例
示例:基本用法 ¶
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_overridescolordepthmobject的深度。
fill_color如果有多种颜色(用于渐变),则返回第一个颜色
heightmobject的高度。
n_points_per_curvesheen_factorstroke_colorwidthmobject的宽度。
- _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))。
- Parameters:
func (Callable[[np.ndarray], np.ndarray])
颜色 (ParsableManimColor | 无)
color_scheme (可调用[[np.ndarray], float] | 无)
min_color_scheme_value (float)
max_color_scheme_value (float)
颜色 (序列[可解析的Manim颜色])
x_range (序列[浮点数])
y_range (序列[浮点数])
z_range (序列[浮点数])
three_dimensions (bool)
noise_factor (float | None)
- create(lag_ratio=None, run_time=None, **kwargs)[source]¶
流线的创建动画。
流线以随机顺序出现。
- Parameters:
lag_ratio (float | None) – 动画的滞后比率。 如果未定义,将选择它以使总动画长度为每条流线创建运行时间的1.5倍。
run_time (Callable[[float], float] | None) – 每条流线创建的时间。由于lag_ratio的原因,整个动画的运行时间可能会更长。 如果未定义,则使用流线的虚拟时间作为运行时间。
- Returns:
流线的创建动画。
- Return type:
示例
示例: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()[来源]¶
平滑地结束流线动画。
返回一个动画,使流线完全显示,没有明显的切割。
- Returns:
动画淡出正在运行的流动画。
- Return type:
- Raises:
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]¶
使用更新器动画化流线。
流线将持续流动
- Parameters:
warm_up (bool) – 如果 True,动画将逐行初始化。否则,它将显示所有行。
flow_speed (float) – 在 flow_speed=1 时,流动每秒移动的距离等于沿其路径的矢量场的大小。速度值会缩放此流动的速度。
time_width (float) – 动画时显示的流线比例
rate_func (Callable[[float], float]) – 每条流线闪烁的速率函数
line_animation_class (type[ShowPassingFlash]) – 使用的动画类
- Return type:
无
示例
示例: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)