mirror of
https://github.com/3b1b/manim.git
synced 2025-08-05 16:49:03 +00:00
Simplify initialization of Line with path arc
This commit is contained in:
parent
5f41e238ba
commit
b53ab02675
1 changed files with 19 additions and 29 deletions
|
@ -451,6 +451,7 @@ class Line(TipableVMobject):
|
||||||
):
|
):
|
||||||
super().__init__(**kwargs)
|
super().__init__(**kwargs)
|
||||||
self.path_arc = path_arc
|
self.path_arc = path_arc
|
||||||
|
self.buff = buff
|
||||||
self.set_start_and_end_attrs(start, end)
|
self.set_start_and_end_attrs(start, end)
|
||||||
self.set_points_by_ends(self.start, self.end, buff, path_arc)
|
self.set_points_by_ends(self.start, self.end, buff, path_arc)
|
||||||
|
|
||||||
|
@ -461,31 +462,15 @@ class Line(TipableVMobject):
|
||||||
buff: float = 0,
|
buff: float = 0,
|
||||||
path_arc: float = 0
|
path_arc: float = 0
|
||||||
) -> Self:
|
) -> Self:
|
||||||
vect = end - start
|
self.clear_points()
|
||||||
dist = get_norm(vect)
|
self.start_new_path(start)
|
||||||
if np.isclose(dist, 0):
|
self.add_arc_to(end, path_arc)
|
||||||
self.set_points_as_corners([start, end])
|
|
||||||
return self
|
|
||||||
if path_arc:
|
|
||||||
neg = path_arc < 0
|
|
||||||
if neg:
|
|
||||||
path_arc = -path_arc
|
|
||||||
start, end = end, start
|
|
||||||
radius = (dist / 2) / math.sin(path_arc / 2)
|
|
||||||
alpha = (PI - path_arc) / 2
|
|
||||||
center = start + radius * normalize(rotate_vector(end - start, alpha))
|
|
||||||
|
|
||||||
raw_arc_points = quadratic_bezier_points_for_arc(path_arc - 2 * buff / radius)
|
# Apply buffer
|
||||||
rot_matrix = rotation_about_z(angle_of_vector(start - center) + buff / radius)
|
if buff > 0:
|
||||||
raw_arc_points = raw_arc_points @ rot_matrix.T
|
length = self.get_arc_length()
|
||||||
if neg:
|
alpha = min(buff / length, 0.5)
|
||||||
raw_arc_points = raw_arc_points[::-1]
|
self.pointwise_become_partial(self, alpha, 1 - alpha)
|
||||||
self.set_points(center + radius * raw_arc_points)
|
|
||||||
else:
|
|
||||||
if buff > 0 and dist > 0:
|
|
||||||
start = start + vect * (buff / dist)
|
|
||||||
end = end - vect * (buff / dist)
|
|
||||||
self.set_points_as_corners([start, end])
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def set_path_arc(self, new_value: float) -> Self:
|
def set_path_arc(self, new_value: float) -> Self:
|
||||||
|
@ -666,6 +651,7 @@ class Arrow(Line):
|
||||||
self.tip_len_to_width = tip_len_to_width
|
self.tip_len_to_width = tip_len_to_width
|
||||||
self.max_tip_length_to_length_ratio = max_tip_length_to_length_ratio
|
self.max_tip_length_to_length_ratio = max_tip_length_to_length_ratio
|
||||||
self.max_width_to_length_ratio = max_width_to_length_ratio
|
self.max_width_to_length_ratio = max_width_to_length_ratio
|
||||||
|
self.n_tip_points = 3
|
||||||
super().__init__(
|
super().__init__(
|
||||||
start, end,
|
start, end,
|
||||||
stroke_color=stroke_color,
|
stroke_color=stroke_color,
|
||||||
|
@ -694,10 +680,13 @@ class Arrow(Line):
|
||||||
alpha = self.max_tip_length_to_length_ratio
|
alpha = self.max_tip_length_to_length_ratio
|
||||||
else:
|
else:
|
||||||
alpha = tip_len / arc_len
|
alpha = tip_len / arc_len
|
||||||
self.pointwise_become_partial(self, 0, 1 - alpha)
|
|
||||||
# Dumb that this is needed
|
if self.path_arc > 0 and self.buff > 0:
|
||||||
self.start_new_path(self.point_from_proportion(1 - 1e-5))
|
self.insert_n_curves(10) # Is this needed?
|
||||||
|
self.pointwise_become_partial(self, 0.0, 1.0 - alpha)
|
||||||
|
self.append_points([self.get_end(), self.get_end()])
|
||||||
self.add_line_to(prev_end)
|
self.add_line_to(prev_end)
|
||||||
|
self.n_tip_points = 3
|
||||||
return self
|
return self
|
||||||
|
|
||||||
@Mobject.affects_data
|
@Mobject.affects_data
|
||||||
|
@ -708,8 +697,9 @@ class Arrow(Line):
|
||||||
float(self.get_stroke_width()),
|
float(self.get_stroke_width()),
|
||||||
self.max_width_to_length_ratio * self.get_length(),
|
self.max_width_to_length_ratio * self.get_length(),
|
||||||
)
|
)
|
||||||
self.data['stroke_width'][:-3] = self.data['stroke_width'][0]
|
ntp = self.n_tip_points
|
||||||
self.data['stroke_width'][-3:, 0] = tip_width * np.linspace(1, 0, 3)
|
self.data['stroke_width'][:-ntp] = self.data['stroke_width'][0]
|
||||||
|
self.data['stroke_width'][-ntp:, 0] = tip_width * np.linspace(1, 0, ntp)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def reset_tip(self) -> Self:
|
def reset_tip(self) -> Self:
|
||||||
|
|
Loading…
Add table
Reference in a new issue