mirror of
https://github.com/3b1b/manim.git
synced 2025-08-19 13:01:00 +00:00
Updates to VectorField
This commit is contained in:
parent
3c240478b8
commit
ed2e3e80d9
1 changed files with 23 additions and 12 deletions
|
@ -62,10 +62,13 @@ def move_submobjects_along_vector_field(mobject, func):
|
||||||
return mobject
|
return mobject
|
||||||
|
|
||||||
|
|
||||||
def move_points_along_vector_field(mobject, func):
|
def move_points_along_vector_field(mobject, func, coordinate_system):
|
||||||
|
cs = coordinate_system
|
||||||
|
origin = cs.get_origin()
|
||||||
|
|
||||||
def apply_nudge(self, dt):
|
def apply_nudge(self, dt):
|
||||||
self.mobject.apply_function(
|
mobject.apply_function(
|
||||||
lambda p: p + func(p) * dt
|
lambda p: p + (cs.c2p(*func(*cs.p2c(p))) - origin) * dt
|
||||||
)
|
)
|
||||||
mobject.add_updater(apply_nudge)
|
mobject.add_updater(apply_nudge)
|
||||||
return mobject
|
return mobject
|
||||||
|
@ -162,19 +165,21 @@ class StreamLines(VGroup):
|
||||||
self.init_style()
|
self.init_style()
|
||||||
|
|
||||||
def point_func(self, point):
|
def point_func(self, point):
|
||||||
return self.coordinate_system.c2p(
|
in_coords = self.coordinate_system.p2c(point)
|
||||||
*self.func(*self.coordinate_system.p2c(point))
|
out_coords = self.func(*in_coords)
|
||||||
)
|
return self.coordinate_system.c2p(*out_coords)
|
||||||
|
|
||||||
def draw_lines(self):
|
def draw_lines(self):
|
||||||
lines = []
|
lines = []
|
||||||
|
origin = self.coordinate_system.get_origin()
|
||||||
for point in self.get_start_points():
|
for point in self.get_start_points():
|
||||||
points = [point]
|
points = [point]
|
||||||
total_arc_len = 0
|
total_arc_len = 0
|
||||||
# for t in np.arange(0, self.virtual_time, self.dt):
|
time = 0
|
||||||
for x in range(self.max_time_steps):
|
for x in range(self.max_time_steps):
|
||||||
|
time += self.dt
|
||||||
last_point = points[-1]
|
last_point = points[-1]
|
||||||
new_point = last_point + self.dt * self.point_func(last_point)
|
new_point = last_point + self.dt * (self.point_func(last_point) - origin)
|
||||||
points.append(new_point)
|
points.append(new_point)
|
||||||
total_arc_len += get_norm(new_point - last_point)
|
total_arc_len += get_norm(new_point - last_point)
|
||||||
if get_norm(last_point) > self.cutoff_norm:
|
if get_norm(last_point) > self.cutoff_norm:
|
||||||
|
@ -182,8 +187,10 @@ class StreamLines(VGroup):
|
||||||
if total_arc_len > self.arc_len:
|
if total_arc_len > self.arc_len:
|
||||||
break
|
break
|
||||||
line = VMobject()
|
line = VMobject()
|
||||||
|
line.virtual_time = time
|
||||||
step = max(1, int(len(points) / self.n_samples_per_line))
|
step = max(1, int(len(points) / self.n_samples_per_line))
|
||||||
line.set_points_smoothly(points[::step])
|
line.set_points_as_corners(points[::step])
|
||||||
|
line.make_approximately_smooth()
|
||||||
lines.append(line)
|
lines.append(line)
|
||||||
self.set_submobjects(lines)
|
self.set_submobjects(lines)
|
||||||
|
|
||||||
|
@ -220,7 +227,7 @@ class StreamLines(VGroup):
|
||||||
rgbas[:, 3] = self.stroke_opacity
|
rgbas[:, 3] = self.stroke_opacity
|
||||||
line.set_rgba_array(rgbas, "stroke_rgba")
|
line.set_rgba_array(rgbas, "stroke_rgba")
|
||||||
else:
|
else:
|
||||||
self.set_stroke(self.stroke_color)
|
self.set_stroke(self.stroke_color, opacity=self.stroke_opacity)
|
||||||
|
|
||||||
if self.taper_stroke_width:
|
if self.taper_stroke_width:
|
||||||
width = [0, self.stroke_width, 0]
|
width = [0, self.stroke_width, 0]
|
||||||
|
@ -234,7 +241,7 @@ class AnimatedStreamLines(VGroup):
|
||||||
"lag_range": 4,
|
"lag_range": 4,
|
||||||
"line_anim_class": VShowPassingFlash,
|
"line_anim_class": VShowPassingFlash,
|
||||||
"line_anim_config": {
|
"line_anim_config": {
|
||||||
"run_time": 4,
|
# "run_time": 4,
|
||||||
"rate_func": linear,
|
"rate_func": linear,
|
||||||
"time_width": 0.5,
|
"time_width": 0.5,
|
||||||
},
|
},
|
||||||
|
@ -244,7 +251,11 @@ class AnimatedStreamLines(VGroup):
|
||||||
super().__init__(**kwargs)
|
super().__init__(**kwargs)
|
||||||
self.stream_lines = stream_lines
|
self.stream_lines = stream_lines
|
||||||
for line in stream_lines:
|
for line in stream_lines:
|
||||||
line.anim = self.line_anim_class(line, **self.line_anim_config)
|
line.anim = self.line_anim_class(
|
||||||
|
line,
|
||||||
|
run_time=line.virtual_time,
|
||||||
|
**self.line_anim_config,
|
||||||
|
)
|
||||||
line.anim.begin()
|
line.anim.begin()
|
||||||
line.time = -self.lag_range * random.random()
|
line.time = -self.lag_range * random.random()
|
||||||
self.add(line.anim.mobject)
|
self.add(line.anim.mobject)
|
||||||
|
|
Loading…
Add table
Reference in a new issue