Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Initial edits to make main movement methods take in about_point and about_edge kwargs

Grant Sanderson850b6f9e888e
diff --git a/mobject/mobject.py b/mobject/mobject.py
index 8f7e0ae18..5ad8afcf4 100644
--- a/mobject/mobject.py
+++ b/mobject/mobject.py
@@ -138,7 +138,7 @@ class Mobject(object):
mob.points += total_vector
return self
- def scale(self, scale_factor, about_point = None, about_edge = ORIGIN):
+ def scale(self, scale_factor, **kwargs):
"""
Default behavior is to scale about the center of the mobject.
The argument about_edge can be a vector, indicating which side of
@@ -148,46 +148,41 @@ class Mobject(object):
Otherwise, if about_point is given a value, scaling is done with
respect to that point.
"""
- if about_point is None:
- about_point = self.get_critical_point(about_edge)
- self.shift(-about_point)
- for mob in self.family_members_with_points():
- mob.points *= scale_factor
- self.shift(about_point)
+ self.apply_points_function_about_point(
+ lambda points : scale_factor*points, **kwargs
+ )
return self
def rotate_about_origin(self, angle, axis = OUT, axes = []):
- if len(axes) == 0:
- axes = [axis]
- rot_matrix = np.identity(self.dim)
- for axis in axes:
- rot_matrix = np.dot(rot_matrix, rotation_matrix(angle, axis))
- t_rot_matrix = np.transpose(rot_matrix)
- for mob in self.family_members_with_points():
- mob.points = np.dot(mob.points, t_rot_matrix)
- return self
+ return self.rotate(angle, axis, about_point = ORIGIN)
- def rotate(self, angle, axis = OUT, axes = [], about_point = None):
- if about_point is None:
- self.rotate_about_origin(angle, axis, axes)
- else:
- self.do_about_point(about_point, self.rotate, angle, axis, axes)
+ def rotate(self, angle, axis = OUT, **kwargs):
+ rot_matrix = rotation_matrix(angle, axis)
+ self.apply_points_function_about_point(
+ lambda points : np.dot(mob.points, rot_matrix.T),
+ **kwargs
+ )
return self
- def stretch(self, factor, dim):
- for mob in self.family_members_with_points():
- mob.points[:,dim] *= factor
+ def stretch(self, factor, dim, **kwargs):
+ def func(points):
+ points[:,dim] *= factor
+ return points
+ self.apply_points_function_about_point(func, about_point)
return self
- def apply_function(self, function):
- for mob in self.family_members_with_points():
- mob.points = np.apply_along_axis(function, 1, mob.points)
+ def apply_function(self, function, about_point = ORIGIN, **kwargs):
+ self.apply_points_function_about_point(
+ lambda points : np.apply_along_axis(function, 1, points),
+ about_point = about_point, **kwargs
+ )
return self
- def apply_matrix(self, matrix):
- matrix = np.array(matrix)
- for mob in self.family_members_with_points():
- mob.points = np.dot(mob.points, matrix.T)
+ def apply_matrix(self, matrix, about_point = ORIGIN, **kwargs):
+ self.apply_points_function_about_point(
+ lambda points : np.dot(points, matrix.T),
+ about_point = about_point, **kwargs
+ )
return self
def wag(self, direction = RIGHT, axis = DOWN, wag_factor = 1.0):
@@ -224,6 +219,15 @@ class Mobject(object):
#### In place operations ######
+ def apply_points_function_about_point(self, func, about_point = None, about_edge = ORIGIN):
+ if about_point is None:
+ about_point = self.get_critical_point(about_edge)
+ for mob in self.family_members_with_points():
+ mob.points -= about_point
+ mob.points = func(mob.points)
+ mob.points += about_point
+ return self
+
def do_about_point(self, point, method, *args, **kwargs):
self.shift(-point)
method(*args, **kwargs)
@@ -235,8 +239,8 @@ class Mobject(object):
return self
def rotate_in_place(self, angle, axis = OUT, axes = []):
- self.do_in_place(self.rotate, angle, axis, axes)
- return self
+ # redundant with default behavior of rotate now.
+ return self.rotate(angle, axis = axis, axes = axes)
def flip(self, axis = UP):
self.rotate_in_place(np.pi, axis)
@@ -244,12 +248,11 @@ class Mobject(object):
def scale_in_place(self, scale_factor):
#Redundant with default behavior of scale now.
- self.do_in_place(self.scale, scale_factor)
- return self
+ return self.scale(scale_factor)
def scale_about_point(self, scale_factor, point):
- self.do_about_point(point, self.scale, scale_factor)
- return self
+ #Redundant with default behavior of scale now.
+ return self.scale(scale_factor, about_point = point)
def pose_at_angle(self):
self.rotate_in_place(np.pi / 7, RIGHT+UP)