Changed several files to conform to PEP8

This commit is contained in:
Grant Sanderson
2018-04-06 13:08:57 -07:00
parent 35b674b480
commit 73433823ce
3 changed files with 131 additions and 110 deletions

View File

@ -15,7 +15,8 @@ from utils.paths import counterclockwise_path
from utils.rate_functions import double_smooth from utils.rate_functions import double_smooth
from utils.rate_functions import smooth from utils.rate_functions import smooth
#Drawing # Drawing
class ShowPartial(Animation): class ShowPartial(Animation):
def update_submobject(self, submobject, starting_submobject, alpha): def update_submobject(self, submobject, starting_submobject, alpha):
@ -26,26 +27,31 @@ class ShowPartial(Animation):
def get_bounds(self, alpha): def get_bounds(self, alpha):
raise Exception("Not Implemented") raise Exception("Not Implemented")
class ShowCreation(ShowPartial): class ShowCreation(ShowPartial):
CONFIG = { CONFIG = {
"submobject_mode" : "one_at_a_time", "submobject_mode": "one_at_a_time",
} }
def get_bounds(self, alpha): def get_bounds(self, alpha):
return (0, alpha) return (0, alpha)
class Uncreate(ShowCreation): class Uncreate(ShowCreation):
CONFIG = { CONFIG = {
"rate_func" : lambda t : smooth(1-t), "rate_func": lambda t: smooth(1 - t),
"remover" : True "remover": True
} }
class Write(ShowCreation): class Write(ShowCreation):
CONFIG = { CONFIG = {
"rate_func" : None, "rate_func": None,
"submobject_mode" : "lagged_start", "submobject_mode": "lagged_start",
} }
def __init__(self, mob_or_text, **kwargs): def __init__(self, mob_or_text, **kwargs):
digest_config(self, kwargs) digest_config(self, kwargs)
if isinstance(mob_or_text, str): if isinstance(mob_or_text, str):
mobject = TextMobject(mob_or_text) mobject = TextMobject(mob_or_text)
else: else:
@ -67,13 +73,15 @@ class Write(ShowCreation):
else: else:
self.run_time = 2 self.run_time = 2
class DrawBorderThenFill(Animation): class DrawBorderThenFill(Animation):
CONFIG = { CONFIG = {
"run_time" : 2, "run_time": 2,
"stroke_width" : 2, "stroke_width": 2,
"stroke_color" : None, "stroke_color": None,
"rate_func" : double_smooth, "rate_func": double_smooth,
} }
def __init__(self, vmobject, **kwargs): def __init__(self, vmobject, **kwargs):
if not isinstance(vmobject, VMobject): if not isinstance(vmobject, VMobject):
raise Exception("DrawBorderThenFill only works for VMobjects") raise Exception("DrawBorderThenFill only works for VMobjects")
@ -82,59 +90,64 @@ class DrawBorderThenFill(Animation):
def update_submobject(self, submobject, starting_submobject, alpha): def update_submobject(self, submobject, starting_submobject, alpha):
submobject.pointwise_become_partial( submobject.pointwise_become_partial(
starting_submobject, 0, min(2*alpha, 1) starting_submobject, 0, min(2 * alpha, 1)
) )
if alpha < 0.5: if alpha < 0.5:
if self.stroke_color: if self.stroke_color:
color = self.stroke_color color = self.stroke_color
elif starting_submobject.stroke_width > 0: elif starting_submobject.stroke_width > 0:
color = starting_submobject.get_stroke_color() color = starting_submobject.get_stroke_color()
else: else:
color = starting_submobject.get_color() color = starting_submobject.get_color()
submobject.set_stroke(color, width = self.stroke_width) submobject.set_stroke(color, width=self.stroke_width)
submobject.set_fill(opacity = 0) submobject.set_fill(opacity=0)
else: else:
if not self.reached_halfway_point_before: if not self.reached_halfway_point_before:
self.reached_halfway_point_before = True self.reached_halfway_point_before = True
submobject.points = np.array(starting_submobject.points) submobject.points = np.array(starting_submobject.points)
width, opacity = [ width, opacity = [
interpolate(start, end, 2*alpha - 1) interpolate(start, end, 2 * alpha - 1)
for start, end in [ for start, end in [
(self.stroke_width, starting_submobject.get_stroke_width()), (self.stroke_width, starting_submobject.get_stroke_width()),
(0, starting_submobject.get_fill_opacity()) (0, starting_submobject.get_fill_opacity())
] ]
] ]
submobject.set_stroke(width = width) submobject.set_stroke(width=width)
submobject.set_fill(opacity = opacity) submobject.set_fill(opacity=opacity)
# Fading
#Fading
class FadeOut(Transform): class FadeOut(Transform):
CONFIG = { CONFIG = {
"remover" : True, "remover": True,
} }
def __init__(self, mobject, **kwargs): def __init__(self, mobject, **kwargs):
target = mobject.copy() target = mobject.copy()
target.fade(1) target.fade(1)
Transform.__init__(self, mobject, target, **kwargs) Transform.__init__(self, mobject, target, **kwargs)
def clean_up(self, surrounding_scene = None): def clean_up(self, surrounding_scene=None):
Transform.clean_up(self, surrounding_scene) Transform.clean_up(self, surrounding_scene)
self.update(0) self.update(0)
class FadeIn(Transform): class FadeIn(Transform):
def __init__(self, mobject, **kwargs): def __init__(self, mobject, **kwargs):
target = mobject.copy() target = mobject.copy()
Transform.__init__(self, mobject, target, **kwargs) Transform.__init__(self, mobject, target, **kwargs)
self.starting_mobject.fade(1) self.starting_mobject.fade(1)
if isinstance(self.starting_mobject, VMobject): if isinstance(self.starting_mobject, VMobject):
self.starting_mobject.set_stroke(width = 0) self.starting_mobject.set_stroke(width=0)
self.starting_mobject.set_fill(opacity = 0) self.starting_mobject.set_fill(opacity=0)
class FadeInAndShiftFromDirection(Transform): class FadeInAndShiftFromDirection(Transform):
CONFIG = { CONFIG = {
"direction" : DOWN, "direction": DOWN,
} }
def __init__(self, mobject, **kwargs): def __init__(self, mobject, **kwargs):
digest_config(self, kwargs) digest_config(self, kwargs)
target = mobject.copy() target = mobject.copy()
@ -142,19 +155,23 @@ class FadeInAndShiftFromDirection(Transform):
mobject.fade(1) mobject.fade(1)
Transform.__init__(self, mobject, target, **kwargs) Transform.__init__(self, mobject, target, **kwargs)
class FadeInFromDown(FadeInAndShiftFromDirection): class FadeInFromDown(FadeInAndShiftFromDirection):
""" """
Essential a more convenient form of FadeInAndShiftFromDirection Essential a more convenient form of FadeInAndShiftFromDirection
""" """
CONFIG = { CONFIG = {
"direction" : DOWN, "direction": DOWN,
} }
#Growing # Growing
class GrowFromPoint(Transform): class GrowFromPoint(Transform):
CONFIG = { CONFIG = {
"point_color" : None, "point_color": None,
} }
def __init__(self, mobject, point, **kwargs): def __init__(self, mobject, point, **kwargs):
digest_config(self, kwargs) digest_config(self, kwargs)
target = mobject.copy() target = mobject.copy()
@ -165,19 +182,23 @@ class GrowFromPoint(Transform):
mobject.set_color(point_mob.get_color()) mobject.set_color(point_mob.get_color())
Transform.__init__(self, mobject, target, **kwargs) Transform.__init__(self, mobject, target, **kwargs)
class GrowFromCenter(GrowFromPoint): class GrowFromCenter(GrowFromPoint):
def __init__(self, mobject, **kwargs): def __init__(self, mobject, **kwargs):
GrowFromPoint.__init__(self, mobject, mobject.get_center(), **kwargs) GrowFromPoint.__init__(self, mobject, mobject.get_center(), **kwargs)
class GrowArrow(GrowFromPoint): class GrowArrow(GrowFromPoint):
def __init__(self, arrow, **kwargs): def __init__(self, arrow, **kwargs):
GrowFromPoint.__init__(self, arrow, arrow.get_start(), **kwargs) GrowFromPoint.__init__(self, arrow, arrow.get_start(), **kwargs)
class SpinInFromNothing(GrowFromCenter): class SpinInFromNothing(GrowFromCenter):
CONFIG = { CONFIG = {
"path_func" : counterclockwise_path() "path_func": counterclockwise_path()
} }
class ShrinkToCenter(Transform): class ShrinkToCenter(Transform):
def __init__(self, mobject, **kwargs): def __init__(self, mobject, **kwargs):
Transform.__init__( Transform.__init__(

View File

@ -25,17 +25,19 @@ from scene.scene import Scene
from utils.rate_functions import squish_rate_func from utils.rate_functions import squish_rate_func
from utils.rate_functions import there_and_back from utils.rate_functions import there_and_back
class PiCreatureScene(Scene): class PiCreatureScene(Scene):
CONFIG = { CONFIG = {
"total_wait_time" : 0, "total_wait_time": 0,
"seconds_to_blink" : 3, "seconds_to_blink": 3,
"pi_creatures_start_on_screen" : True, "pi_creatures_start_on_screen": True,
"default_pi_creature_kwargs" : { "default_pi_creature_kwargs": {
"color" : GREY_BROWN, "color": GREY_BROWN,
"flip_at_start" : True, "flip_at_start": True,
}, },
"default_pi_creature_start_corner" : DOWN+LEFT, "default_pi_creature_start_corner": DOWN + LEFT,
} }
def setup(self): def setup(self):
self.pi_creatures = self.create_pi_creatures() self.pi_creatures = self.create_pi_creatures()
self.pi_creature = self.get_primary_pi_creature() self.pi_creature = self.get_primary_pi_creature()
@ -43,7 +45,7 @@ class PiCreatureScene(Scene):
self.add(*self.pi_creatures) self.add(*self.pi_creatures)
def create_pi_creatures(self): def create_pi_creatures(self):
""" """
Likely updated for subclasses Likely updated for subclasses
""" """
return VGroup(self.create_pi_creature()) return VGroup(self.create_pi_creature())
@ -66,7 +68,7 @@ class PiCreatureScene(Scene):
def get_on_screen_pi_creatures(self): def get_on_screen_pi_creatures(self):
mobjects = self.get_mobjects() mobjects = self.get_mobjects()
return VGroup(*filter( return VGroup(*filter(
lambda pi : pi in mobjects, lambda pi: pi in mobjects,
self.get_pi_creatures() self.get_pi_creatures()
)) ))
@ -80,7 +82,7 @@ class PiCreatureScene(Scene):
bubble_class = kwargs.pop("bubble_class", SpeechBubble) bubble_class = kwargs.pop("bubble_class", SpeechBubble)
target_mode = kwargs.pop( target_mode = kwargs.pop(
"target_mode", "target_mode",
"thinking" if bubble_class is ThoughtBubble else "speaking" "thinking" if bubble_class is ThoughtBubble else "speaking"
) )
bubble_kwargs = kwargs.pop("bubble_kwargs", {}) bubble_kwargs = kwargs.pop("bubble_kwargs", {})
@ -91,10 +93,11 @@ class PiCreatureScene(Scene):
on_screen_mobjects = self.camera.extract_mobject_family_members( on_screen_mobjects = self.camera.extract_mobject_family_members(
self.get_mobjects() self.get_mobjects()
) )
def has_bubble(pi): def has_bubble(pi):
return hasattr(pi, "bubble") and \ return hasattr(pi, "bubble") and \
pi.bubble is not None and \ pi.bubble is not None and \
pi.bubble in on_screen_mobjects pi.bubble in on_screen_mobjects
pi_creatures_with_bubbles = filter(has_bubble, self.get_pi_creatures()) pi_creatures_with_bubbles = filter(has_bubble, self.get_pi_creatures())
if pi_creature in pi_creatures_with_bubbles: if pi_creature in pi_creatures_with_bubbles:
@ -102,7 +105,7 @@ class PiCreatureScene(Scene):
old_bubble = pi_creature.bubble old_bubble = pi_creature.bubble
bubble = pi_creature.get_bubble( bubble = pi_creature.get_bubble(
*content, *content,
bubble_class = bubble_class, bubble_class=bubble_class,
**bubble_kwargs **bubble_kwargs
) )
anims += [ anims += [
@ -114,9 +117,9 @@ class PiCreatureScene(Scene):
anims.append(PiCreatureBubbleIntroduction( anims.append(PiCreatureBubbleIntroduction(
pi_creature, pi_creature,
*content, *content,
bubble_class = bubble_class, bubble_class=bubble_class,
bubble_kwargs = bubble_kwargs, bubble_kwargs=bubble_kwargs,
target_mode = target_mode, target_mode=target_mode,
**kwargs **kwargs
)) ))
anims += [ anims += [
@ -130,26 +133,28 @@ class PiCreatureScene(Scene):
def pi_creature_says(self, *args, **kwargs): def pi_creature_says(self, *args, **kwargs):
self.introduce_bubble( self.introduce_bubble(
*args, *args,
bubble_class = SpeechBubble, bubble_class=SpeechBubble,
**kwargs **kwargs
) )
def pi_creature_thinks(self, *args, **kwargs): def pi_creature_thinks(self, *args, **kwargs):
self.introduce_bubble( self.introduce_bubble(
*args, *args,
bubble_class = ThoughtBubble, bubble_class=ThoughtBubble,
**kwargs **kwargs
) )
def say(self, *content, **kwargs): def say(self, *content, **kwargs):
self.pi_creature_says(self.get_primary_pi_creature(), *content, **kwargs) self.pi_creature_says(
self.get_primary_pi_creature(), *content, **kwargs)
def think(self, *content, **kwargs): def think(self, *content, **kwargs):
self.pi_creature_thinks(self.get_primary_pi_creature(), *content, **kwargs) self.pi_creature_thinks(
self.get_primary_pi_creature(), *content, **kwargs)
def compile_play_args_to_animation_list(self, *args): def compile_play_args_to_animation_list(self, *args):
""" """
Add animations so that all pi creatures look at the Add animations so that all pi creatures look at the
first mobject being animated with each .play call first mobject being animated with each .play call
""" """
animations = Scene.compile_play_args_to_animation_list(self, *args) animations = Scene.compile_play_args_to_animation_list(self, *args)
@ -157,13 +162,13 @@ class PiCreatureScene(Scene):
return animations return animations
non_pi_creature_anims = filter( non_pi_creature_anims = filter(
lambda anim : anim.mobject not in self.get_pi_creatures(), lambda anim: anim.mobject not in self.get_pi_creatures(),
animations animations
) )
if len(non_pi_creature_anims) == 0: if len(non_pi_creature_anims) == 0:
return animations return animations
first_anim = non_pi_creature_anims[0] first_anim = non_pi_creature_anims[0]
#Look at ending state # Look at ending state
first_anim.update(1) first_anim.update(1)
point_of_interest = first_anim.mobject.get_center() point_of_interest = first_anim.mobject.get_center()
first_anim.update(0) first_anim.update(0)
@ -174,7 +179,7 @@ class PiCreatureScene(Scene):
if pi_creature in first_anim.mobject.submobject_family(): if pi_creature in first_anim.mobject.submobject_family():
continue continue
anims_with_pi_creature = filter( anims_with_pi_creature = filter(
lambda anim : pi_creature in anim.mobject.submobject_family(), lambda anim: pi_creature in anim.mobject.submobject_family(),
animations animations
) )
for anim in anims_with_pi_creature: for anim in anims_with_pi_creature:
@ -193,7 +198,7 @@ class PiCreatureScene(Scene):
def blink(self): def blink(self):
self.play(Blink(random.choice(self.get_on_screen_pi_creatures()))) self.play(Blink(random.choice(self.get_on_screen_pi_creatures())))
def joint_blink(self, pi_creatures = None, shuffle = True, **kwargs): def joint_blink(self, pi_creatures=None, shuffle=True, **kwargs):
if pi_creatures is None: if pi_creatures is None:
pi_creatures = self.get_on_screen_pi_creatures() pi_creatures = self.get_on_screen_pi_creatures()
creatures_list = list(pi_creatures) creatures_list = list(pi_creatures)
@ -202,25 +207,25 @@ class PiCreatureScene(Scene):
def get_rate_func(pi): def get_rate_func(pi):
index = creatures_list.index(pi) index = creatures_list.index(pi)
proportion = float(index)/len(creatures_list) proportion = float(index) / len(creatures_list)
start_time = 0.8*proportion start_time = 0.8 * proportion
return squish_rate_func( return squish_rate_func(
there_and_back, there_and_back,
start_time, start_time + 0.2 start_time, start_time + 0.2
) )
self.play(*[ self.play(*[
Blink(pi, rate_func = get_rate_func(pi), **kwargs) Blink(pi, rate_func=get_rate_func(pi), **kwargs)
for pi in creatures_list for pi in creatures_list
]) ])
return self return self
def wait(self, time = 1, blink = True): def wait(self, time=1, blink=True):
while time >= 1: while time >= 1:
time_to_blink = self.total_wait_time%self.seconds_to_blink == 0 time_to_blink = self.total_wait_time % self.seconds_to_blink == 0
if blink and self.any_pi_creatures_on_screen() and time_to_blink: if blink and self.any_pi_creatures_on_screen() and time_to_blink:
self.blink() self.blink()
self.num_plays -= 1 #This shouldn't count as an animation self.num_plays -= 1 # This shouldn't count as an animation
else: else:
self.non_blink_wait() self.non_blink_wait()
time -= 1 time -= 1
@ -229,14 +234,14 @@ class PiCreatureScene(Scene):
self.non_blink_wait(time) self.non_blink_wait(time)
return self return self
def non_blink_wait(self, time = 1): def non_blink_wait(self, time=1):
Scene.wait(self, time) Scene.wait(self, time)
return self return self
def change_mode(self, mode): def change_mode(self, mode):
self.play(self.get_primary_pi_creature().change_mode, mode) self.play(self.get_primary_pi_creature().change_mode, mode)
def look_at(self, thing_to_look_at, pi_creatures = None): def look_at(self, thing_to_look_at, pi_creatures=None):
if pi_creatures is None: if pi_creatures is None:
pi_creatures = self.get_pi_creatures() pi_creatures = self.get_pi_creatures()
self.play(*it.chain(*[ self.play(*it.chain(*[
@ -244,30 +249,33 @@ class PiCreatureScene(Scene):
for pi in pi_creatures for pi in pi_creatures
])) ]))
class TeacherStudentsScene(PiCreatureScene): class TeacherStudentsScene(PiCreatureScene):
CONFIG = { CONFIG = {
"student_colors" : [BLUE_D, BLUE_E, BLUE_C], "student_colors": [BLUE_D, BLUE_E, BLUE_C],
"student_scale_factor" : 0.8, "student_scale_factor": 0.8,
"seconds_to_blink" : 2, "seconds_to_blink": 2,
"screen_height" : 3, "screen_height": 3,
} }
def setup(self): def setup(self):
PiCreatureScene.setup(self) PiCreatureScene.setup(self)
self.screen = ScreenRectangle(height = self.screen_height) self.screen = ScreenRectangle(height=self.screen_height)
self.screen.to_corner(UP+LEFT) self.screen.to_corner(UP + LEFT)
self.hold_up_spot = self.teacher.get_corner(UP+LEFT) + MED_LARGE_BUFF*UP self.hold_up_spot = self.teacher.get_corner(
UP + LEFT) + MED_LARGE_BUFF * UP
def create_pi_creatures(self): def create_pi_creatures(self):
self.teacher = Mortimer() self.teacher = Mortimer()
self.teacher.to_corner(DOWN + RIGHT) self.teacher.to_corner(DOWN + RIGHT)
self.teacher.look(DOWN+LEFT) self.teacher.look(DOWN + LEFT)
self.students = VGroup(*[ self.students = VGroup(*[
Randolph(color = c) Randolph(color=c)
for c in self.student_colors for c in self.student_colors
]) ])
self.students.arrange_submobjects(RIGHT) self.students.arrange_submobjects(RIGHT)
self.students.scale(self.student_scale_factor) self.students.scale(self.student_scale_factor)
self.students.to_corner(DOWN+LEFT) self.students.to_corner(DOWN + LEFT)
self.teacher.look_at(self.students[-1].eyes) self.teacher.look_at(self.students[-1].eyes)
for student in self.students: for student in self.students:
student.look_at(self.teacher.eyes) student.look_at(self.teacher.eyes)
@ -288,8 +296,8 @@ class TeacherStudentsScene(PiCreatureScene):
def student_says(self, *content, **kwargs): def student_says(self, *content, **kwargs):
if "target_mode" not in kwargs: if "target_mode" not in kwargs:
target_mode = random.choice([ target_mode = random.choice([
"raise_right_hand", "raise_right_hand",
"raise_left_hand", "raise_left_hand",
]) ])
kwargs["target_mode"] = target_mode kwargs["target_mode"] = target_mode
student = self.get_students()[kwargs.get("student_index", 1)] student = self.get_students()[kwargs.get("student_index", 1)]
@ -307,7 +315,7 @@ class TeacherStudentsScene(PiCreatureScene):
return self.pi_creature_thinks(student, *content, **kwargs) return self.pi_creature_thinks(student, *content, **kwargs)
def change_all_student_modes(self, mode, **kwargs): def change_all_student_modes(self, mode, **kwargs):
self.change_student_modes(*[mode]*len(self.students), **kwargs) self.change_student_modes(*[mode] * len(self.students), **kwargs)
def change_student_modes(self, *modes, **kwargs): def change_student_modes(self, *modes, **kwargs):
added_anims = kwargs.pop("added_anims", []) added_anims = kwargs.pop("added_anims", [])
@ -326,12 +334,12 @@ class TeacherStudentsScene(PiCreatureScene):
pi.look_at(kwargs["look_at_arg"]) pi.look_at(kwargs["look_at_arg"])
submobject_mode = kwargs.get("submobject_mode", "lagged_start") submobject_mode = kwargs.get("submobject_mode", "lagged_start")
return Transform( return Transform(
start, target, start, target,
submobject_mode = submobject_mode, submobject_mode=submobject_mode,
run_time = 2 run_time=2
) )
def zoom_in_on_thought_bubble(self, bubble = None, radius = FRAME_Y_RADIUS+FRAME_X_RADIUS): def zoom_in_on_thought_bubble(self, bubble=None, radius=FRAME_Y_RADIUS + FRAME_X_RADIUS):
if bubble is None: if bubble is None:
for pi in self.get_pi_creatures(): for pi in self.get_pi_creatures():
if hasattr(pi, "bubble") and isinstance(pi.bubble, ThoughtBubble): if hasattr(pi, "bubble") and isinstance(pi.bubble, ThoughtBubble):
@ -340,15 +348,16 @@ class TeacherStudentsScene(PiCreatureScene):
if bubble is None: if bubble is None:
raise Exception("No pi creatures have a thought bubble") raise Exception("No pi creatures have a thought bubble")
vect = -bubble.get_bubble_center() vect = -bubble.get_bubble_center()
def func(point): def func(point):
centered = point+vect centered = point + vect
return radius*centered/np.linalg.norm(centered) return radius * centered / np.linalg.norm(centered)
self.play(*[ self.play(*[
ApplyPointwiseFunction(func, mob) ApplyPointwiseFunction(func, mob)
for mob in self.get_mobjects() for mob in self.get_mobjects()
]) ])
def teacher_holds_up(self, mobject, target_mode = "raise_right_hand", **kwargs): def teacher_holds_up(self, mobject, target_mode="raise_right_hand", **kwargs):
mobject.move_to(self.hold_up_spot, DOWN) mobject.move_to(self.hold_up_spot, DOWN)
mobject.shift_onto_screen() mobject.shift_onto_screen()
mobject_copy = mobject.copy() mobject_copy = mobject.copy()
@ -358,5 +367,3 @@ class TeacherStudentsScene(PiCreatureScene):
ReplacementTransform(mobject_copy, mobject), ReplacementTransform(mobject_copy, mobject),
self.teacher.change, target_mode, self.teacher.change, target_mode,
) )

View File

@ -12,12 +12,13 @@ from constants import *
VECTOR_LABEL_SCALE_FACTOR = 0.8 VECTOR_LABEL_SCALE_FACTOR = 0.8
def matrix_to_tex_string(matrix): def matrix_to_tex_string(matrix):
matrix = np.array(matrix).astype("string") matrix = np.array(matrix).astype("string")
if matrix.ndim == 1: if matrix.ndim == 1:
matrix = matrix.reshape((matrix.size, 1)) matrix = matrix.reshape((matrix.size, 1))
n_rows, n_cols = matrix.shape n_rows, n_cols = matrix.shape
prefix = "\\left[ \\begin{array}{%s}"%("c"*n_cols) prefix = "\\left[ \\begin{array}{%s}" % ("c" * n_cols)
suffix = "\\end{array} \\right]" suffix = "\\end{array} \\right]"
rows = [ rows = [
" & ".join(row) " & ".join(row)
@ -25,39 +26,43 @@ def matrix_to_tex_string(matrix):
] ]
return prefix + " \\\\ ".join(rows) + suffix return prefix + " \\\\ ".join(rows) + suffix
def matrix_to_mobject(matrix): def matrix_to_mobject(matrix):
return TexMobject(matrix_to_tex_string(matrix)) return TexMobject(matrix_to_tex_string(matrix))
def vector_coordinate_label(vector_mob, integer_labels = True,
n_dim = 2, color = WHITE): def vector_coordinate_label(vector_mob, integer_labels=True,
n_dim=2, color=WHITE):
vect = np.array(vector_mob.get_end()) vect = np.array(vector_mob.get_end())
if integer_labels: if integer_labels:
vect = np.round(vect).astype(int) vect = np.round(vect).astype(int)
vect = vect[:n_dim] vect = vect[:n_dim]
vect = vect.reshape((n_dim, 1)) vect = vect.reshape((n_dim, 1))
label = Matrix(vect, add_background_rectangles = True) label = Matrix(vect, add_background_rectangles=True)
label.scale(VECTOR_LABEL_SCALE_FACTOR) label.scale(VECTOR_LABEL_SCALE_FACTOR)
shift_dir = np.array(vector_mob.get_end()) shift_dir = np.array(vector_mob.get_end())
if shift_dir[0] >= 0: #Pointing right if shift_dir[0] >= 0: # Pointing right
shift_dir -= label.get_left() + DEFAULT_MOBJECT_TO_MOBJECT_BUFFER*LEFT shift_dir -= label.get_left() + DEFAULT_MOBJECT_TO_MOBJECT_BUFFER * LEFT
else: #Pointing left else: # Pointing left
shift_dir -= label.get_right() + DEFAULT_MOBJECT_TO_MOBJECT_BUFFER*RIGHT shift_dir -= label.get_right() + DEFAULT_MOBJECT_TO_MOBJECT_BUFFER * RIGHT
label.shift(shift_dir) label.shift(shift_dir)
label.set_color(color) label.set_color(color)
label.rect = BackgroundRectangle(label) label.rect = BackgroundRectangle(label)
label.add_to_back(label.rect) label.add_to_back(label.rect)
return label return label
class Matrix(VMobject): class Matrix(VMobject):
CONFIG = { CONFIG = {
"v_buff" : 0.5, "v_buff": 0.5,
"h_buff" : 1, "h_buff": 1,
"add_background_rectangles" : False "add_background_rectangles": False
} }
def __init__(self, matrix, **kwargs): def __init__(self, matrix, **kwargs):
""" """
Matrix can either either include numbres, tex_strings, Matrix can either either include numbres, tex_strings,
or mobjects or mobjects
""" """
VMobject.__init__(self, **kwargs) VMobject.__init__(self, **kwargs)
@ -89,9 +94,9 @@ class Matrix(VMobject):
if i == 0 and j == 0: if i == 0 and j == 0:
continue continue
elif i == 0: elif i == 0:
mob.next_to(matrix[i][j-1], RIGHT, self.h_buff) mob.next_to(matrix[i][j - 1], RIGHT, self.h_buff)
else: else:
mob.next_to(matrix[i-1][j], DOWN, self.v_buff) mob.next_to(matrix[i - 1][j], DOWN, self.v_buff)
return self return self
def add_brackets(self): def add_brackets(self):
@ -107,7 +112,7 @@ class Matrix(VMobject):
def set_color_columns(self, *colors): def set_color_columns(self, *colors):
for i, color in enumerate(colors): for i, color in enumerate(colors):
VGroup(*self.mob_matrix[:,i]).set_color(color) VGroup(*self.mob_matrix[:, i]).set_color(color)
return self return self
def add_background_to_entries(self): def add_background_to_entries(self):
@ -123,15 +128,3 @@ class Matrix(VMobject):
def get_brackets(self): def get_brackets(self):
return self.brackets return self.brackets