├── geometric_series.py ├── lagrangian.py ├── log_int.py ├── pythagorean_circles.py ├── pythagorean_spiral.py └── wallis.py /geometric_series.py: -------------------------------------------------------------------------------- 1 | from manimlib.imports import * 2 | #from manimHelper import add_obj 3 | 4 | #define helper function for adding TexMobjects 5 | #manimHelper is a function library I wrote to which 6 | #add_obj belongs. I posted it here explicitly for 7 | #reading convenience 8 | 9 | def add_obj(obj, string="-", where=RIGHT, color=WHITE): 10 | 11 | if color==WHITE: 12 | colors={} 13 | else: 14 | colors={string: color} 15 | 16 | new_obj=TexMobject( 17 | string, 18 | tex_to_color_map = colors 19 | ) 20 | 21 | return new_obj.next_to(obj, where) 22 | 23 | class geoSeries(Scene): 24 | def construct(self): 25 | # for x in range(-4, 4): 26 | # for y in range(-4, 4): 27 | # self.add(Dot(np.array([x, y, 0]), color=DARK_GREY)) 28 | ## only run while creating for help with aboslute coordinates 29 | 30 | 31 | 32 | tex1a = TexMobject( 33 | "r"+"S", 34 | tex_to_color_map={"S": ORANGE, "r":BLACK} 35 | ) 36 | 37 | tex1a.shift(np.array([-1,2,0])) 38 | 39 | tex1b = add_obj(tex1a, string="=") 40 | 41 | tex1c = TexMobject( 42 | "1 + r + r^2 + \\cdots", 43 | tex_to_color_map={"r": BLUE} 44 | ) 45 | 46 | tex1c.next_to(tex1b, RIGHT) 47 | 48 | 49 | 50 | tex2a = TexMobject( 51 | "r" + "S", 52 | tex_to_color_map={"S": ORANGE, "r": BLUE} 53 | ) 54 | 55 | tex2a.next_to(tex1a, np.array([0,-3,0])) 56 | 57 | tex2b = add_obj(tex2a, "=") 58 | 59 | tex2c = TexMobject( 60 | "{r} + {r}^2 + \\cdots", 61 | tex_to_color_map={"{r}": BLUE} 62 | ) 63 | 64 | tex2c.next_to(tex2b, RIGHT) 65 | 66 | 67 | 68 | 69 | ############################################# 70 | 71 | tex1aCopy = TexMobject( 72 | "r"+"S", 73 | tex_to_color_map={"S": ORANGE, "r":BLACK} 74 | ) 75 | 76 | tex1aCopy.shift(np.array([-1,2,0])) 77 | 78 | tex1bCopy = add_obj(tex1aCopy, string="=") 79 | 80 | tex1cCopy = TexMobject( 81 | "{1} + {r} + {r}^2 + \\cdots", 82 | tex_to_color_map={"{r}": BLUE} 83 | ) 84 | 85 | tex1cCopy.next_to(tex1b, RIGHT) 86 | 87 | 88 | tex2cMinus = TexMobject( 89 | "- {r} - {r}^2 - \\cdots", 90 | tex_to_color_map={"{r}": BLUE} 91 | ) 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | #coord2 = DOWN 102 | self.play(Write(tex1a), Write(tex1b), Write(tex1c)) 103 | self.wait(0.5) 104 | self.add(tex1aCopy, tex1bCopy, tex1cCopy) 105 | self.play(ApplyMethod(tex1a.shift, DOWN),ApplyMethod(tex1b.shift, DOWN) ,ApplyMethod(tex1c.shift, DOWN)) 106 | self.wait(1.5) 107 | self.play(FadeOut(tex1a), FadeOut(tex1b),GrowFromEdge(tex2a, LEFT),GrowFromEdge(tex2b, LEFT), 108 | FadeOut(tex1c, LEFT),GrowFromEdge(tex2c, LEFT) ) 109 | self.wait(2.5) 110 | self.play(ApplyMethod(tex1aCopy.shift, np.array([-.5,-2,0])),FadeOut(tex1bCopy) ) 111 | self.wait(.2) 112 | minus = add_obj(tex1aCopy, color=WHITE) 113 | self.add(minus) 114 | self.play(ApplyMethod(tex2a.next_to, minus, RIGHT ),FadeOut(tex2b) ) 115 | self.wait(.2) 116 | eq = add_obj(tex2a, "=") 117 | self.add( eq ) 118 | self.play(ApplyMethod(tex1cCopy.next_to, eq , RIGHT ) ) 119 | self.wait(.2) 120 | self.play(Transform(tex2c,tex2cMinus.next_to(tex1cCopy, DOWN))) 121 | self.wait(2) 122 | one=add_obj(eq, "1") 123 | zero=add_obj(one, "\\ + \\ 0") 124 | self.play(Transform(tex1cCopy, one ) ,Transform(tex2c, zero )) 125 | self.wait(1) 126 | self.play(FadeOut(tex2c)) 127 | self.wait(1) 128 | self.play(ApplyMethod(tex1cCopy.shift, np.array([0,1.5,0])), 129 | ApplyMethod(tex1aCopy.shift, np.array([0,1.5,0])), 130 | ApplyMethod(tex2a.shift, np.array([0,1.5,0])), 131 | ApplyMethod(minus.shift, np.array([0,1.5,0])), 132 | ApplyMethod(eq.shift, np.array([0,1.5,0])) 133 | ) 134 | 135 | #This very last self.play above is hackish .. 136 | #I discovered the Group function shortly hereafter thinking 137 | #there has got to be a better way... 138 | #This should have been done using Group (as it was below) 139 | 140 | self.wait(1.5) 141 | 142 | 143 | group = Group(tex1cCopy, tex1aCopy, tex2a, minus, eq) 144 | factor = TexMobject( 145 | "S (1-r) \\ = \\ 1", 146 | tex_to_color_map = {"S":ORANGE, "r":BLUE} 147 | ) 148 | factor.shift(np.array([1,1.5,0])) 149 | 150 | final = TexMobject( 151 | " S \\ = \\ \\frac{1}{1 - {r} }", 152 | 153 | tex_to_color_map = {"S":ORANGE , "{r}":BLUE} 154 | ) 155 | final.shift(np.array([.7,-.4,0])) 156 | 157 | 158 | self.play(Transform(group, factor)) 159 | self.wait(1.5) 160 | self.play(Transform(factor, final)) 161 | 162 | self.wait(2) 163 | 164 | #To make animation go to terminal and execute 165 | # manim geometric_series.py geoSeries 166 | -------------------------------------------------------------------------------- /lagrangian.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | """ 4 | Created on Tue Dec 3 10:26:51 2019 5 | 6 | @author: dillonberger 7 | """ 8 | 9 | from manimlib.imports import * 10 | 11 | 12 | class Lagrangian(Scene): 13 | def construct(self): 14 | 15 | F = TexMobject( 16 | "\\mathbf{F}" , color=YELLOW 17 | ) 18 | eq = TexMobject( 19 | "\\ = \\" 20 | ) 21 | 22 | m = TexMobject( 23 | "m", color=YELLOW 24 | ) 25 | 26 | 27 | a = TexMobject( 28 | "\\mathbf{a}", color=BLUE 29 | ) 30 | 31 | 32 | 33 | 34 | 35 | ddt = TexMobject( 36 | "{ \\mathrm{d} \\over \\mathrm{d} t }" 37 | ).scale(2) 38 | 39 | eq.next_to(F, RIGHT) 40 | m.next_to(eq, RIGHT) 41 | a.next_to(m, .2*RIGHT) 42 | 43 | 44 | group = Group(F, eq, m,a).shift(LEFT).shift(UP) 45 | group.scale(2) 46 | 47 | dvdt = TexMobject( 48 | "{ \\mathrm{d} \\mathbf{v} \\over \\mathrm{d} t }", 49 | tex_to_color_map = {"\\mathbf{v}": BLUE} 50 | ) 51 | dvdt.scale(2).next_to(m, RIGHT) 52 | v = dvdt.get_part_by_tex("\\mathbf{v}") 53 | d = dvdt.get_part_by_tex("\\mathrm{d}") 54 | 55 | ddt.move_to(dvdt) 56 | 57 | grad = TexMobject("- \\nabla U ( \\mathbf{x} ) ", tex_to_color_map={"U": WHITE,"\\mathbf{x}": BLUE}).scale(1.5) 58 | newF = TexMobject( 59 | "\\mathbf{F}" , color=YELLOW 60 | ).scale(1.5) 61 | neweq = TexMobject("=").scale(1.5) 62 | newF.next_to(F, 8*DOWN) 63 | neweq.next_to(newF,RIGHT) 64 | grad.next_to(neweq, RIGHT) 65 | 66 | 67 | 68 | 69 | ## Replacement transform replaces the object in the group .. use this 70 | 71 | self.play(Write(F)) 72 | self.play(Write(eq)) 73 | self.play(Write(m)) 74 | self.play(Write(a)) 75 | self.wait(1) 76 | self.play(ReplacementTransform(a, dvdt)) 77 | self.wait(1) 78 | self.play(CyclicReplace(dvdt, m)) 79 | self.wait(1) 80 | self.play(ApplyMethod(m.next_to, dvdt,RIGHT), ApplyMethod(v.next_to, m, .2*RIGHT), 81 | ApplyMethod(d.shift, RIGHT*.01)) 82 | mv = Group(m,v) 83 | self.wait(1) 84 | p = TexMobject("\\mathbf{p}", color=BLUE).move_to(mv).scale(2.1) 85 | self.play(ReplacementTransform(mv, p)) 86 | self.play(ApplyMethod(p.next_to, d, RIGHT*.3)) 87 | self.wait(1) 88 | self.play(GrowFromCenter(neweq), FadeInFrom(newF, 2*LEFT), FadeInFrom(grad, 2*RIGHT)) 89 | dpdt = Group(dvdt, p) 90 | self.wait(1) 91 | self.play(ApplyMethod(grad.next_to, eq, LEFT*2), FadeOutAndShift(F, 10*LEFT), 92 | ApplyMethod(dpdt.scale, 1.5/2) , FadeOutAndShift(newF, 10*LEFT), 93 | FadeOutAndShift(neweq,20*DOWN) ) 94 | newtons_law = Group(grad, eq, dpdt) 95 | self.play(ApplyMethod(newtons_law.shift, RIGHT)) 96 | newton_text = TexMobject( 97 | "\\text{Newton's $2^{nd}$ Law}", color=RED 98 | ).scale(2).next_to(newtons_law, UP*2) 99 | self.play(Write(newton_text)) 100 | box = Group(newtons_law, newton_text) 101 | self.play(ApplyMethod(box.scale, .4)) 102 | self.play(ApplyMethod(box.shift, 1.5*UP+ 5.35*LEFT)) 103 | 104 | lag_0 = TexMobject( 105 | "\\text{Lagrangian} \\ = \\ \\text{Kinetic Energy} - \\text{Potential Energy} ", 106 | tex_to_color_map={"\\text{Kinetic Energy}": BLUE, "\\text{Potential Energy}": WHITE, 107 | "\\text{Lagrangian}": YELLOW} 108 | ) 109 | 110 | lag = TexMobject( 111 | "\\mathcal{L} \\ = \\ \\frac{1}{2} {m} \\mathbf{v} ^2 \\ - \\ U( \\mathbf{x} )", 112 | tex_to_color_map = {"\\mathcal{L}": YELLOW,"\\mathbf{v}": BLUE, 113 | "\\mathbf{x}": BLUE} 114 | ).scale(2) 115 | lag_text = TexMobject("\\text{Lagrangian}", color=RED).next_to(lag, UP*2).scale(2) 116 | 117 | 118 | dummyLHS = TexMobject("\\mathcal{L}", color=YELLOW).shift(UP*.5).scale(1.5) 119 | 120 | keq = TexMobject("\\ = \\").next_to(dummyLHS, 2*RIGHT).scale(1.5) 121 | 122 | dummy = TexMobject( 123 | "\\frac{1}{2} {m} \\mathbf{v} ^2", 124 | tex_to_color_map = {"\\mathbf{v}": BLUE 125 | } 126 | ).next_to(keq, 2*RIGHT).scale(1.5) 127 | 128 | dummypot = TexMobject("\\ - \\ U( \\mathbf{x} )", 129 | tex_to_color_map={"\\mathbf{x}": BLUE}).next_to(dummy, 2*RIGHT).scale(1.5) 130 | 131 | 132 | 133 | 134 | eqGroup = Group(dummyLHS, keq, dummy, dummypot).move_to(ORIGIN).shift(UP*.5) 135 | 136 | summation = TexMobject("\\mathcal{L} \\ = \\ \\frac{1}{2} m \\sum_{k=1}^3 \\mathbf{v} _k^2\\ - \\ U( \\mathbf{x} )", 137 | tex_to_color_map = {"\\mathbf{v}" :BLUE, "\\mathbf{x}": BLUE, 138 | "\\mathcal{L}": YELLOW}).move_to(dummy).scale(1.5) 139 | 140 | self.play(Write(lag_0)) 141 | self.wait(2) 142 | self.play(ReplacementTransform(lag_0, lag)) 143 | self.wait(1) 144 | self.play(Write(lag_text)) 145 | self.wait(1.5) 146 | lag_box = Group(lag, lag_text) 147 | self.play(ApplyMethod(lag_box.match_width, box), ApplyMethod(lag_box.match_height, box)) 148 | self.play(ApplyMethod(lag_box.next_to, box, 7*RIGHT)) 149 | el_eqns = TexMobject( 150 | "{\\mathrm{d} \\over \\mathrm{d} t}", 151 | "{\\partial", "\\mathcal{L}", "\\over", "\\partial", "\\dot{\\mathbf{x}}", "_i}", 152 | "\\ = \\", 153 | "{\\partial", "\\mathcal{L}", "\\over", "\\partial", "\\mathbf{x}", "_i }" 154 | 155 | ).set_color_by_tex("\\mathcal{L}", YELLOW).set_color_by_tex("\\mathbf{x}", BLUE) 156 | el_eqns.set_color_by_tex("\\dot{\\mathbf{x}}", BLUE).scale(2).shift(DOWN) 157 | el_text= TexMobject("\\text{Euler-Lagrange Equations}", color=RED).scale(2).next_to(el_eqns,2*UP) 158 | self.play(Write(el_eqns)) 159 | self.wait(1.5) 160 | xdot = el_eqns.get_part_by_tex("\\dot{\\mathbf{x}}") 161 | vee = TexMobject("\\mathbf{v}", color=BLUE).scale(2.2).move_to(xdot).shift(DOWN*.1) 162 | self.play(ReplacementTransform(xdot, vee)) 163 | explain = TextMobject("For each vector component $i$", color=YELLOW) 164 | explain.next_to(el_eqns, 2*DOWN) 165 | self.play(Write(explain)) 166 | self.wait(1.5) 167 | self.play(Write(el_text), FadeOutAndShift(explain, DOWN)) 168 | self.wait(1.5) 169 | el_box = Group(el_eqns, el_text,vee) 170 | self.play(ApplyMethod(el_box.match_width, box), ApplyMethod(el_box.match_height, box)) 171 | self.play(ApplyMethod(el_box.next_to, lag_box, 7*RIGHT)) 172 | bgroup = Group(box, lag_box, el_box) 173 | self.wait(1) 174 | self.play(TransformFromCopy(lag, eqGroup)) 175 | self.wait(1.5) 176 | self.play(ReplacementTransform(eqGroup, summation), FadeOut(dummypot)) 177 | self.wait(3) 178 | el_1 = TexMobject("{\\partial", "\\mathcal{L}", "\\over", "\\partial", "\\mathbf{v}", "_i}" , 179 | "\\ = \\" 180 | ).set_color_by_tex("\\mathcal{L}", YELLOW).set_color_by_tex("\\mathbf{v}", BLUE).scale(1.2) 181 | el_1.next_to(summation, 3.5*DOWN).shift(3*LEFT) 182 | 183 | rhs = TexMobject("m" ,"\\mathbf{v}", "_i}").set_color_by_tex("\\mathbf{v}", BLUE).scale(1.2) 184 | rhs.next_to(el_1, RIGHT) 185 | self.play(TransformFromCopy(el_eqns, el_1 )) 186 | self.wait(1) 187 | self.play(Write(rhs)) 188 | self.wait(2) 189 | mom = TexMobject("\\mathbf{p} _i",tex_to_color_map={"\\mathbf{p}": BLUE}).scale(1.2).move_to(rhs) 190 | self.play(ReplacementTransform(rhs, mom)) 191 | derivt = TexMobject("\\frac{\\mathrm{d}}{\\mathrm{d} t}").scale(1.2).next_to(el_1,LEFT) 192 | pdot = TexMobject("{\\mathrm{d} \\mathbf{p} _i \\over \\mathrm{d} t }", 193 | tex_to_color_map={"\\mathbf{p}":BLUE}).move_to(mom).scale(1.2) 194 | self.wait(1) 195 | self.play(FadeInFrom(derivt, LEFT), ReplacementTransform(mom, pdot)) 196 | self.wait(1.5) 197 | derivx = TexMobject("{\\partial", "\\mathcal{L}", "\\over", "\\partial", "\\mathbf{x}", "_i }" 198 | 199 | ).set_color_by_tex("\\mathcal{L}", YELLOW).set_color_by_tex("\\mathbf{x}", BLUE) 200 | derivx.next_to(summation, 3.5*DOWN).shift(2.5*RIGHT).scale(1.2) 201 | dU = TexMobject( 202 | " = - { \\partial U \\over \\partial \\mathbf{x} _i}", 203 | tex_to_color_map={"\\mathbf{x}": BLUE}).scale(1.2) 204 | dU.next_to(derivx, RIGHT) 205 | self.play(TransformFromCopy(el_eqns,derivx)) 206 | self.wait(1) 207 | self.play(Write(dU)) 208 | self.wait(1.5) 209 | gp = Group(derivt, el_1, pdot, derivx, dU) 210 | self.play(ReplacementTransform(summation, lag), ApplyMethod(gp.shift, 2*UP)) 211 | brace = Brace(gp, 3*DOWN) 212 | eqText= brace.get_text("These are equal, by the Euler-Lagrange Equations", color=YELLOW) 213 | self.wait(1) 214 | self.play(GrowFromCenter(brace), Write(eqText)) 215 | self.wait(3) 216 | equality = TexMobject( 217 | "- { \\partial U \\over \\partial", "\\mathbf{x}", "_i} \\ = \\", 218 | "{\\mathrm{d}", "\\mathbf{p}", "_i \\over \\mathrm{d} t}" 219 | , 220 | tex_to_color_map={"\\mathbf{p}": BLUE, "\\mathbf{x}": BLUE} 221 | ).scale(2) 222 | self.play(FadeOutAndShift(brace, DOWN), FadeOutAndShift(eqText, DOWN), 223 | ReplacementTransform(gp, equality)) 224 | equality_text = TextMobject("Now, write this as a vector equation", color=YELLOW) 225 | equality_text.next_to(equality, 4*DOWN).scale(1.3) 226 | self.play(Write(equality_text)) 227 | final = TexMobject( 228 | "-\\nabla U(", "\\mathbf{x}", ") \\ = \\ ","{\\mathrm{d}", 229 | "\\mathbf{p}", "\\over \\mathrm{d} t}", 230 | tex_to_color_map={"\\mathbf{p}": BLUE, "\\mathbf{x}": BLUE} 231 | ).scale(2) 232 | self.wait(1.5) 233 | self.play(FadeOutAndShift(equality_text, DOWN), ReplacementTransform(equality, final)) 234 | square = Rectangle(color=YELLOW) 235 | square.surround(final) 236 | self.wait(1.5) 237 | self.play(Write(square)) 238 | g = Group(square, final) 239 | n2 = TextMobject("Newton's $2^{nd}$ Law", color=RED).next_to(g, UP).shift(DOWN*.5).scale(1.5) 240 | self.play(ApplyMethod(g.shift, DOWN), Write(n2) ) 241 | self.wait(5) 242 | 243 | 244 | -------------------------------------------------------------------------------- /log_int.py: -------------------------------------------------------------------------------- 1 | from manimlib.imports import * 2 | 3 | class logAnim(Scene): 4 | def construct(self): 5 | color = BLACK 6 | sc = 1 7 | lhs = TexMobject("\\int^x_1", "x^n", " \\ d x ", color=color).scale(sc).shift(2*LEFT) 8 | eq = TexMobject("\\ = \\ ",color=color).scale(sc).next_to(lhs,1.2*RIGHT) 9 | rhs = TexMobject("{ x^{n+1}-1 \\over n+1}",color=color).scale(sc).next_to(eq, RIGHT) 10 | 11 | 12 | self.play(Write(lhs)) 13 | self.play(Write(eq)) 14 | self.wait(1) 15 | self.play(Write(rhs)) 16 | self.play(ApplyMethod(rhs.shift, RIGHT)) 17 | 18 | lLim = TexMobject("\\lim_{ n\\to -1}",color=color).scale(sc).next_to(lhs, LEFT) 19 | rLim = TexMobject("\\lim_{ n\\to -1}",color=color).scale(sc).next_to(rhs, .3*LEFT) 20 | 21 | 22 | self.play(FadeInFrom(lLim, LEFT), FadeInFrom(rLim, LEFT)) 23 | self.wait(1) 24 | 25 | grand = lhs.get_parts_by_tex("x^n") 26 | integral = TexMobject("{1 \\over x }",color=color).scale(sc).move_to(grand) 27 | self.play(ReplacementTransform(grand, integral), FadeOut(lLim)) 28 | self.wait(1) 29 | 30 | LH = TexMobject(" \\frac{d}{dn}( x^{ n +1} - 1 ) \\over \\frac{d}{dn} ( n +1 ) ",color=color).scale(sc).move_to(rhs).shift(.5*RIGHT) 31 | 32 | self.play(ReplacementTransform(rhs, LH)) 33 | 34 | self.wait(2) 35 | 36 | new = TexMobject("\\frac{d}{dn}", "x^{ n +1 }",color=color).scale(sc).move_to(rhs).shift(.5*LEFT) 37 | 38 | self.play(ReplacementTransform(LH, new)) 39 | 40 | self.wait(1.5) 41 | 42 | exp = new.get_part_by_tex("x^{ n +1 }") 43 | 44 | 45 | elog = TexMobject("e^", "{( n + 1 ) \\ln( x )}", color=color).scale(sc).move_to(exp).shift(RIGHT*.7) 46 | 47 | self.play(ReplacementTransform(exp, elog)) 48 | 49 | self.wait(1.5) 50 | 51 | 52 | deriv = new.get_part_by_tex("\\frac{d}{dn}") 53 | res = TexMobject("\\ln(x)", color=color).scale(sc).move_to(deriv).shift(.2*RIGHT) 54 | 55 | self.play(ReplacementTransform(deriv, res), ApplyMethod(elog.shift, .4*RIGHT)) 56 | # self.play(ApplyMethod(elog.shift, LEFT*.7), FadeInFrom(res, RIGHT)) 57 | self.wait(1) 58 | 59 | 60 | # ApplyMethod(limGroup.shift, LEFT) 61 | 62 | 63 | 64 | self.play(CyclicReplace(res, rLim)) 65 | limGroup = Group(rLim, elog) 66 | one = TexMobject("\\cdot 1", color=color).scale(sc).next_to(res,RIGHT).shift(.1*LEFT) 67 | # self.play(ApplyMethod(res.shift, LEFT) ) 68 | self.wait(1.5) 69 | self.play(ReplacementTransform(limGroup, one)) 70 | self.wait(.5) 71 | 72 | 73 | 74 | square = Rectangle(color=BLACK) 75 | final = Group(lhs, integral, eq, res) 76 | 77 | self.play(ApplyMethod(final.shift, RIGHT),FadeOutAndShift(one, RIGHT)) 78 | square.surround(final) 79 | self.play(Write(square)) 80 | 81 | self.wait(4) 82 | -------------------------------------------------------------------------------- /pythagorean_circles.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | """ 4 | Created on Wed Nov 20 15:10:43 2019 5 | 6 | @author: dillonberger 7 | """ 8 | 9 | from manimlib.imports import * 10 | import numpy as np 11 | 12 | 13 | #add basic TexMobjects adjacent to obj 14 | def add_obj(obj, string="-", where=RIGHT, color=WHITE): 15 | 16 | if color==WHITE: 17 | colors={} 18 | else: 19 | colors={string: color} 20 | 21 | new_obj=TexMobject( 22 | string, 23 | tex_to_color_map = colors 24 | ) 25 | 26 | return new_obj.next_to(obj, where) 27 | 28 | 29 | #add basic TexMobjects adjacent to obj .. fix is a shift applied 30 | #afterward to correct for slight misalignments 31 | #add_obj() should be depracated by taking fix=np.array([0,0,0]) 32 | #but i was too lazy 33 | def add_obj_shift(obj, string="-", where=RIGHT, color=WHITE, 34 | fix = np.array([0,-.9,0])): 35 | 36 | if color==WHITE: 37 | colors={} 38 | else: 39 | colors={string: color} 40 | 41 | new_obj=TexMobject( 42 | string, 43 | tex_to_color_map = colors 44 | ) 45 | 46 | return new_obj.next_to(obj, where).shift(fix) 47 | 48 | #self explanatory 49 | def get_angle(v1, v2): 50 | n1 = v1/np.linalg.norm(v1) 51 | n2 = v2/np.linalg.norm(v2) 52 | 53 | dprod = np.dot(n1, n2) 54 | 55 | return np.arccos(dprod) 56 | 57 | 58 | class triangle(Scene): 59 | def construct(self): 60 | 61 | 62 | bot = Line( 63 | np.array([-2, -1, 0]), 64 | np.array([2, -1 , 0]), 65 | color=PURPLE 66 | ) 67 | 68 | vert = Line( 69 | np.array([2, -1 , 0]), 70 | np.array([2, 1, 0]), 71 | color=GREEN 72 | ) 73 | hyp = Line( 74 | np.array([2, 1, 0]), 75 | np.array([-2, -1, 0]), 76 | color=ORANGE 77 | ) 78 | bot.shift(2*UP) 79 | vert.shift(2*UP) 80 | hyp.shift(2*UP) 81 | 82 | 83 | a = add_obj(bot, "{2a}", color=PURPLE, where=DOWN) 84 | 85 | b = add_obj(vert, "{2b}", color=GREEN, where=RIGHT) 86 | 87 | c = add_obj_shift(hyp, "{2c}", color=ORANGE, where=UP) 88 | 89 | aGroup = Group(bot, a) 90 | bGroup = Group(vert, b) 91 | cGroup = Group(hyp, c) 92 | 93 | 94 | 95 | 96 | 97 | hypT = Line( 98 | np.array([-np.sqrt(20)/2 ,-1.5, 0]), 99 | np.array([np.sqrt(20)/2, -1.5,0]), 100 | color=ORANGE 101 | ) 102 | hypT.shift(4*LEFT) 103 | 104 | cT = add_obj(hypT, "{2c}", color=ORANGE, where=DOWN) 105 | 106 | cGroupT = Group(hypT, cT) 107 | 108 | 109 | botT = Line( 110 | np.array([-2,0,0]), 111 | np.array([2,0,0]), 112 | color=PURPLE 113 | ) 114 | 115 | 116 | 117 | aT = add_obj(botT, "{2a}", color=PURPLE, where=DOWN) 118 | 119 | aGroupT = Group(botT, aT) 120 | aGroupT.next_to(cGroupT, 4*RIGHT) 121 | 122 | 123 | vertT = Line( 124 | np.array([-1 ,0, 0]), 125 | np.array([1,0,0]), 126 | color=GREEN 127 | ) 128 | 129 | 130 | 131 | bT = add_obj(vertT, "{2b}", color=GREEN, where=DOWN) 132 | 133 | bGroupT = Group(vertT, bT) 134 | bGroupT.next_to(aGroupT, 4*RIGHT) 135 | 136 | linGroup = Group(aGroupT, bGroupT, cGroupT) 137 | 138 | l_circ = Circle(radius = np.sqrt(20)/2, color=ORANGE, fill_color=ORANGE, fill_opacity=.5) 139 | l_circ.next_to(cGroupT, UP).shift(np.array([0,-1,0])) 140 | 141 | m_circ = Circle(radius = 2, color=PURPLE, fill_color=PURPLE, fill_opacity=.5) 142 | m_circ.next_to(aGroupT, UP).shift(np.array([0,-1,0])) 143 | 144 | r_circ = Circle(radius = 1, color=GREEN, fill_color=GREEN, fill_opacity=.5) 145 | r_circ.next_to(bGroupT, UP).shift(np.array([0,0,0])) 146 | 147 | c2 = TexMobject( 148 | "{c}^2", tex_to_color_map={"{c}": ORANGE} 149 | ) 150 | c2.next_to(l_circ, 2.5*UP) 151 | c2.scale(2) 152 | 153 | a2 = TexMobject( 154 | "{a}^2", tex_to_color_map={"{a}": PURPLE} 155 | ) 156 | 157 | a2.next_to(m_circ, 4*UP) 158 | a2.scale(2) 159 | 160 | b2 = TexMobject( 161 | "{b}^2", tex_to_color_map={"{b}": GREEN} 162 | ) 163 | 164 | b2.next_to(r_circ, 8*UP) 165 | b2.scale(2) 166 | 167 | eq = add_obj(l_circ,"=", where=RIGHT) 168 | pl = add_obj(m_circ,"+", where=RIGHT) 169 | 170 | eqU = add_obj(eq,"=", where=10.5*UP) 171 | plU = add_obj(pl,"+", where=10.5*UP).shift(.8*LEFT) 172 | 173 | eqU.scale(2) 174 | plU.scale(2) 175 | 176 | p1 = add_obj_shift(c2, "\\pi", where=LEFT, fix=np.array([0,-.235,0])) 177 | p1.scale(2) 178 | 179 | p2 = add_obj_shift(a2, "\\pi", where=LEFT, fix=np.array([0,-.235,0])) 180 | p2.scale(2) 181 | 182 | p3 = add_obj_shift(b2, "\\pi", where=LEFT, fix=np.array([0,-.235,0])) 183 | p3.scale(2) 184 | 185 | self.play(GrowFromCenter(bot)) 186 | self.play(GrowFromCenter(vert)) 187 | self.play(GrowFromCenter(hyp)) 188 | self.play(GrowFromCenter(a),GrowFromCenter(b),GrowFromCenter(c)) 189 | self.wait(3) 190 | self.play(Transform(cGroup, cGroupT)) 191 | self.play(Transform(aGroup, aGroupT)) 192 | self.play(Transform(bGroup, bGroupT)) 193 | self.wait(1) 194 | self.play(ApplyMethod(linGroup.shift, 1.5*UP), 195 | FadeOut(aGroup), FadeOut(bGroup), FadeOut(cGroup)) 196 | self.wait(2) 197 | self.play(CounterclockwiseTransform(cGroupT, l_circ), 198 | CounterclockwiseTransform(aGroupT, m_circ), 199 | CounterclockwiseTransform(bGroupT, r_circ), 200 | FadeIn(c2), FadeIn(eq), FadeIn(pl), FadeIn(a2), FadeIn(b2), 201 | FadeIn(eqU), FadeIn(plU)) 202 | self.wait(1) 203 | self.play(FadeIn(p1), FadeIn(p2), FadeIn(p3)) 204 | self.wait(5) 205 | 206 | 207 | 208 | -------------------------------------------------------------------------------- /pythagorean_spiral.py: -------------------------------------------------------------------------------- 1 | """ 2 | Created on Wed Apr 29 11:26:23 2020 3 | 4 | @author: dillonberger 5 | """ 6 | 7 | 8 | from manimlib.imports import * 9 | import numpy as np 10 | from math import atan 11 | 12 | from matplotlib import cm 13 | 14 | 15 | sc = .6 16 | color = WHITE 17 | N = 18 18 | 19 | 20 | viridis = cm.get_cmap('viridis_r', N-1) 21 | 22 | 23 | 24 | def mktri(n): 25 | verts = [[n-1,0,0], [n,0,0], [n,np.sqrt(n),0]] 26 | poly = Polygon(*verts, color=WHITE, fill_opacity = .7 , fill_color = rgba_to_color(viridis(n)), 27 | stroke_width=2) 28 | return poly 29 | 30 | def mkLab(n,sc=sc): 31 | triLab = TexMobject('\\sqrt{'+str(n)+'}',color=color).move_to([n -.26, np.sqrt(n)/2.5, 0]) 32 | triLab.scale(sc) 33 | return triLab 34 | 35 | def linInterp(x1,x2): 36 | xs = np.arange(x1,x2, (x2-x1)/500) 37 | pts = [] 38 | objs = np.zeros(len(xs), dtype=object) 39 | 40 | for i in range(1,len(xs)): 41 | 42 | objs[i] = Line((xs[i-1], np.sqrt(xs[i-1]),0),(xs[i], np.sqrt(xs[i]),0),color=color) 43 | 44 | 45 | pts.append( 46 | 47 | Write( 48 | objs[i] 49 | ) 50 | 51 | ) 52 | 53 | return pts, np.trim_zeros(objs) 54 | 55 | def flipAnim(obj): 56 | anim = ApplyMethod(obj.flip, [0,1,0]) 57 | return anim 58 | 59 | def rotAnim(obj, n): 60 | anim = Rotate(obj, PI/2, about_point=[n,0,0]) 61 | return anim 62 | 63 | def centerAtOrigin(obj,n): 64 | anim = ApplyMethod(obj.shift, [np.sqrt(n+1)-n,0,0]) 65 | return anim 66 | 67 | def rotToPlace(obj, n, about = ORIGIN): 68 | θ = 0 69 | 70 | for nn in range(1,n+1): 71 | θ = θ + np.arctan(1/np.sqrt(nn)) 72 | 73 | 74 | anim = Rotate(obj, θ, about_point=about, CONFIG={"replace_mobject_with_target_in_scene": True}) 75 | 76 | return anim 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | class tri(Scene): 85 | 86 | CONFIG = { 87 | "camera_class": Camera, 88 | "camera_config": {"frame_center": [7,1,0], "background_color":BLACK}, 89 | } 90 | 91 | 92 | def construct(self): 93 | 94 | 95 | LineAnim, LineObjects = linInterp(0,N) 96 | 97 | triList = [] 98 | labList = [] 99 | triLabTups = np.zeros(N, dtype=object) 100 | triLabGroups = np.zeros(N, dtype=object) 101 | 102 | for n in range(1,N): 103 | triLabTups[n-1] = [mktri(n),mkLab(n)] 104 | triLabGroups[n-1] = Group(mktri(n), mkLab(n)) 105 | 106 | triList.append( ShowCreation( triLabTups[n-1][0] ) ) 107 | labList.append( ShowCreation( triLabTups[n-1][1] ) ) 108 | 109 | label = TexMobject('f(x) = \\sqrt{x}', color=color).move_to([4.5,3,0]) 110 | label.scale(1.25) 111 | LineAnim.append(FadeIn(label)) 112 | 113 | self.play(*triList) 114 | self.wait(1) 115 | self.play(*labList) 116 | self.wait(1) 117 | self.play(*LineAnim) 118 | self.wait(1) 119 | 120 | 121 | 122 | 123 | flipList = [] 124 | rotList = [] 125 | centList = [] 126 | finRotList = [] 127 | for n in range(1,len(triLabGroups)-1): 128 | obj2 = triLabGroups[n] 129 | tri = triLabTups[n] 130 | 131 | flipList.append(flipAnim(obj2[0])) 132 | flipList.append( ApplyMethod(obj2[1].shift, .5*LEFT) ) 133 | rotList.append(rotAnim(obj2,n)) 134 | centList.append(centerAtOrigin(obj2, n)) 135 | 136 | 137 | 138 | tupArray = np.array(triLabTups).flatten() 139 | tupArray = np.trim_zeros(tupArray) 140 | 141 | 142 | i = 0 143 | for ele in tupArray: 144 | if i==0: 145 | i+=1 146 | continue 147 | for el in ele: 148 | self.remove(el) 149 | i+=1 150 | 151 | self.play(*flipList, run_time=.8) 152 | self.wait(1) 153 | self.play(*rotList, run_time=.8) 154 | self.wait(1) 155 | self.play(*centList, run_time=.8) 156 | self.wait(1) 157 | 158 | 159 | 160 | fade = [FadeOut(label)] 161 | for li in LineObjects: 162 | fade.append(FadeOut(li)) 163 | 164 | 165 | 166 | 167 | self.play(*fade) 168 | 169 | self.wait(.2) 170 | 171 | 172 | 173 | self.remove(triLabTups[0][0] ) 174 | self.remove(triLabTups[0][1] ) 175 | 176 | 177 | toGroup = np.array(triLabGroups).flatten() 178 | toGroup = np.trim_zeros(toGroup) 179 | initGroup = Group(*toGroup) 180 | 181 | self.play(ApplyMethod(initGroup.shift, [7,1.5,0]) , run_time=.75) 182 | self.wait(.5) 183 | 184 | for n in range(1,len(triLabGroups)-1): 185 | obj2 = triLabGroups[n] 186 | tri = triLabTups[n] 187 | finRotList.append(rotToPlace(obj2,n, about=toGroup[0].get_corner(DOWN+LEFT))) 188 | 189 | self.play(*finRotList, run_time=.7) 190 | 191 | self.wait(4) 192 | 193 | 194 | self.play(FadeOut(initGroup)) 195 | -------------------------------------------------------------------------------- /wallis.py: -------------------------------------------------------------------------------- 1 | from manimlib.imports import * 2 | 3 | 4 | class Wallis(Scene): 5 | def construct(self): 6 | color = BLACK 7 | 8 | sc = 1 9 | sep = 2 10 | 11 | π = TexMobject('{\\pi} \\over 2', substrings_to_isolate=['{\\pi}'] ,color=color).shift(4*LEFT) 12 | 13 | 14 | eq = TexMobject(' = ', color=color ).next_to(π, RIGHT) 15 | two = TexMobject('2^{ \\phantom{ \\frac{1}{2} } }', color=color, 16 | substrings_to_isolate=['{ \\phantom{ \\frac{1}{2} } }']).next_to(eq, RIGHT) 17 | dot1 = TexMobject('\\cdot', color=color).next_to(two, 1.5*RIGHT) 18 | t2 = TexMobject('\\left( \\frac{2\\cdot 4}{3\\cdot 3} \\right)^{ \\phantom{ \\left( \\frac{1}{2} \\right)^2 } } ', 19 | color=color, substrings_to_isolate=['{ \\phantom{ \\left( \\frac{1}{2} \\right)^2 } }']) 20 | t2.next_to(two,3*RIGHT) 21 | dot2 = TexMobject('\\cdot', color=color).next_to(t2, 1.5*RIGHT) 22 | t3 = TexMobject('\\left( \\frac{4\\cdot 6 \\cdot 6 \\cdot 8}{5\\cdot 5 \\cdot 7 \\cdot 7} \\right)^{ \\phantom{ \\left( \\frac{1}{2} \\right)^3 } }', 23 | color=color, substrings_to_isolate=['{ \\phantom{ \\left( \\frac{1}{2} \\right)^3 } }']) 24 | t3.next_to(t2, 3.5*RIGHT) 25 | 26 | cdots = TexMobject('\\cdots', color=color).next_to(t3,1.5*RIGHT) 27 | 28 | terms = [π, eq, two, t2, t3, dot1, dot2,cdots] 29 | sc1 = Group(*terms) 30 | 31 | 32 | self.add(sc1) 33 | 34 | 35 | twoPos = two.get_part_by_tex('{ \\phantom{ \\frac{1}{2} } }') 36 | t2Pos = t2.get_part_by_tex('{ \\phantom{ \\left( \\frac{1}{2} \\right)^2 } }') 37 | t3Pos = t3.get_part_by_tex('{ \\phantom{ \\left( \\frac{1}{2} \\right)^3 } }') 38 | 39 | firstPow = TexMobject('{}^{ \\frac{1}{2} }', color='#0247FE').move_to(twoPos).scale(1) 40 | firstPow.shift(.15*RIGHT + .2*UP) 41 | 42 | t2Pow = TexMobject('{}^{ \\left(\\frac{1}{2}\\right)^2 }', color='#0247FE').move_to(t2Pos).scale(1) 43 | t2Pow.shift(.4*RIGHT + .6*UP) 44 | 45 | t3Pow = TexMobject('{}^{ \\left(\\frac{1}{2}\\right)^3 }', color='#0247FE').move_to(t3Pos).scale(1) 46 | t3Pow.shift(.4*RIGHT + .6*UP) 47 | 48 | πPos = π.get_part_by_tex('{\\pi}') 49 | e = TexMobject('e', color='#0247FE').move_to(πPos) 50 | 51 | rt=3 52 | self.play(GrowFromCenter(firstPow, run_time=rt), GrowFromCenter(t2Pow, run_time=rt), 53 | GrowFromCenter(t3Pow, run_time=rt), 54 | GrowFromCenter(e, run_time=rt), ShrinkToCenter(πPos, run_time=rt)) 55 | 56 | --------------------------------------------------------------------------------