A community-maintained Python framework for creating mathematical animations.
Beginning EoLA chapter 3
Grant Sandersonb16695b6f385
diff --git a/eola/chapter2.py b/eola/chapter2.pyindex 2663411b7..c1affdc8c 100644--- a/eola/chapter2.py+++ b/eola/chapter2.py@@ -24,8 +24,8 @@ from eola.two_d_space import *class OpeningQuote(Scene):def construct(self):words = TextMobject("""- Mathematics requires a small dose, not of genius, \\\\- but of an imaginative freedom which, in a larger \\\\+ Mathematics requires a small dose, not of genius,+ but of an imaginative freedom which, in a largerdose, would be insanity.""")words.to_edge(UP)@@ -553,8 +553,8 @@ class LinearDependentVectors(ShowVaryingLinearCombinations):class WhenVectorsLineUp(LinearDependentVectors):CONFIG = {- "vector1" : [2, 1],- "vector2" : [1, 0.5],+ "vector1" : [3, 2],+ "vector2" : [1.5, 1],"scalar_pairs" : [(1.5, 0.6),(0.7, 1.3),diff --git a/eola/chapter3.py b/eola/chapter3.pynew file mode 100644index 000000000..3d5e1a87b--- /dev/null+++ b/eola/chapter3.py@@ -0,0 +1,116 @@+from mobject.tex_mobject import TexMobject+from mobject import Mobject+from mobject.image_mobject import ImageMobject+from mobject.vectorized_mobject import VMobject++from animation.animation import Animation+from animation.transform import *+from animation.simple_animations import *+from topics.geometry import *+from topics.characters import *+from topics.functions import *+from topics.number_line import *+from topics.numerals import *+from scene import Scene+from camera import Camera+from mobject.svg_mobject import *+from mobject.tex_mobject import *+from mobject.vectorized_mobject import *++from eola.matrix import *+from eola.two_d_space import *++class OpeningQuote(Scene):+ def construct(self):+ words = TextMobject([+ "Unfortunately, no one can be told what the",+ "Matrix",+ "is. You have to",+ "see it for yourself.",+ ])+ words.scale_to_fit_width(2*SPACE_WIDTH - 2)+ words.to_edge(UP)+ words.split()[1].highlight(GREEN)+ words.split()[3].highlight(GREEN)+ author = TextMobject("-Morpheus")+ author.highlight(YELLOW)+ author.next_to(words, DOWN, buff = 0.5)+ comment = TextMobject("""+ (Surprisingly apt words on the importance+ of understanding matrix operations visually.)+ """)+ comment.scale(0.7)+ comment.next_to(author, DOWN, buff = 1)++ self.play(FadeIn(words))+ self.dither(3)+ self.play(Write(author, run_time = 3))+ self.dither()+ self.play(Write(comment))+ self.dither()+++class Introduction(TeacherStudentsScene):+ def construct(self):+ title = TextMobject("Matrices as linear transformations")+ title.to_edge(UP)+ title.highlight(YELLOW)+ self.add(title)+ self.setup()+ self.teacher_says("""+ Listen up folks, this one is+ particularly important+ """, height = 3)+ self.random_blink(2)+ self.teacher_says("We'll start by just watching", height = 3)+ self.random_blink()+ self.teacher_thinks(VMobject())+ everything = VMobject(*self.get_mobjects())+ def spread_out(p):+ p = p + 3*DOWN+ return (SPACE_WIDTH+SPACE_HEIGHT)*p/np.linalg.norm(p)+ self.play(ApplyPointwiseFunction(spread_out, everything))+++class ShowGridCreation(Scene):+ def construct(self):+ plane = NumberPlane()+ coords = plane.get_coordinate_labels()+ self.play(ShowCreation(plane))+ self.play(Write(coords, run_time = 5))+ self.dither()++class SimpleLinearTransformationScene(LinearTransformationScene):+ CONFIG = {+ "show_basis_vectors" : False+ }+ def construct(self):+ self.setup()++++++++++++++++++++++++++++diff --git a/eola/two_d_space.py b/eola/two_d_space.pyindex 7ac827a70..b1c9b31d6 100644--- a/eola/two_d_space.py+++ b/eola/two_d_space.py@@ -106,6 +106,9 @@ class LinearTransformationScene(Scene):))+ def apply_nonlinear_transformation(self, function, **kwargs):+ pass #TODO+diff --git a/mobject/tex_mobject.py b/mobject/tex_mobject.pyindex dd94cb521..d96f1db5a 100644--- a/mobject/tex_mobject.py+++ b/mobject/tex_mobject.py@@ -1,6 +1,7 @@from vectorized_mobject import VMobjectfrom svg_mobject import SVGMobject, VMobjectFromSVGPathstringfrom helpers import *+import collectionsTEX_MOB_SCALE_VAL = 0.05TEXT_MOB_SCALE_VAL = 0.05@@ -32,17 +33,32 @@ class TexMobject(SVGMobject):"fill_color" : WHITE,"should_center" : True,"separate_list_arg_with_spaces" : True,+ "enforce_new_line_structure" : False,"initial_scale_val" : TEX_MOB_SCALE_VAL,"organize_left_to_right" : False,"propogate_style_to_family" : True,}def __init__(self, expression, **kwargs):digest_config(self, kwargs, locals())+ self.is_input_a_list = isinstance(expression, list)VMobject.__init__(self, **kwargs)self.move_into_position()if self.organize_left_to_right:self.organize_submobjects_left_to_right()+ def handle_input_type(self):+ if isinstance(self.expression, str):+ self.is_input_a_list = False+ elif isinstance(self.expression, collections.Iterable):+ self.is_input_a_list = True+ self.expression = list(self.expression)+ else:+ raise Exception(+ "TexMobject was expecting string or list, got " + \+ str(type(self.expression)) + \+ " instead."+ )+def path_string_to_mobject(self, path_string):#Overwrite superclass default to use#specialized path_string mobject@@ -50,16 +66,22 @@ class TexMobject(SVGMobject):def generate_points(self):- is_list = isinstance(self.expression, list)- separator = ""- if is_list and self.separate_list_arg_with_spaces:- separator = " "- expression = separator.join(self.expression)- self.svg_file = tex_to_svg_file(expression, self.template_tex_file)+ self.svg_file = tex_to_svg_file(+ self.get_modified_expression(),+ self.template_tex_file+ )SVGMobject.generate_points(self)- if is_list:+ if self.is_input_a_list:self.handle_list_expression(self.expression)+ def get_modified_expression(self):+ separator = ""+ if self.is_input_a_list and self.separate_list_arg_with_spaces:+ separator = " "+ result = separator.join(self.expression)+ if self.enforce_new_line_structure:+ result = result.replace("\n", " \\\\ \n ")+ return resultdef handle_list_expression(self, list_expression):new_submobjects = []@@ -84,10 +106,12 @@ class TexMobject(SVGMobject):class TextMobject(TexMobject):CONFIG = {"template_tex_file" : TEMPLATE_TEXT_FILE,- "initial_scale_val" : TEXT_MOB_SCALE_VAL+ "initial_scale_val" : TEXT_MOB_SCALE_VAL,+ "enforce_new_line_structure" : True,}+class Brace(TexMobject):CONFIG = {"buff" : 0.2,diff --git a/topics/characters.py b/topics/characters.pyindex a82f00a5e..967ed6238 100644--- a/topics/characters.py+++ b/topics/characters.py@@ -5,7 +5,7 @@ from mobject.svg_mobject import SVGMobjectfrom mobject.vectorized_mobject import VMobjectfrom mobject.tex_mobject import TextMobject-from animation.transform import ApplyMethod, FadeOut, FadeIn+from animation.transform import Transform, ApplyMethod, FadeOut, FadeInfrom animation.simple_animations import Writefrom scene import Scene@@ -289,51 +289,52 @@ class TeacherStudentsScene(Scene):def get_everyone(self):return [self.get_teacher()] + self.get_students()+ def get_bubble_intro_animation(self, content, bubble_type,+ pi_creature,+ **bubble_kwargs):+ bubble = pi_creature.get_bubble(bubble_type, **bubble_kwargs)+ bubble.add_content(content)+ if pi_creature.bubble:+ content_intro_anims = [+ Transform(pi_creature.bubble, bubble)+ ]+ else:+ content_intro_anims = [+ FadeIn(bubble.split()[0]),+ Write(content),+ ]+ pi_creature.bubble = bubble+ return content_intro_anims+def introduce_bubble(self, content, bubble_type, pi_creature,- content_intro_anim = None,pi_creature_target_mode = None,- added_anims = []):- bubble = pi_creature.get_bubble(bubble_type)+ added_anims = [],+ **bubble_kwargs):if isinstance(content, str):- content = TextMobject(content)- bubble.position_mobject_inside(content)- pi_creature.bubble = bubble-- content_intro_anim = content_intro_anim or Write(content)- run_time = content_intro_anim.run_time- one_sec_rate_func = squish_rate_func(- smooth, 0, 1./run_time+ content = TextMobject(content)+ content_intro_anims = self.get_bubble_intro_animation(+ content, bubble_type, pi_creature, **bubble_kwargs)+if not pi_creature_target_mode:if bubble_type is "speech":pi_creature_target_mode = "speaking"else:pi_creature_target_mode = "pondering"- faders = []- to_plains = []for p in self.get_everyone():if p.bubble and p is not pi_creature:- faders.append(FadeOut(- p.bubble, rate_func = one_sec_rate_func- ))+ added_anims.append(FadeOut(p.bubble))p.bubble = None- to_plains.append(ApplyMethod(- p.change_mode, "plain",- rate_func = one_sec_rate_func- ))-- anims = faders + to_plains + added_anims + [- content_intro_anim,- FadeIn(bubble, rate_func = one_sec_rate_func),+ added_anims.append(ApplyMethod(p.change_mode, "plain"))++ anims = added_anims + content_intro_anims + [ApplyMethod(pi_creature.change_mode,pi_creature_target_mode,- rate_func = one_sec_rate_func),]- self.play(*anims, run_time = run_time)- bubble.add_content(content)+ self.play(*anims)def teacher_says(self, content, **kwargs):self.introduce_bubble(@@ -344,7 +345,7 @@ class TeacherStudentsScene(Scene):student = self.get_students()[student_index]self.introduce_bubble(content, "speech", student, **kwargs)- def teacher_thinks(self, content):+ def teacher_thinks(self, content, **kwargs):self.introduce_bubble(content, "thought", self.get_teacher(), **kwargs)@@ -353,9 +354,11 @@ class TeacherStudentsScene(Scene):student = self.get_students()[student_index]self.introduce_bubble(content, "thought", student, **kwargs)- def random_blink(self):- pi_creature = random.choice(self.get_everyone())- self.play(Blink(pi_creature))+ def random_blink(self, num_times = 1):+ for x in range(num_times):+ pi_creature = random.choice(self.get_everyone())+ self.play(Blink(pi_creature))+ self.dither()