mirror of
https://github.com/3b1b/manim.git
synced 2025-08-05 16:49:03 +00:00
Factored out Line.get_start and Line.get_end to be more general Mobject methods
This commit is contained in:
parent
5c7f1b15c0
commit
0ecac2bfb4
3 changed files with 30 additions and 38 deletions
|
@ -327,37 +327,17 @@ class Line(VMobject):
|
|||
else:
|
||||
return self.get_length()
|
||||
|
||||
def get_start_and_end(self):
|
||||
return self.get_start(), self.get_end()
|
||||
|
||||
def get_vector(self):
|
||||
return self.get_end() - self.get_start()
|
||||
|
||||
def get_unit_vector(self):
|
||||
vect = self.get_vector()
|
||||
norm = get_norm(vect)
|
||||
if norm == 0:
|
||||
# TODO, is this the behavior I want?
|
||||
return np.array(ORIGIN)
|
||||
return vect / norm
|
||||
|
||||
def get_start(self):
|
||||
return np.array(self.points[0])
|
||||
|
||||
def get_end(self):
|
||||
return np.array(self.points[-1])
|
||||
|
||||
def get_slope(self):
|
||||
start, end = self.get_start_and_end()
|
||||
rise, run = [
|
||||
float(end[i] - start[i])
|
||||
for i in [1, 0]
|
||||
]
|
||||
return np.inf if run == 0 else rise / run
|
||||
return normalize(self.get_vector())
|
||||
|
||||
def get_angle(self):
|
||||
start, end = self.get_start_and_end()
|
||||
return angle_of_vector(end - start)
|
||||
return angle_of_vector(self.get_vector())
|
||||
|
||||
def get_slope(self):
|
||||
return np.tan(self.get_angle())
|
||||
|
||||
def set_angle(self, angle):
|
||||
self.rotate(
|
||||
|
|
|
@ -535,8 +535,20 @@ class Mobject(Container):
|
|||
self.scale_in_place((length + buff) / length)
|
||||
return self
|
||||
|
||||
def get_start(self):
|
||||
self.throw_error_if_no_points()
|
||||
return np.array(self.points[0])
|
||||
|
||||
def get_end(self):
|
||||
self.throw_error_if_no_points()
|
||||
return np.array(self.points[-1])
|
||||
|
||||
def get_start_and_end(self):
|
||||
return self.get_start(), self.get_end()
|
||||
|
||||
def put_start_and_end_on(self, start, end):
|
||||
curr_vect = self.points[-1] - self.points[0]
|
||||
curr_start, curr_end = self.get_start_and_end()
|
||||
curr_vect = curr_end - curr_start
|
||||
if np.all(curr_vect == 0):
|
||||
raise Exception("Cannot position endpoints of closed loop")
|
||||
target_vect = end - start
|
||||
|
@ -545,7 +557,7 @@ class Mobject(Container):
|
|||
angle_of_vector(target_vect) -
|
||||
angle_of_vector(curr_vect)
|
||||
)
|
||||
self.shift(start - self.points[0])
|
||||
self.shift(start - self.get_start())
|
||||
return self
|
||||
|
||||
# Background rectangle
|
||||
|
@ -1032,6 +1044,17 @@ class Mobject(Container):
|
|||
sm1.interpolate_color(sm1, sm2, 1)
|
||||
return self
|
||||
|
||||
# Errors
|
||||
def has_no_points(self):
|
||||
return len(self.points) == 0
|
||||
|
||||
def throw_error_if_no_points(self):
|
||||
if self.has_no_points():
|
||||
message = "Cannot call Mobject.{}" +\
|
||||
"for a Mobject with no points"
|
||||
caller_name = sys._getframe(1).f_code.co_name
|
||||
raise Exception(message.format(caller_name))
|
||||
|
||||
|
||||
class Group(Mobject):
|
||||
def __init__(self, *mobjects, **kwargs):
|
||||
|
|
|
@ -472,9 +472,6 @@ class VMobject(Mobject):
|
|||
self.points[0], self.points[-1]
|
||||
)
|
||||
|
||||
def has_no_points(self):
|
||||
return len(self.points) == 0
|
||||
|
||||
def add_points_as_corners(self, points):
|
||||
for point in points:
|
||||
self.add_line_to(point)
|
||||
|
@ -791,14 +788,6 @@ class VMobject(Mobject):
|
|||
))
|
||||
return self
|
||||
|
||||
# Errors
|
||||
def throw_error_if_no_points(self):
|
||||
if self.has_no_points():
|
||||
message = "Cannot call VMobject.{}" +\
|
||||
"for a VMobject with no points"
|
||||
caller_name = sys._getframe(1).f_code.co_name
|
||||
raise Exception(message.format(caller_name))
|
||||
|
||||
|
||||
class VGroup(VMobject):
|
||||
def __init__(self, *vmobjects, **kwargs):
|
||||
|
|
Loading…
Add table
Reference in a new issue