├── Application_of _complex_number.py ├── Block Collision ├── Block_Collision.py └── hit.mp3 ├── Collision Simulation ├── Collision_Simulations.py └── hit.mp3 ├── FourierScene.py ├── Fourier_Transform_with_manim.py ├── Fourier_epycycles.py ├── Gradient and Tangent vector lemma.py ├── GradientApproximation.py ├── Integral Curve of Vector Field.py ├── Projectile Motion ├── Projectile_cairo_backened.py ├── Projectile_manimgl.py ├── Readme.txt └── grass.jpg ├── README.md ├── Solar_System.py ├── change_of_basis.py ├── create animation engine like 3blue1brown.py ├── double_pendulum.py └── why_sin^2_cos^2_1.py /Application_of _complex_number.py: -------------------------------------------------------------------------------- 1 | from manimlib import * 2 | 3 | def glow_dot(point, r_min=0.05, r_max=0.3, color=YELLOW, n=20, opacity_mult=1.0): 4 | result = VGroup(*( 5 | Dot(point, radius=interpolate(r_min, r_max, a)) 6 | for a in np.linspace(0, 1, n) 7 | )) 8 | result.set_fill(color, opacity=opacity_mult / 10) 9 | return result 10 | 11 | 12 | class Application(Scene): 13 | def construct(self) -> None: 14 | axes=Axes() 15 | axes.coords_to_point() 16 | square=Square(fill_color=RED,fill_opacity=0.2).rotate(10*DEGREES,about_point=[-1,0,0]).scale(1).move_to([-1,0,0],aligned_edge=LEFT+DOWN) 17 | square1=Square(fill_color=GREEN,fill_opacity=0.2).move_to(square.get_vertices()[2],aligned_edge=LEFT+UP).rotate(-60*DEGREES,about_point=square.get_vertices()[2])\ 18 | .scale(1.1,about_point=square.get_vertices()[2]) 19 | square2=Square(fill_color=YELLOW,fill_opacity=0.2).move_to(square1.get_vertices()[0],aligned_edge=LEFT+UP)\ 20 | .rotate(30*DEGREES,about_point=square1.get_vertices()[0])\ 21 | .scale(0.9,about_point=square1.get_vertices()[0]) 22 | def normal(point): 23 | x,y,_=point 24 | return np.array([-y,x,0]) 25 | def mag(point): 26 | return sum([x**2 for x in point])**0.5 27 | lent=mag(-square.get_vertices()[3]+square2.get_vertices()[0]) 28 | a=square2.get_vertices()[0]-square2.get_vertices()[1] 29 | b=square.get_vertices()[3]-square.get_vertices()[0] 30 | square3=Polygon(square.get_vertices()[3],square2.get_vertices()[0],square2.get_vertices()[0]+lent*(a/mag(a)), 31 | square2.get_vertices()[0] + lent * (a / mag(a))+lent*(normal(a)/mag(a)),fill_color=PINK,fill_opacity=0.2) 32 | vector=Arrow(square.get_vertices()[2],square.get_vertices()[3],color=YELLOW,buff=0) 33 | vector1 = Arrow(square3.get_vertices()[0], square3.get_vertices()[1], buff=0,color=RED) 34 | vector2 = Arrow(square3.get_vertices()[1], square2.get_vertices()[1], buff=0,color=BLUE) 35 | vector3 = Arrow(square2.get_vertices()[1], square.get_vertices()[2], buff=0,color=PINK) 36 | group=VGroup(square,square3,square1,square2) 37 | dot=glow_dot(square.get_vertices()[2],color=GREEN) 38 | tex=Tex('2a','2b','2c','2d').scale(0.5) 39 | tex[0].next_to(vector,DOWN,buff=0) 40 | tex[1].next_to(vector1, LEFT,buff=0) 41 | tex[2].next_to(vector2, UP,buff=0).shift(0.3*DOWN+0.2*LEFT) 42 | tex[3].next_to(vector3, RIGHT,buff=0).shift(0.3*LEFT+0.2*DOWN) 43 | dots=VGroup(*[Dot(group[i].get_center()) for i in range(4)]) 44 | line1=Arrow(dots[0].get_center(),dots[3].get_center(),color=BLUE,buff=0) 45 | line2 =Arrow(dots[1].get_center(), dots[2].get_center(),buff=0) 46 | group1=VGroup(vector, vector1, vector2, vector3) 47 | point=line_intersection([line1.get_start(),line1.get_end()],[line2.get_start(),line2.get_end()]) 48 | label=Tex('p','q','r','s','A','B') 49 | label[0].next_to(dots[0],UP) 50 | label[1].next_to(dots[1], RIGHT) 51 | label[2].next_to(dots[2], DOWN) 52 | label[3].next_to(dots[3], LEFT) 53 | label[4].next_to(line1,RIGHT,buff=0).shift(0.5*RIGHT) 54 | label[5].next_to(line2, LEFT,buff=0).shift(1.2*UP+RIGHT) 55 | d1=normalize(line1.get_end()-line1.get_start()) 56 | d2=normalize(line2.get_end()-line2.get_start()) 57 | angle=Polygon(point,point-d1*0.3,d2*0.3+point-d1*0.3,point+d2*0.3) 58 | group2=VGroup(dots,label,angle,dot,tex) 59 | maing=VGroup(group,group2,group1).shift(2*RIGHT+UP) 60 | self.play(ShowCreation(maing)) 61 | self.play(ShowCreation(line1.shift(2*RIGHT+UP))) 62 | self.play(ShowCreation(line2.shift(2*RIGHT+UP))) 63 | self.play(ShowCreation(dot)) 64 | # path=Polygon(dot.get_center(),vector.get_end(),vector1.get_end(),vector2.get_end()) 65 | cond=Tex('2a+2b+2c+2d=0').shift(4.7*LEFT+3*UP) 66 | cond1=Tex('2(a+b+c+d)=0').move_to(cond) 67 | cond2 = Tex('a+b+c+d=0').move_to(cond1) 68 | 69 | 70 | # self.play(MoveAlongPath(dot,path),run_time=8) 71 | # self.play(ShowCreation(cond)) 72 | # for i in [cond1,cond2]: 73 | # self.play(Transform(cond,i)) 74 | # self.wait() 75 | pvector, pvector1=vector.copy(),vector.copy() 76 | tex1=Tex('p=','a','+','ai').next_to(cond2,DOWN,aligned_edge=LEFT) 77 | tex2=Tex('q=2a+b+bi','s=2a+2b+c+ci','r=2a+2b+2c+d+di') 78 | tex2[0].next_to(tex1,DOWN,aligned_edge=LEFT) 79 | tex2[1].next_to(tex2[0], DOWN, aligned_edge=LEFT) 80 | tex2[2].next_to(tex2[1], DOWN, aligned_edge=LEFT) 81 | 82 | tex3=Tex('B','=','s','-','p','2a+2b+c+ci','a+ai') 83 | tex3[:5].next_to(tex2[2],DOWN,aligned_edge=LEFT) 84 | tex34=Tex('B','=','2a+2b+c+ci','-','(a+ai)').next_to(tex3[:5],DOWN,aligned_edge=LEFT) 85 | 86 | tex4 = Tex('A', '=', 'r', '-', 'q', '2a+2b+2c+d+di', '2a+b+bi') 87 | tex4[:5].next_to(tex34, DOWN, aligned_edge=LEFT) 88 | tex35 = Tex('A', '=', '2a+2b+2c+d+di', '-', '(2a+b+bi)').next_to(tex4[:5], DOWN, aligned_edge=LEFT) 89 | self.play(ShowCreation(tex1[0:2])) 90 | self.play(pvector.animate.scale(0.5,about_point=pvector1.get_start()),TransformFromCopy(tex[0],tex1[1],path_arc=3),run_time=5) 91 | 92 | pvector1.scale(0.5,about_point=pvector1.get_start()).shift(pvector.get_end()-pvector.get_start()).set_stroke(color=ORANGE) 93 | self.play(TransformFromCopy(pvector,pvector1,path_arc=2)) 94 | self.play(Rotate(pvector1,PI/2,about_point=pvector1.get_start()),TransformFromCopy(tex[0],tex1[2:])) 95 | self.play(Write(tex2[0])) 96 | self.play(Write(tex2[1])) 97 | self.play(Write(tex2[2])) 98 | 99 | self.play(Write(tex3[:5]),Indicate(line1)) 100 | self.play(Write(tex34[:2])) 101 | self.play(TransformFromCopy(tex3[2],tex34[2],path_arc=2)) 102 | self.play(TransformFromCopy(tex3[4], tex34[3:5]),path_arc=2) 103 | 104 | self.play(Write(tex4[:5]),Indicate(line2)) 105 | self.play(Write(tex35[:2])) 106 | self.play(TransformFromCopy(tex4[2], tex35[2], path_arc=2)) 107 | self.play(TransformFromCopy(tex4[4], tex35[3:5]), path_arc=2) 108 | self.remove(tex1,tex2,tex3,tex4) 109 | self.play(ApplyMethod(tex34.shift,2*UP),ApplyMethod(tex35.shift,3.5*UP),ApplyMethod(tex34.shift,3*UP),ApplyMethod(maing.shift,1.5*RIGHT), 110 | ApplyMethod(pvector.shift,1.5*RIGHT),ApplyMethod(pvector1.shift,1.5*RIGHT),ApplyMethod(line1.shift,1.5*RIGHT), 111 | ApplyMethod(line2.shift,1.5*RIGHT)) 112 | line1c=line1.copy() 113 | self.play(Rotate(line1c,-PI/2,about_point=line_intersection([line1.get_start(),line1.get_end()],[line2.get_start(),line2.get_end()]))) 114 | self.play(ApplyMethod(line1c.shift,-mag(line2.get_center()-line_intersection([line1.get_start(),line1.get_end()],[line2.get_start(),line2.get_end()]))*normalize(dots[1].get_center()-dots[2].get_center()))) 115 | tex34s=Tex('B','=','a+2b+c+ci','-','ai').move_to(tex34) 116 | tex35s = Tex('A', '=', 'b+2c+d+di', '-', 'bi').move_to(tex35).next_to(tex34s,DOWN,aligned_edge=LEFT) 117 | self.play(Transform(tex34,tex34s)) 118 | self.play(Transform(tex35, tex35s)) 119 | final=Tex('A+iB','=','b+2c+d+di', '-', 'bi','+i(a+2b+c+ci','-','ai)').next_to(tex35s,DOWN,aligned_edge=LEFT).shift(LEFT) 120 | final.scale(0.8,about_point=final[0][0].get_start()) 121 | final1 = Tex('A+iB', '=', 'b+2c+d+di', '-', 'bi', '+(ai+2bi+ci-c', '+', 'a)').next_to(final, DOWN, 122 | aligned_edge=LEFT) 123 | final1.scale(0.8, about_point=final[0][0].get_start()) 124 | final2 = Tex('A+iB', '=', 'a+b+c+d', '+i(a+b+c+d)',).next_to(final1, DOWN, 125 | aligned_edge=LEFT,buff=0.5) 126 | final2.scale(0.8, about_point=final[0][0].get_start()) 127 | self.play(Write(final)) 128 | self.play(Write(final1)) 129 | self.play(Write(final2)) 130 | 131 | 132 | path = Polygon(dot.get_center(), vector.get_end(), vector1.get_end(), vector2.get_end()) 133 | 134 | self.play(MoveAlongPath(dot,path),run_time=8) 135 | self.play(ShowCreation(cond)) 136 | for i in [cond1,cond2]: 137 | self.play(Transform(cond,i)) 138 | self.wait() 139 | 140 | self.play(Transform(final2[2],Tex('0').move_to(final2[2]))) 141 | self.play(Transform(final2[3], Tex('+0').move_to(final2[3]))) 142 | last=Tex('=0').next_to(final2,RIGHT) 143 | self.play(ShowCreation(last)) 144 | 145 | 146 | 147 | 148 | class Suppor(Scene): 149 | def construct(self) -> None: 150 | plane=ComplexPlane() 151 | label=plane.add_coordinate_labels() 152 | self.play(ShowCreation(label)) 153 | vect=Vector(RIGHT,color=RED) 154 | self.play(ShowCreation(vect)) 155 | tex=Tex('i',color=YELLOW) 156 | texc = Tex('-i', color=YELLOW) 157 | tex.next_to(vect,RIGHT,buff=0) 158 | self.play(FadeInFromPoint(tex,ORIGIN)) 159 | copy=vect.copy() 160 | tex.add_updater(lambda v:v.move_to(copy.get_end())) 161 | texc.add_updater(lambda v: v.move_to(vect.get_end())) 162 | 163 | self.play(Rotate(copy,PI/2,about_point=ORIGIN)) 164 | self.play(FadeInFromPoint(texc, ORIGIN)) 165 | self.play(Rotate(vect, -PI / 2, about_point=ORIGIN)) 166 | class Support(Scene): 167 | def construct(self) -> None: 168 | plane=ComplexPlane() 169 | label = plane.add_coordinate_labels() 170 | self.play(ShowCreation(label)) 171 | vect=Tex('A=1+i',color=YELLOW) 172 | vect1=Tex('B=-1+i',color=PINK) 173 | rvect=Vector(RIGHT+UP,color=YELLOW) 174 | rvect1=Vector(LEFT+UP,color=PINK) 175 | vect.next_to(rvect,UP,aligned_edge=rvect.get_end()).shift(RIGHT).scale(0.6) 176 | vect1.next_to(rvect1, UP, aligned_edge=rvect1.get_end()).shift(LEFT).scale(0.6) 177 | self.play(ShowCreation(vect),ShowCreation(rvect)) 178 | self.play(ShowCreation(vect1), ShowCreation(rvect1)) 179 | pr=Tex('A','=','-iB').shift(4*LEFT+3*UP) 180 | su=SurroundingRectangle(pr[2]) 181 | self.play(ShowCreation(pr)) 182 | self.play(Rotate(rvect1, -PI / 2, about_point=ORIGIN),ShowCreation(su)) 183 | pr1=Tex('A','+','iB=0').shift(4*LEFT+3*UP) 184 | self.remove(su) 185 | self.play(Transform(pr,pr1)) 186 | 187 | class Application2(Scene): 188 | def construct(self) -> None: 189 | frame=self.camera.frame 190 | circle=Circle() 191 | theta=Tex('\\theta_{1}',color=YELLOW).scale(0.5) 192 | theta2 = Tex('\\theta_{2}',color=PINK).scale(0.5) 193 | theta3 = Tex('(\\theta_{1}+\\theta_{2})', color=GREEN).scale(0.3) 194 | point=np.array([np.cos(45*DEGREES),np.sin(45*DEGREES),0]) 195 | point1 = np.array([np.cos(120 * DEGREES), np.sin(120 * DEGREES), 0]) 196 | point2 = np.array([np.cos(165 * DEGREES), np.sin(165 * DEGREES), 0]) 197 | 198 | line=DashedLine(ORIGIN,point) 199 | line1 = DashedLine(ORIGIN, point1) 200 | line3 = DashedLine(ORIGIN, point2) 201 | arc=Arc(0,45*DEGREES,radius=0.3,color=YELLOW) 202 | arc1 = Arc(0, 120 * DEGREES, radius=0.4,color=PINK) 203 | arc2=Arc(0, 165 * DEGREES, radius=0.5,color=GREEN) 204 | line2=Line(ORIGIN,RIGHT) 205 | line2.add_updater(lambda v:v.become(Line(ORIGIN,circle.get_end()))) 206 | theta.next_to(arc,RIGHT) 207 | theta2.next_to(arc1, RIGHT).shift(0.3*UP+0.5*LEFT) 208 | theta3.next_to(arc2,UP).shift(0.6*LEFT+0.3*DOWN).rotate(45*DEGREES) 209 | euler=Tex('e^{i\\theta}=\\cos(\\theta)+i\\sin(\\theta)',color=YELLOW).shift(4*LEFT+3*UP).fix_in_frame() 210 | firstp=Tex('e^{i\\theta_{1}}',color=YELLOW).shift(point*1.4).scale(0.5) 211 | secondp = Tex('e^{i\\theta_{2}}',color=PINK).shift(point1*1.2).scale(0.5) 212 | thirdp = Tex('e^{i(\\theta_{1}+\\theta_{2})}',color=GREEN).shift(point2 * 1.4).scale(0.5) 213 | euler1 = Tex('e^{i(\\theta_{1}+\\theta_{2})}=\\cos(\\theta_{1}+\\theta_{2})+i\\sin(\\theta_{1}+\\theta_{2})', color=GREEN).shift(4 * LEFT + 3 * UP).scale(0.6).fix_in_frame() 214 | euler1.next_to(euler,DOWN,aligned_edge=LEFT) 215 | lhs=Tex('L.H.S=e^{i(\\theta_{1}+\\theta_{2})}').next_to(euler1,DOWN,aligned_edge=LEFT).fix_in_frame() 216 | split=Tex('=e^{i\\theta_{1}}e^{i\\theta_{2}}').next_to(lhs,DOWN,aligned_edge=LEFT).fix_in_frame() 217 | further=Tex('=(\\cos(\\theta_{1})+i\\sin(\\theta_{1}))(\\cos(\\theta_{2})+i\\sin(\\theta_{2}))').scale(0.8).next_to(split,DOWN,aligned_edge=LEFT).fix_in_frame() 218 | more=Tex('=\\cos(\\theta_{1})\\cos(\\theta_{2})+i\\sin(\\theta_{1})\\cos(\\theta_{2})+i\\cos(\\theta_{1})\\sin(\\theta_{2})-\\sin(\\theta_{1})\\sin(\\theta_{2})').scale(0.6).next_to(further,DOWN,aligned_edge=LEFT).fix_in_frame() 219 | more1 = Tex( 220 | '=\\cos(\\theta_{1})\\cos(\\theta_{2})-\\sin(\\theta_{1})\\sin(\\theta_{2})+i(\\sin(\\theta_{1})\\cos(\\theta_{2})+\\cos(\\theta_{1})\\sin(\\theta_{2}))').scale( 221 | 0.6).next_to(more, DOWN, aligned_edge=LEFT).fix_in_frame() 222 | 223 | touch=Tex('\\cos(\\theta_{1})\\cos(\\theta_{2})-\\sin(\\theta_{1})\\sin(\\theta_{2})+i(\\sin(\\theta_{1})\\cos(\\theta_{2})+\\cos(\\theta_{1})\\sin(\\theta_{2}))=\\cos(\\theta_{1}+\\theta_{2})+i\\sin(\\theta_{1}+\\theta_{2})').scale( 224 | 0.6).next_to(more1, DOWN, aligned_edge=LEFT,buff=1).fix_in_frame() 225 | cos=Tex('\\cos(\\theta_{1}+\\theta_{2})=\\cos(\\theta_{1})\\cos(\\theta_{2})-\\sin(\\theta_{1})\\sin(\\theta_{2})').scale(0.8).next_to(touch,DOWN,aligned_edge=LEFT).fix_in_frame() 226 | 227 | sin=Tex('\\sin(\\theta_{1}+\\theta_{2})=\\sin(\\theta_{1})\\cos(\\theta_{2})+\\cos(\\theta_{1})\\sin(\\theta_{2})').scale(0.8).next_to(cos,DOWN,aligned_edge=LEFT).fix_in_frame() 228 | 229 | 230 | group=VGroup(circle,theta,theta2,line1,line,arc,arc1,line2,firstp,secondp,line3,arc2,theta3,thirdp) 231 | self.play(ShowCreation(line2)) 232 | self.play(ShowCreation(circle)) 233 | line2.clear_updaters() 234 | self.play(Write(euler)) 235 | self.play(frame.scale,0.4) 236 | self.play(ShowCreation(line), Write(theta), ShowCreation(arc), ShowCreation(firstp)) 237 | self.play(ShowCreation(line1), Write(theta2), ShowCreation(arc1), ShowCreation(secondp)) 238 | self.play(ShowCreation(line3), Write(theta3), ShowCreation(arc2), ShowCreation(thirdp)) 239 | 240 | self.play(group.shift,1.5*RIGHT+0.29*UP) 241 | for i in [ euler1, lhs, split, further, more,more1,touch,cos,sin]: 242 | self.play(ShowCreation(i)) 243 | self.wait() 244 | # self.add(euler, euler1, lhs, split, further, more,more1,touch,cos,sin) 245 | 246 | # 247 | class Application3(Scene): 248 | def construct(self) -> None: 249 | plane = ComplexPlane(x_range=[-10,10,1],y_range=[-8,8,1]) 250 | label = plane.add_coordinate_labels() 251 | self.play(ShowCreation(label)) 252 | point=np.array([2,1,0]) 253 | pointl=Tex('2+i') 254 | pointl1=Tex('1+2i') 255 | line=DashedLine(ORIGIN,point) 256 | dot=Dot(point,color=RED) 257 | base=Vector(RIGHT,color=YELLOW) 258 | dot1=Dot(UP+RIGHT) 259 | self.play(ShowCreation(base)) 260 | self.play(Rotate(base,90*DEGREES,about_point=ORIGIN)) 261 | self.wait() 262 | self.remove(base) 263 | 264 | pointl.shift(1.5*point) 265 | self.play(ShowCreation(pointl),ShowCreation(dot),ShowCreation(dot1)) 266 | self.remove(pointl) 267 | self.play(plane.shift,RIGHT) 268 | self.play(plane.shift, UP) 269 | self.play(Rotate(dot,PI/2,about_point=RIGHT+UP)) 270 | self.play(plane.shift, -RIGHT) 271 | self.play(plane.shift, -UP) 272 | pointl1.next_to(dot,UP) 273 | self.play(ShowCreation(pointl1)) 274 | rect=FullScreenRectangle().shift(LEFT*8) 275 | self.play(ShowCreation(rect)) 276 | form=Tex('z','=','(','z_{1}','-a',')','e^{i\\theta}','+','a').shift(5*LEFT+3*UP) 277 | form1 = Tex('z', '=', '(', '(2+1i)', '-(1+1i)', ')', 'e^{i\\frac{\\pi}{2}}', '+', '(1+1i)').\ 278 | scale(0.7).next_to(form,DOWN,aligned_edge=LEFT) 279 | simp=Tex('z', '=', '(', '1', '+0i)', ')', 'i', '+', '(1+1i)').next_to(form1,DOWN,aligned_edge=LEFT) 280 | fina=Tex('z=1+2i').next_to(simp,DOWN,aligned_edge=LEFT) 281 | self.play(ShowCreation(form)) 282 | self.add(form1[0:3],form1[5:8]) 283 | self.play(TransformFromCopy(form[3],form1[3])) 284 | self.play(TransformFromCopy(form[8], form1[8]),TransformFromCopy(form[4], form1[4])) 285 | self.play(ShowCreation(simp)) 286 | self.play(ShowCreation(fina)) 287 | 288 | class Application4(Scene): 289 | def construct(self) -> None: 290 | integ=Tex(*['\\int\\frac{1}{x^{n}-1}'],color=YELLOW) 291 | 292 | ex=Tex(*['\\int\\frac{1}{x^{2}-1}','=','\\int\\left[\\frac{1}{x-1}+\\frac{1}{x+1}\\right]'],color=BLUE).next_to(integ,DOWN) 293 | 294 | ex2=Tex(*['\\int\\frac{1}{x^{4}-1}','=','\\int\\left[\\frac{A}{x-1}+\\frac{B}{x+1}+\\frac{Cx}{x^{2}+1}+\\frac{D}{x^{2}+1}\\right]'],color=YELLOW).next_to(ex,DOWN) 295 | 296 | group=VGroup(integ,ex,ex2) 297 | self.play(ShowCreation(integ)) 298 | self.play(ShowCreation(ex)) 299 | self.play(ShowCreation(ex2)) 300 | 301 | self.wait(1) 302 | self.play(group.shift,3*UP) 303 | # ex3=Tex(*['\\int\\frac{1}{x^{5}-1}'],color=BLUE).next_to(group,DOWN) 304 | ex3=VMobject() 305 | for i in [5,6,7,'n',3]: 306 | tr=Tex(*['\\int\\frac{1}{',f'x^{i}','-1}'],color=BLUE).scale(2).next_to(group,DOWN) 307 | self.wait(1) 308 | self.play(Transform(ex3,tr)) 309 | self.play(ex3.animate.move_to(5*LEFT+3*UP).scale(0.5),FadeOut(group)) 310 | circl=Circle(color=YELLOW) 311 | self.add(circl) 312 | point=Dot(RIGHT*3,color=RED) 313 | lablep=Tex(*['p']).next_to(point,UP).scale(0.7) 314 | self.add(point,lablep) 315 | line=Line(circl.get_center(),point.get_center(),color=BLUE) 316 | self.add(line) 317 | all_point=VGroup() 318 | all_pointl=VGroup() 319 | for i in range(3): 320 | dot1=Dot(np.array([np.exp(1j*i*TAU/3).real,np.exp(1j*i*TAU/3).imag,0])) 321 | labeld=Tex(*[f"c_{i}"]).scale(0.7).next_to(dot1,UP) 322 | 323 | all_point.add(dot1) 324 | all_pointl.add(labeld) 325 | 326 | for k,col in enumerate([RED,PINK,GREEN]): 327 | 328 | arc=Arc(start_angle=k*TAU/3,angle=(k+1)*TAU/3-k*TAU/3,color=col) 329 | self.add(arc) 330 | brac=Brace(line,DOWN) 331 | brac1 = Brace(Line(point.get_center(),RIGHT), UP) 332 | self.add(brac) 333 | length=Tex('x','x-1').scale(0.7) 334 | length[0].next_to(brac,DOWN) 335 | length[1].next_to(brac1,UP) 336 | self.play(ShowCreationThenFadeOut(length[0],lag_ratio=2),ShowCreationThenFadeOut(brac,lag_ratio=2)) 337 | self.play(ShowCreationThenFadeOut(length[1], lag_ratio=2), ShowCreationThenFadeOut(brac1, lag_ratio=2)) 338 | 339 | line1=DashedLine(point.get_center(),all_point[1].get_center()) 340 | line2 = DashedLine(point.get_center(), all_point[2].get_center()) 341 | line3=DashedLine(all_point[1].get_center(),all_point[2].get_center()) 342 | ngon=Polygon(*[i.get_center() for i in all_point]) 343 | ngon1 = Polygon(*[i.get_center() for i in all_point]) 344 | gon4=Polygon(*[np.array([np.exp(1j*i*TAU/4).real,np.exp(1j*i*TAU/4).imag,0]) for i in range(4)]) 345 | gon5 = Polygon(*[np.array([np.exp(1j * i * TAU / 5).real, np.exp(1j * i * TAU / 5).imag, 0]) for i in range(5)]) 346 | self.play(ShowCreation(ngon)) 347 | for i in [gon4,gon5,ngon1]: 348 | self.play(Transform(ngon,i)) 349 | self.play(ShowCreation(all_point)) 350 | 351 | self.play(ShowCreation(all_pointl)) 352 | # self.add(line1,line2,line3) 353 | self.play(ShowCreation(line1)) 354 | self.play(Indicate(line1)) 355 | c_point = Tex('\\left(-\\frac{1}{2},\\frac{\\sqrt{3}}{2} \\right)').scale(0.5).next_to(all_point[1], UP, buff=1) 356 | c_point1 = Tex('\\left( -\\frac{1}{2},-\\frac{\\sqrt{3}}{2} \\right)').scale(0.5).next_to(all_point[2], DOWN, 357 | buff=1) 358 | c_point2 = Tex('\\left( 1,0 \\right)').next_to(all_point[0], RIGHT) 359 | p_pont=Tex('\\left( x,0 \\right)').next_to(point, RIGHT) 360 | self.play(ShowCreation(c_point),ShowCreation(p_pont),ShowCreation(c_point1),ShowCreation(c_point2)) 361 | 362 | root_unity=Tex(*['e^{\\frac{2\\pi ik}{n}}']).scale(1.5).shift(5*LEFT+3*UP) 363 | k_running=Tex('k=0,1,2...n-1').next_to(root_unity,RIGHT,buff=1) 364 | self.play(ex3.animate.shift(8*RIGHT)) 365 | self.play(ShowCreation(root_unity),ShowCreation(k_running)) 366 | self.play(FadeOut(c_point1),FadeOut(c_point2)) 367 | dista=Tex("\\sqrt{(y_{1}-y_{0})^{2}+(x_{1}-x_{0})^{2}}").next_to(root_unity,DOWN,aligned_edge=LEFT,buff=1).scale(0.8) 368 | dista1 = Tex("\\sqrt{\\left(0-\\frac{\\sqrt{3}}{2}\\right)^{2}+\\left(x+\\frac{1}{2}\\right)^{2}}").move_to(dista).scale(0.6) 369 | dista2 = Tex("\\sqrt{\\left(0+\\frac{\\sqrt{3}}{2}\\right)^{2}+\\left(x+\\frac{1}{2}\\right)^{2}}").move_to( 370 | dista).scale(0.6).next_to(line2.get_center(),DOWN).scale(0.7) 371 | 372 | self.play(Write(dista)) 373 | self.play(Transform(dista,dista1)) 374 | self.play(dista.animate.next_to(line1.get_center(),UP,aligned_edge=ORIGIN).scale(0.7)) 375 | self.play(FadeOut(c_point)) 376 | self.play(FadeIn(c_point1)) 377 | self.play(FadeIn(line2)) 378 | self.play(Write(dista2)) 379 | 380 | simply=Tex('x^{n}-1=').move_to(5*LEFT+3*UP) 381 | pc=Tex('U_{3}(x)=','pc_{0}','.','pc_{1}','.','pc_{2}').next_to(root_unity,DOWN,aligned_edge=LEFT) 382 | self.play(ShowCreation(pc)) 383 | self.play(ShowCreationThenFadeOut(length[1], lag_ratio=2), ShowCreationThenFadeOut(brac1, lag_ratio=2)) 384 | pc1 = Tex('U_{3}(x)=', '(x-1)', '.', '\\sqrt{\\left(0-\\frac{\\sqrt{3}}{2}\\right)^{2}+\\left(x+\\frac{1}{2}\\right)^{2}}', '.', '\\sqrt{\\left(0+\\frac{\\sqrt{3}}{2}\\right)^{2}+\\left(x+\\frac{1}{2}\\right)^{2}}')\ 385 | .scale(0.5).next_to(root_unity, DOWN, aligned_edge=LEFT) 386 | pc2=Tex('U_{3}(x)=(x-1)(x^{2}+x+1)',color=BLUE).scale(0.7).next_to(pc1,DOWN,aligned_edge=LEFT) 387 | 388 | self.play(Transform(pc,pc1)) 389 | self.play(Write(pc2)) 390 | 391 | class Application41(Scene): 392 | def construct(self) -> None: 393 | pc2 = Tex('U_{3}(x)=(x-1)(x^{2}+x+1)', color=BLUE).scale(0.7) 394 | ex = Tex(*['\\int\\frac{1}{x^{3}-1}', '=', '\\int\\frac{1}{(x-1)(x^{2}+x+1)}'], 395 | color=YELLOW).next_to(pc2, DOWN) 396 | part=Tex("=\\int\\frac{1}{3(x-1)}+\\int\\frac{-(x+2)}{3(x^2+x+1)}",color=BLUE).scale(0.7) 397 | self.add(pc2,ex) 398 | self.play(ex[2].animate.scale(0.7).next_to(ex,DOWN,aligned_edge=LEFT)) 399 | part.next_to(ex[2],RIGHT) 400 | self.play(Write(part)) 401 | self.play(AnimationGroup(*[FadeOut(i) for i in [pc2,ex,part]])) 402 | circl = Circle(color=YELLOW) 403 | gon5 = Polygon(*[np.array([np.exp(1j * i * TAU / 5).real, np.exp(1j * i * TAU / 5).imag, 0]) for i in range(5)]) 404 | self.play(ShowCreation(circl)) 405 | self.play(ShowCreation(gon5)) 406 | 407 | point = Dot(RIGHT * 3, color=RED) 408 | lablep = Tex(*['p']).next_to(point, UP).scale(0.7) 409 | self.add(point, lablep) 410 | line = Line(circl.get_center(), point.get_center(), color=BLUE) 411 | self.add(line) 412 | for i in [np.array([np.exp(1j * i * TAU / 5).real, np.exp(1j * i * TAU / 5).imag, 0]) for i in range(5)]: 413 | lin=DashedLine( point.get_center(),i, color=RED_E) 414 | self.play(ShowCreation(lin)) 415 | u5=Tex("U_{5}(x)=(x-1).\\left(x^{2}+\\left[\\frac{1+\\sqrt{5}}{2}\\right]x+1\\right)." 416 | "\\left(x^{2}+\\left[\\frac{1-\\sqrt{5}}{2}\\right]x+1\\right)",color=BLUE).scale(0.7) .next_to(circl,DOWN) 417 | self.play(ShowCreation(u5),run_time=4) 418 | self.wait(2) 419 | 420 | 421 | class Root_of_unity(Scene): 422 | def construct(self) -> None: 423 | root_unity = Tex(*['e^{\\frac{2\\pi ik}{n}}']).scale(1.5).shift(5 * LEFT + 3 * UP) 424 | k_running = Tex('k=0,1,2...n-1').next_to(root_unity, RIGHT, buff=1) 425 | self.play(Write(root_unity)) 426 | self.play(Write(k_running)) 427 | exp=Tex("x^{n}-1=0").next_to(root_unity,DOWN,aligned_edge=LEFT) 428 | self.play(Write(exp)) 429 | circl = Circle(color=YELLOW).scale(2) 430 | gon5 = Polygon(*[np.array([np.exp(1j * i * TAU / 8).real, np.exp(1j * i * TAU / 8).imag, 0]) for i in range(8)],color=RED).scale(2) 431 | self.play(ShowCreation(circl)) 432 | self.play(ShowCreation(gon5)) 433 | -------------------------------------------------------------------------------- /Block Collision/Block_Collision.py: -------------------------------------------------------------------------------- 1 | from manimlib.imports import * 2 | 3 | # I took the code from here 4 | #https://github.com/3b1b/manim/blob/cairo-backend/from_3b1b/old/clacks/question.py 5 | class Block(Square): 6 | CONFIG = { 7 | "mass": 1, 8 | "velocity": 0, 9 | "width": None, 10 | "label_text": None, 11 | "label_scale_value": 0.8, 12 | "fill_opacity": 1, 13 | "stroke_width": 3, 14 | "stroke_color": WHITE, 15 | "fill_color": None, 16 | "sheen_direction": UL, 17 | "sheen_factor": 0.5, 18 | "sheen_direction": UL, 19 | } 20 | 21 | def __init__(self, **kwargs): 22 | digest_config(self, kwargs) 23 | if self.width is None: 24 | self.width = self.mass_to_width(self.mass) 25 | if self.fill_color is None: 26 | self.fill_color = self.mass_to_color(self.mass) 27 | if self.label_text is None: 28 | self.label_text = self.mass_to_label_text(self.mass) 29 | if "width" in kwargs: 30 | kwargs.pop("width") 31 | Square.__init__(self, side_length=self.width, **kwargs) 32 | self.label = self.get_label() 33 | self.add(self.label) 34 | 35 | def get_label(self): 36 | label = TextMobject(self.label_text) 37 | label.scale(self.label_scale_value) 38 | label.next_to(self, UP, SMALL_BUFF) 39 | return label 40 | 41 | def get_points_defining_boundary(self): 42 | return self.points 43 | 44 | def mass_to_color(self, mass): 45 | colors = [ 46 | LIGHT_GREY, 47 | BLUE_D, 48 | BLUE_D, 49 | BLUE_E, 50 | BLUE_E, 51 | DARK_GREY, 52 | DARK_GREY, 53 | BLACK, 54 | ] 55 | index = min(int(np.log10(mass)), len(colors) - 1) 56 | return colors[index] 57 | 58 | def mass_to_width(self, mass): 59 | return 1 + 0.25 * np.log10(mass) 60 | 61 | def mass_to_label_text(self, mass): 62 | return "{:,}\\,kg".format(int(mass)) 63 | 64 | 65 | class SlidingBlocks(VGroup): 66 | CONFIG = { 67 | "block1_config": { 68 | "distance": 7, 69 | "mass": 1e6, 70 | "velocity": -2, 71 | }, 72 | "block2_config": { 73 | "distance": 3, 74 | "mass": 1, 75 | "velocity": 0, 76 | }, 77 | "collect_clack_data": True, 78 | } 79 | 80 | def __init__(self, scene, **kwargs): 81 | VGroup.__init__(self, **kwargs) 82 | self.scene = scene 83 | self.floor = scene.floor 84 | self.wall = scene.wall 85 | 86 | self.block1 = self.get_block(**self.block1_config) 87 | self.block2 = self.get_block(**self.block2_config) 88 | self.mass_ratio = self.block2.mass / self.block1.mass 89 | self.phase_space_point_tracker = self.get_phase_space_point_tracker() 90 | self.add( 91 | self.block1, self.block2, 92 | self.phase_space_point_tracker, 93 | ) 94 | self.add_updater(self.__class__.update_positions) 95 | 96 | if self.collect_clack_data: 97 | self.clack_data = self.get_clack_data() 98 | 99 | def get_block(self, distance, **kwargs): 100 | block = Block(**kwargs) 101 | block.move_to( 102 | self.floor.get_top()[1] * UP + 103 | (self.wall.get_right()[0] + distance) * RIGHT, 104 | DL, 105 | ) 106 | return block 107 | 108 | def get_phase_space_point_tracker(self): 109 | block1, block2 = self.block1, self.block2 110 | w2 = block2.get_width() 111 | s1 = block1.get_left()[0] - self.wall.get_right()[0] - w2 112 | s2 = block2.get_right()[0] - self.wall.get_right()[0] - w2 113 | result = VectorizedPoint([ 114 | s1 * np.sqrt(block1.mass), 115 | s2 * np.sqrt(block2.mass), 116 | 0 117 | ]) 118 | 119 | result.velocity = np.array([ 120 | np.sqrt(block1.mass) * block1.velocity, 121 | np.sqrt(block2.mass) * block2.velocity, 122 | 0 123 | ]) 124 | return result 125 | 126 | def update_positions(self, dt): 127 | self.phase_space_point_tracker.shift( 128 | self.phase_space_point_tracker.velocity * dt 129 | ) 130 | self.update_blocks_from_phase_space_point_tracker() 131 | 132 | def update_blocks_from_phase_space_point_tracker(self): 133 | block1, block2 = self.block1, self.block2 134 | ps_point = self.phase_space_point_tracker.get_location() 135 | 136 | theta = np.arctan(np.sqrt(self.mass_ratio)) 137 | ps_point_angle = angle_of_vector(ps_point) 138 | 139 | n_clacks = int(ps_point_angle / theta) 140 | reflected_point = rotate_vector( 141 | ps_point, 142 | -2 * np.ceil(n_clacks / 2) * theta 143 | ) 144 | reflected_point = np.abs(reflected_point) 145 | 146 | shadow_wall_x = self.wall.get_right()[0] + block2.get_width() 147 | floor_y = self.floor.get_top()[1] 148 | s1 = reflected_point[0] / np.sqrt(block1.mass) 149 | s2 = reflected_point[1] / np.sqrt(block2.mass) 150 | block1.move_to( 151 | (shadow_wall_x + s1) * RIGHT + 152 | floor_y * UP, 153 | DL, 154 | ) 155 | block2.move_to( 156 | (shadow_wall_x + s2) * RIGHT + 157 | floor_y * UP, 158 | DR, 159 | ) 160 | 161 | self.scene.update_num_clacks(n_clacks) 162 | 163 | def get_clack_data(self): 164 | ps_point = self.phase_space_point_tracker.get_location() 165 | ps_velocity = self.phase_space_point_tracker.velocity 166 | if ps_velocity[1] != 0: 167 | raise Exception( 168 | "Haven't implemented anything to gather clack " 169 | "data from a start state with block2 moving" 170 | ) 171 | y = ps_point[1] 172 | theta = np.arctan(np.sqrt(self.mass_ratio)) 173 | 174 | clack_data = [] 175 | for k in range(1, int(PI / theta) + 1): 176 | clack_ps_point = np.array([ 177 | y / np.tan(k * theta), 178 | y, 179 | 0 180 | ]) 181 | time = get_norm(ps_point - clack_ps_point) / get_norm(ps_velocity) 182 | reflected_point = rotate_vector( 183 | clack_ps_point, 184 | -2 * np.ceil((k - 1) / 2) * theta 185 | ) 186 | block2 = self.block2 187 | s2 = reflected_point[1] / np.sqrt(block2.mass) 188 | location = np.array([ 189 | self.wall.get_right()[0] + s2, 190 | block2.get_center()[1], 191 | 0 192 | ]) 193 | if k % 2 == 1: 194 | location += block2.get_width() * RIGHT 195 | clack_data.append((location, time)) 196 | return clack_data 197 | 198 | 199 | # TODO, this is untested after turning it from a 200 | # ContinualAnimation into a VGroup 201 | class ClackFlashes(VGroup): 202 | CONFIG = { 203 | "flash_config": { 204 | "run_time": 0.5, 205 | "line_length": 0.1, 206 | "flash_radius": 0.2, 207 | }, 208 | "start_up_time": 0, 209 | "min_time_between_flashes": 1 / 30, 210 | } 211 | 212 | def __init__(self, clack_data, **kwargs): 213 | VGroup.__init__(self, **kwargs) 214 | self.flashes = [] 215 | last_time = 0 216 | for location, time in clack_data: 217 | if (time - last_time) < self.min_time_between_flashes: 218 | continue 219 | last_time = time 220 | flash = Flash(location, **self.flash_config) 221 | flash.begin() 222 | for sm in flash.mobject.family_members_with_points(): 223 | if isinstance(sm, VMobject): 224 | sm.set_stroke(YELLOW, 3) 225 | sm.set_stroke(WHITE, 6, 0.5, background=True) 226 | flash.start_time = time 227 | flash.end_time = time + flash.run_time 228 | self.flashes.append(flash) 229 | 230 | self.time = 0 231 | self.add_updater(lambda m: m.update(dt)) 232 | 233 | def update(self, dt): 234 | time = self.time 235 | self.time += dt 236 | for flash in self.flashes: 237 | if flash.start_time < time < flash.end_time: 238 | if flash.mobject not in self.submobjects: 239 | self.add(flash.mobject) 240 | flash.update( 241 | (time - flash.start_time) / flash.run_time 242 | ) 243 | else: 244 | if flash.mobject in self.submobjects: 245 | self.remove(flash.mobject) 246 | 247 | 248 | class Wall(Line): 249 | CONFIG = { 250 | "tick_spacing": 0.5, 251 | "tick_length": 0.25, 252 | "tick_style": { 253 | "stroke_width": 1, 254 | "stroke_color": WHITE, 255 | }, 256 | } 257 | 258 | def __init__(self, height, **kwargs): 259 | Line.__init__(self, ORIGIN, height * UP, **kwargs) 260 | self.height = height 261 | self.ticks = self.get_ticks() 262 | self.add(self.ticks) 263 | 264 | def get_ticks(self): 265 | n_lines = int(self.height / self.tick_spacing) 266 | lines = VGroup(*[ 267 | Line(ORIGIN, self.tick_length * UR).shift(n * self.tick_spacing * UP) 268 | for n in range(n_lines) 269 | ]) 270 | lines.set_style(**self.tick_style) 271 | lines.move_to(self, DR) 272 | return lines 273 | 274 | 275 | class BlocksAndWallScene(Scene): 276 | CONFIG = { 277 | "include_sound": True, 278 | "collision_sound": "hit.mp3", 279 | "count_clacks": True, 280 | "counter_group_shift_vect": LEFT, 281 | "sliding_blocks_config": {}, 282 | "floor_y": -2, 283 | "wall_x": -6, 284 | "n_wall_ticks": 15, 285 | "counter_label": "\\# Collisions: ", 286 | "show_flash_animations": True, 287 | "min_time_between_sounds": 0.004, 288 | } 289 | 290 | def setup(self): 291 | self.track_time() 292 | self.add_floor_and_wall() 293 | self.add_blocks() 294 | if self.show_flash_animations: 295 | self.add_flash_animations() 296 | 297 | if self.count_clacks: 298 | self.add_counter() 299 | 300 | def add_floor_and_wall(self): 301 | self.floor = self.get_floor() 302 | self.wall = self.get_wall() 303 | self.add(self.floor, self.wall) 304 | 305 | def add_blocks(self): 306 | self.blocks = SlidingBlocks(self, **self.sliding_blocks_config) 307 | if hasattr(self.blocks, "clack_data"): 308 | self.clack_data = self.blocks.clack_data 309 | self.add(self.blocks) 310 | 311 | def add_flash_animations(self): 312 | self.clack_flashes = ClackFlashes(self.clack_data) 313 | self.add(self.clack_flashes) 314 | 315 | def track_time(self): 316 | time_tracker = ValueTracker() 317 | time_tracker.add_updater(lambda m, dt: m.increment_value(dt)) 318 | self.add(time_tracker) 319 | self.get_time = time_tracker.get_value 320 | 321 | def add_counter(self): 322 | self.n_clacks = 0 323 | counter_label = TextMobject(self.counter_label) 324 | counter_mob = Integer(self.n_clacks) 325 | counter_mob.next_to( 326 | counter_label[-1], RIGHT, 327 | ) 328 | counter_mob.align_to(counter_label[-1][-1], DOWN) 329 | counter_group = VGroup( 330 | counter_label, 331 | counter_mob, 332 | ) 333 | counter_group.to_corner(UR) 334 | counter_group.shift(self.counter_group_shift_vect) 335 | self.add(counter_group) 336 | 337 | self.counter_mob = counter_mob 338 | 339 | def get_wall(self): 340 | height = (FRAME_HEIGHT / 2) - self.floor_y 341 | wall = Wall(height=height) 342 | wall.shift(self.wall_x * RIGHT) 343 | wall.to_edge(UP, buff=0) 344 | return wall 345 | 346 | def get_floor(self): 347 | floor = Line(self.wall_x * RIGHT, FRAME_WIDTH * RIGHT / 2) 348 | floor.shift(self.floor_y * UP) 349 | return floor 350 | 351 | def update_num_clacks(self, n_clacks): 352 | if hasattr(self, "n_clacks"): 353 | if n_clacks == self.n_clacks: 354 | return 355 | self.counter_mob.set_value(n_clacks) 356 | 357 | def add_clack_sounds(self, clack_data): 358 | clack_file = self.collision_sound 359 | total_time = self.get_time() 360 | times = [ 361 | time 362 | for location, time in clack_data 363 | if time < total_time 364 | ] 365 | last_time = 0 366 | for time in times: 367 | d_time = time - last_time 368 | if d_time < self.min_time_between_sounds: 369 | continue 370 | last_time = time 371 | self.add_sound( 372 | clack_file, 373 | time_offset=(time - total_time), 374 | gain=-20, 375 | ) 376 | return self 377 | 378 | def tear_down(self): 379 | if self.include_sound: 380 | self.add_clack_sounds(self.clack_data) 381 | 382 | # Animated scenes 383 | class BlocksAndWallExampleMass1e2(BlocksAndWallScene): # Run This Class To Render The Animation 384 | CONFIG = { 385 | "sliding_blocks_config": { 386 | "block1_config": { 387 | "mass": 1e6, #<------Channge The According to you 388 | "velocity": -0.6, 389 | } 390 | }, 391 | "wait_time": 35, #<-----Change the Run time 392 | } 393 | def construct(self): 394 | self.wait(self.wait_time) 395 | -------------------------------------------------------------------------------- /Block Collision/hit.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MathAnimation1198/ManimTutorial/1b708cc5cef09b1ceebfcacab36885840cb0091b/Block Collision/hit.mp3 -------------------------------------------------------------------------------- /Collision Simulation/Collision_Simulations.py: -------------------------------------------------------------------------------- 1 | from manimlib import * 2 | 3 | class Myscreen(Rectangle): 4 | def __init__(self,height=4): 5 | self.height=height 6 | self.width=self.height*(16/9) 7 | Rectangle.__init__(self,width=self.width,height=height) 8 | def get_right(self): 9 | return self.width/2 10 | def get_left(self): 11 | return -self.width/2 12 | def get_top(self): 13 | return self.height/2 14 | def get_down(self): 15 | return -self.height/2 16 | 17 | class Ball(Circle): 18 | def __init__(self,radius=1): 19 | self.radius=radius 20 | self.mass=PI*self.radius**2 21 | Circle.__init__(self,radius=self.radius,color=BLUE) 22 | self.set_fill(color=BLUE,opacity=1) 23 | def get_right(self): 24 | return self.get_center()[0]+self.radius 25 | def get_left(self): 26 | return self.get_center()[0]-self.radius 27 | def get_top(self): 28 | return self.get_center()[1]+self.radius 29 | def get_down(self): 30 | return self.get_center()[1]-self.radius 31 | 32 | 33 | class OneBallCollision(Scene): 34 | def construct(self) -> None: 35 | screen=Myscreen(height=5) 36 | velocities=[2*RIGHT] 37 | colors=[RED] 38 | position=[RIGHT * 2] 39 | for i,rad in zip(range(1),[0.5]): 40 | ball=Ball(radius=rad).set_color(color=colors[i]) 41 | ball.id=i 42 | ball.velocity=velocities[i] 43 | ball.shift(position[i]) 44 | 45 | 46 | def update(mob,dt): 47 | mob.acceleration=np.array([0,-0.1,0]) 48 | mob.velocity+=mob.acceleration 49 | mob.shift(mob.velocity*dt) 50 | handle_with_box(mob,screen) 51 | 52 | def handle_with_box(bal,scren): 53 | 54 | if bal.get_right()>=scren.get_right() or \ 55 | bal.get_left()<=scren.get_left(): 56 | self.add_sound('hit.mp3',time_offset=-0.3) 57 | bal.velocity[0]=-bal.velocity[0] 58 | 59 | if bal.get_top()>=scren.get_top() or \ 60 | bal.get_down()<=scren.get_down(): 61 | self.add_sound('hit.mp3',time_offset=-0.3) 62 | bal.velocity[1]=-bal.velocity[1] 63 | 64 | # this code is taken from 65 | # https://github.com/nipunramk/Reducible/blob/master/2021/Collision/collision.py 66 | 67 | ball.add_updater(update) 68 | self.add(ball,screen) 69 | self.wait(30) 70 | 71 | class TwoBallCollision(Scene): 72 | def construct(self) -> None: 73 | screen=Myscreen(height=4) 74 | balls=[] 75 | velocities=[RIGHT * 2 + UP * 2, LEFT * 1 + UP * 2] 76 | colors=[RED,YELLOW] 77 | position=[RIGHT * 2, RIGHT * -2] 78 | for i,rad in zip(range(2),[0.3,0.4]): 79 | ball=Ball(radius=rad).set_color(color=colors[i]) 80 | ball.id=i 81 | ball.velocity=velocities[i] 82 | ball.shift(position[i]) 83 | balls.append(ball) 84 | 85 | def update(mob,dt): 86 | mob.shift(mob.velocity*dt) 87 | handle_with_box(mob,screen) 88 | handle_ball_collisions(mob) 89 | 90 | def handle_with_box(bal,scren): 91 | 92 | if bal.get_right()>=scren.get_right() or \ 93 | bal.get_left()<=scren.get_left(): 94 | self.add_sound('hit.mp3', time_offset=-0.3) 95 | bal.velocity[0]=-bal.velocity[0] 96 | if bal.get_top()>=scren.get_top() or \ 97 | bal.get_down()<=scren.get_down(): 98 | self.add_sound('hit.mp3', time_offset=-0.3) 99 | bal.velocity[1]=-bal.velocity[1] 100 | # this code is taken from 101 | # https://github.com/nipunramk/Reducible/blob/master/2021/Collision/collision.py 102 | def handle_ball_collisions(ball): 103 | t_colors = [RED, ORANGE, GREEN_SCREEN, GOLD, PINK, WHITE] 104 | i = 0 105 | for other_ball in balls: 106 | if ball.id != other_ball.id: 107 | dist = np.linalg.norm(ball.get_center() - other_ball.get_center()) 108 | if dist<= (ball.radius + other_ball.radius): 109 | self.add_sound('hit.mp3', time_offset=-0.3) 110 | v1, v2 = get_response_velocities(ball, other_ball) 111 | ball.velocity = v1 112 | other_ball.velocity = v2 113 | 114 | def get_response_velocities(ball, other_ball): 115 | # https://en.wikipedia.org/wiki/Elastic_collision 116 | v1 = ball.velocity 117 | v2 = other_ball.velocity 118 | m1 = ball.mass 119 | m2 = other_ball.mass 120 | x1 = ball.get_center() 121 | x2 = other_ball.get_center() 122 | 123 | ball_response_v = compute_velocity(v1, v2, m1, m2, x1, x2) 124 | other_ball_response_v = compute_velocity(v2, v1, m2, m1, x2, x1) 125 | return ball_response_v, other_ball_response_v 126 | 127 | def compute_velocity(v1, v2, m1, m2, x1, x2): 128 | return v1 - (2 * m2 / (m1 + m2)) * np.dot(v1 - v2, x1 - x2) / np.linalg.norm(x1 - x2) ** 2 * (x1 - x2) 129 | 130 | 131 | 132 | 133 | for ball in balls: 134 | ball.add_updater(update) 135 | self.add(ball) 136 | self.add(screen) 137 | self.wait(40) 138 | 139 | class FourBallCollision(Scene): 140 | def construct(self) -> None: 141 | screen=Myscreen(height=4) 142 | balls=[] 143 | velocities=[RIGHT * 2 + UP * 2, LEFT * 1 + UP * 2, RIGHT * 1 + UP * 1 ,LEFT+DOWN] 144 | colors=[RED,YELLOW,GREEN,PINK] 145 | position=[RIGHT * 2, RIGHT * -2,UP+RIGHT,LEFT+UP] 146 | for i,rad in zip(range(4),[0.3,0.3,0.3,0.3]): 147 | ball=Ball(radius=rad).set_color(color=colors[i]) 148 | ball.id=i 149 | ball.velocity=velocities[i] 150 | ball.shift(position[i]) 151 | balls.append(ball) 152 | 153 | def update(mob,dt): 154 | mob.shift(mob.velocity*dt) 155 | handle_with_box(mob,screen) 156 | handle_ball_collisions(mob) 157 | 158 | def handle_with_box(bal,scren): 159 | 160 | if bal.get_right()>=scren.get_right() or \ 161 | bal.get_left()<=scren.get_left(): 162 | self.add_sound('hit.mp3', time_offset=-0.3) 163 | bal.velocity[0]=-bal.velocity[0] 164 | if bal.get_top()>=scren.get_top() or \ 165 | bal.get_down()<=scren.get_down(): 166 | self.add_sound('hit.mp3', time_offset=-0.3) 167 | bal.velocity[1]=-bal.velocity[1] 168 | # this code is taken from 169 | # https://github.com/nipunramk/Reducible/blob/master/2021/Collision/collision.py 170 | def handle_ball_collisions(ball): 171 | t_colors = [RED, ORANGE, GREEN_SCREEN, GOLD, PINK, WHITE] 172 | i = 0 173 | for other_ball in balls: 174 | if ball.id != other_ball.id: 175 | dist = np.linalg.norm(ball.get_center() - other_ball.get_center()) 176 | if dist<= (ball.radius + other_ball.radius): 177 | self.add_sound('hit.mp3', time_offset=-0.3) 178 | v1, v2 = get_response_velocities(ball, other_ball) 179 | ball.velocity = v1 180 | other_ball.velocity = v2 181 | 182 | def get_response_velocities(ball, other_ball): 183 | # https://en.wikipedia.org/wiki/Elastic_collision 184 | v1 = ball.velocity 185 | v2 = other_ball.velocity 186 | m1 = ball.mass 187 | m2 = other_ball.mass 188 | x1 = ball.get_center() 189 | x2 = other_ball.get_center() 190 | 191 | ball_response_v = compute_velocity(v1, v2, m1, m2, x1, x2) 192 | other_ball_response_v = compute_velocity(v2, v1, m2, m1, x2, x1) 193 | return ball_response_v, other_ball_response_v 194 | 195 | def compute_velocity(v1, v2, m1, m2, x1, x2): 196 | return v1 - (2 * m2 / (m1 + m2)) * np.dot(v1 - v2, x1 - x2) / np.linalg.norm(x1 - x2) ** 2 * (x1 - x2) 197 | 198 | 199 | 200 | 201 | for ball in balls: 202 | ball.add_updater(update) 203 | self.add(ball) 204 | self.add(screen) 205 | self.wait(40) -------------------------------------------------------------------------------- /Collision Simulation/hit.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MathAnimation1198/ManimTutorial/1b708cc5cef09b1ceebfcacab36885840cb0091b/Collision Simulation/hit.mp3 -------------------------------------------------------------------------------- /FourierScene.py: -------------------------------------------------------------------------------- 1 | from manimlib import * 2 | class fourier1(Scene): 3 | CONFIG = { 4 | "vector_config": { 5 | "buff": 0, 6 | "max_tip_length_to_length_ratio": 0.35, 7 | "fill_opacity": 0.75, 8 | } 9 | } 10 | 11 | def construct(self) -> None: 12 | #create frame 13 | frame=self.camera.frame 14 | 15 | # mobject with point 16 | square= Tex('m').set_height(4).family_members_with_points()[0] 17 | coff = self.get_coefficients_of_path(square) 18 | print(self.freq) 19 | 20 | #initial vector 21 | vect=Vector(RIGHT,**self.vector_config).scale(abs(coff[0]),about_point=ORIGIN) 22 | vect.rotate(np.log(coff[0]).imag) 23 | 24 | t=ValueTracker(0) 25 | last=vect 26 | group=VGroup() 27 | def updated1(f,pv,v): 28 | def updat(mob): 29 | mob.set_angle(np.log(v).imag+t.get_value() * TAU * f) 30 | mob.shift(pv.get_end()-mob.get_start()) 31 | return updat 32 | #remaining all vector 33 | for v,f in zip(coff[1:],self.freq[1:]): 34 | vec=Vector(RIGHT,**self.vector_config) 35 | vec.scale(abs(v),about_point=ORIGIN) 36 | vec.rotate(np.log(v).imag) 37 | vec.add_updater(updated1(f,last,v)) 38 | group.add(vec) 39 | last=vec 40 | 41 | 42 | self.add(vect,group) 43 | def updated(f): 44 | def updat(mob): 45 | mob.set_angle(np.log(coff[1]).imag+t.get_value() * TAU * f) 46 | return updat 47 | 48 | 49 | 50 | 51 | dot=Dot(group[-1].get_points()[-1],color=RED).scale(0.001) 52 | dot.move_to(group[-1].get_end()) 53 | 54 | 55 | 56 | dot.add_updater(lambda v: v.move_to(group[-1].get_end())) 57 | 58 | self.add(dot) 59 | 60 | def path_update(dot,path): 61 | 62 | def updater(mob): 63 | line=Line(path.get_end(),dot.get_center()).set_stroke(width=0.01) 64 | path.append_vectorized_mobject(line) 65 | mob.become(path) 66 | 67 | 68 | 69 | return updater 70 | vect.add_updater(updated(self.freq[0])) 71 | 72 | #creating path initial path 73 | path1 = Line(dot.get_center(),dot.get_center()+UP*0.00001,color=YELLOW).set_stroke(width=0.5) 74 | self.add(path1) 75 | #adding updater in path 76 | path1.add_updater(path_update(dot,path1)) 77 | 78 | # for camera scaling 79 | # frame.scale(0.01) 80 | # frame.add_updater(lambda v:v.move_to(dot)) 81 | self.play(ApplyMethod(t.set_value,1),run_time=10,rate_func=linear) 82 | 83 | def get_coefficients_of_path(self, path, n_samples=8000, freqs=None): 84 | freqs=list(range(50,-50,-1)) 85 | print(len(freqs)) 86 | freqs.sort(key=abs) 87 | self.freq=freqs 88 | dt = 1 / n_samples 89 | ts = np.arange(0, 1, dt) 90 | samples = np.array([ 91 | path.point_from_proportion(t) 92 | for t in ts 93 | ]) 94 | samples -= ORIGIN 95 | complex_samples = samples[:, 0] + 1j * samples[:, 1] 96 | 97 | result = [] 98 | for freq in freqs: 99 | riemann_sum = np.array([ 100 | np.exp(-TAU * 1j * freq * t) * cs 101 | for t, cs in zip(ts, complex_samples) 102 | ]).sum() * dt 103 | result.append(riemann_sum) 104 | 105 | return result 106 | -------------------------------------------------------------------------------- /Fourier_Transform_with_manim.py: -------------------------------------------------------------------------------- 1 | from manimlib import * 2 | class Windind(Scene): 3 | def construct(self) -> None: 4 | value = ValueTracker(0.0) 5 | 6 | def func(f=0.1,step=0.001): 7 | out=lambda t:0.5*(2 + 1 * np.cos(TAU * t * 3)+1 * np.cos(TAU * t * 2)) * np.exp(-TAU * 1j * t * f) 8 | dt = step 9 | ran = np.arange(0, 6, dt) 10 | Int = sum([out(i) * dt for i in ran]) 11 | return (1/6)*np.array([Int.real,Int.imag,0]) 12 | 13 | 14 | 15 | def complex_to_real(f): 16 | z=lambda t:0.5*(2+1*np.cos(TAU*t*3)+1*np.cos(TAU*t*2))*np.exp(-TAU*1j*t*f) 17 | return lambda t:np.array([z(t).real,z(t).imag,0]) 18 | curve=ParametricCurve(complex_to_real(value.get_value()),t_range=[0,6,0.01],color=YELLOW) 19 | dot = Dot(func(),color=RED) 20 | dot.add_updater(lambda v:v.become(Dot(func(f=value.get_value()),color=RED))) 21 | 22 | 23 | 24 | axes=Axes(x_range=[0,5,1],y_range=[-1,1,1],width=4,height=3) 25 | axes1=Axes(x_range=[0,5,1],y_range=[0,2,1],width=4,height=1.5).shift(4.5*RIGHT+3*UP) 26 | graph=axes1.get_graph(lambda t:1+np.cos(TAU*t*3),color=BLUE) 27 | axes2 = Axes(x_range=[0, 5, 1], y_range=[0, 2, 1], width=4, height=1.5).next_to(axes1,DOWN,aligned_edge=LEFT,buff=0.5) 28 | graph2 = axes2.get_graph(lambda t: 1 + np.cos(TAU * t * 2), color=YELLOW) 29 | axes3 = Axes(x_range=[0, 5, 1], y_range=[0, 2, 1], width=4, height=1.5).next_to(axes2, DOWN, aligned_edge=LEFT,buff=2) 30 | graph3 = axes3.get_graph(lambda t: 2 + np.cos(TAU * t * 2)+np.cos(TAU*t*3), color=PINK) 31 | axes.shift(4.5*LEFT+2*UP) 32 | axes.add_coordinate_labels() 33 | tex1=TexText('2 beat/per second','3 beat/ per second','mix').scale(0.5) 34 | tex1[0].next_to(graph,LEFT) 35 | tex1[1].next_to(graph2, UP) 36 | tex1[2].next_to(graph3, UP) 37 | 38 | 39 | freq=Tex('Frequency').next_to(axes.x_axis,DOWN,aligned_edge=RIGHT) 40 | yl=Text('x coordinate of center of mass') 41 | yl.scale(0.5,about_point=yl[0].get_start()).next_to(axes.y_axis,RIGHT,aligned_edge=UP) 42 | 43 | 44 | dot1=Dot(axes.c2p(value.get_value(),dot.get_center()[0])) 45 | dot1.add_updater(lambda v:v.move_to(axes.c2p(value.get_value(),dot.get_center()[0]))) 46 | path=Line(dot1.get_center(),dot1.get_center(),color=RED) 47 | def path_update(mob): 48 | path.add_smooth_curve_to(dot1.get_center()) 49 | mob.become(path) 50 | 51 | path.add_updater(path_update) 52 | plane=ComplexPlane(x_range=[-2,2,1],y_range=[-2,2,1]) 53 | 54 | self.add(plane,freq,yl) 55 | self.add(curve,dot,axes,dot1,path,axes1,graph,graph2,axes2,axes3,graph3,tex1) 56 | 57 | 58 | curve.add_updater(lambda v:v.become(ParametricCurve(complex_to_real(value.get_value()),t_range=[0,5,0.001],color=YELLOW))) 59 | self.play(value.set_value,2,run_time=12,rate_func=linear) 60 | self.wait() 61 | self.play(value.set_value, 3, run_time=6, rate_func=linear) 62 | self.wait() 63 | self.play(value.set_value, 5, run_time=12, rate_func=linear) 64 | 65 | 66 | class fwindind(Scene): 67 | def construct(self) -> None: 68 | 69 | def four(t): 70 | if t<-0.5 or t>0.5: 71 | return 0 72 | else: 73 | return 1 74 | 75 | 76 | axes=Axes(x_range=[-5,5,1],y_range=[-2,2,1],height=3,width=8).shift(2*DOWN) 77 | axes1=Axes(x_range=[-5,5,1],y_range=[-2,2,1],height=3,width=8).next_to(axes,UP) 78 | line1=Line(axes1.c2p(-0.5,0),axes1.c2p(-0.5,1)) 79 | line2 = Line(axes1.c2p(0.5, 0), axes1.c2p(0.5, 1)) 80 | line3 = Line(axes1.c2p(-0.5, 1), axes1.c2p(0.5, 1)) 81 | 82 | 83 | tex=Tex('f(\\omega)=\\int_{-\\infty}^{\\infty}g(t)e^{-2\\pi i \\omega t}dt').shift(4*LEFT) 84 | square = VGroup(line1, line2, line3).set_color(YELLOW) 85 | arr = CurvedArrow(axes1.c2p(0, 0),tex.get_center()+UP*0.8 ,color=YELLOW) 86 | arr1 = CurvedArrow(tex.get_center() +5.5*RIGHT * 0.5,axes.get_center()+UP,angle=-TAU/4,color=BLUE) 87 | 88 | 89 | def integral(f,step=0.001): 90 | dt=step 91 | sample=np.arange(-6,6,dt) 92 | z=sum([four(t)*np.exp(-TAU*1j*t*f)*dt for t in sample]) 93 | return z.real+z.imag 94 | 95 | 96 | 97 | curve=axes.get_graph(lambda t:integral(t),color=BLUE) 98 | 99 | self.play(AnimationGroup(*[ShowCreation(i) for i in [axes1,square,arr,tex,arr1,axes,curve]],lag_ratio=1)) 100 | 101 | -------------------------------------------------------------------------------- /Fourier_epycycles.py: -------------------------------------------------------------------------------- 1 | from manimlib import * 2 | class Epicycle(Scene): 3 | def construct(self) -> None: 4 | m1=self.first(mu=2.2) 5 | m2=self.first(ran=range(3,20,2),mu=0) 6 | m3 = self.first(ran=range(3, 250, 2), mu=-2.2) 7 | label=Tex(f"n={len(range(3,10,2))+1}",f"n={len(range(3,20,2))+1}",f"n={len(range(3,250,2))+1}") 8 | label[0].next_to(m1,LEFT) 9 | label[1].next_to(m2, LEFT) 10 | label[2].next_to(m3, LEFT) 11 | self.add(m1,m2,m3,label) 12 | self.wait(20) 13 | 14 | def first(self,ran=range(3,10,2),mu=2): 15 | vect = Vector(color=GREY_A) 16 | value = ValueTracker(0) 17 | rate = 0.24 18 | value.add_updater(lambda v, dt: v.set_value(dt * rate)) 19 | self.add(value) 20 | vect.add_updater(lambda v: v.set_angle(v.get_angle() + 1 * TAU * value.get_value())) 21 | 22 | # vect2=Vector().shift(vect.get_end()).scale(1/3) 23 | def update(pv, f): 24 | 25 | def updat(mob): 26 | mob.set_angle(mob.get_angle() + f * value.get_value() * TAU, about_point=pv.get_end()) 27 | mob.shift(pv.get_end() - mob.get_start()) 28 | 29 | return updat 30 | 31 | last = vect 32 | vgroup = VGroup() 33 | rang = ran 34 | for i in rang: 35 | vect2 = Vector().scale(1 / i) 36 | vect2.add_updater(update(last, i)) 37 | vgroup.add(vect2) 38 | last = vect2 39 | 40 | line = Line(vgroup[-1].get_end(), vgroup[-1].get_end() + np.array([1 - vgroup[-1].get_end()[0] + 1, 0, 0])) 41 | line.add_updater( 42 | lambda v: v.become( 43 | Line(vgroup[-1].get_end(), vgroup[-1].get_end() + np.array([1 - vgroup[-1].get_end()[0] + 1, 0, 0])))) 44 | 45 | path = Line(vgroup[-1].get_end(), vgroup[-1].get_end() + UP * 0.00001).set_stroke(width=0.5) 46 | 47 | circ3 = Circle() 48 | 49 | def cupdat(v): 50 | def upd(mob): 51 | mob.move_to(v.move_to(v.get_start())) 52 | 53 | return upd 54 | 55 | for i, j, c in zip(range(len(rang)), rang, 56 | np.random.choice([YELLOW, BLUE, GREEN, PINK, PURPLE, MAROON, GOLD, TEAL], size=len(rang))): 57 | circ = Circle(radius=1 / j, color=c).set_stroke(width=2) 58 | 59 | circ.add_updater(cupdat(vgroup[i])) 60 | self.add(circ) 61 | 62 | def path_update(mob): 63 | line1 = Line(path.get_end(), line.get_end()) 64 | path.append_vectorized_mobject(line1) 65 | path.shift(RIGHT * value.get_value() * 3) 66 | mob.become(path) 67 | 68 | path.add_updater(path_update) 69 | 70 | # frame=self.camera.frame 71 | # frame.scale(0.5) 72 | # frame.add_updater(lambda v:v.move_to(vgroup[1])) 73 | 74 | # dot=Dot(vgroup[-1].get_end(),color=RED) 75 | # dot.add_updater(lambda v:v.move_to(vgroup[-1].get_end())) 76 | fgroup = VGroup(vect, vgroup, line, path, circ3) 77 | fgroup.shift( mu*UP) 78 | return fgroup 79 | 80 | class Sin(Scene): 81 | def construct(self) -> None: 82 | circle=Circle().shift(5*LEFT) 83 | vect=Vector().shift(5*LEFT) 84 | t=ValueTracker(0) 85 | rate=1 86 | t.add_updater(lambda v,dt:v.increment_value(dt*rate)) 87 | vect.add_updater(lambda v: v.set_angle(t.get_value())) 88 | self.add(t,circle,vect) 89 | 90 | 91 | line=Line(vect.get_end(),vect.get_end()+np.array([1-vect.get_end()[0]-4,0,0])) 92 | line.add_updater(lambda v:v.become(Line(vect.get_end(),vect.get_end()+np.array([1-vect.get_end()[0]-4,0,0])))) 93 | 94 | 95 | 96 | axes=NumberLine(x_range=[0,TAU+1,1]) 97 | axes.next_to(line,RIGHT,buff=0) 98 | 99 | 100 | self.add(line) 101 | path=VMobject(color=YELLOW) 102 | path.set_points_as_corners([line.get_end(),line.get_end()+UP*0.0001]) 103 | 104 | 105 | 106 | def path_upadate(mob,dt): 107 | line1st=Line(path.get_end(),line.get_end()) 108 | path.append_vectorized_mobject(line1st) 109 | path.shift(RIGHT*dt*0.5) 110 | 111 | path.add_updater(path_upadate) 112 | 113 | self.add(path) 114 | 115 | 116 | self.wait(20) 117 | 118 | class SinCos(Scene): 119 | def construct(self) -> None: 120 | circle=Circle().shift(5*LEFT+2*UP) 121 | vect=Vector().shift(5*LEFT+2*UP) 122 | t=ValueTracker(0) 123 | rate=2 124 | t.add_updater(lambda v,dt:v.increment_value(dt*rate)) 125 | vect.add_updater(lambda v: v.set_angle(t.get_value())) 126 | self.add(t,circle,vect) 127 | 128 | 129 | line=Line(vect.get_end(),vect.get_end()+np.array([1-vect.get_end()[0]-4,0,0])) 130 | line.add_updater(lambda v:v.become(Line(vect.get_end(),vect.get_end()+np.array([1-vect.get_end()[0]-4,0,0])))) 131 | 132 | line1 = Line(vect.get_end(), vect.get_end() + np.array([0,1 - vect.get_end()[1] - 1, 0])) 133 | line1.add_updater( 134 | lambda v: v.become(Line(vect.get_end(), vect.get_end() + np.array([0,1 - vect.get_end()[1] - 1, 0])))) 135 | 136 | axes=NumberLine(x_range=[0,TAU+1,1]) 137 | axes.next_to(line,RIGHT,buff=0) 138 | axes1 = NumberLine(y_range=[0, TAU, 1]).rotate(90*DEGREES) 139 | axes1.next_to(line1, DOWN, buff=0) 140 | axes1.shift(1*LEFT) 141 | 142 | self.add(line,line1,axes,axes1) 143 | path=VMobject(color=YELLOW) 144 | path.set_points_as_corners([line.get_end(),line.get_end()+UP*0.0001]) 145 | 146 | path1 = VMobject(color=YELLOW) 147 | path1.set_points_as_corners([line1.get_end(), line1.get_end() + UP * 0.0001]) 148 | 149 | def path_upadate(mob,dt): 150 | line1st=Line(path.get_end(),line.get_end()) 151 | path.append_vectorized_mobject(line1st) 152 | path.shift(RIGHT*dt*rate) 153 | def path_upadate1(mob,dt): 154 | line2nd=Line(path1.get_end(),line1.get_end()) 155 | path1.append_vectorized_mobject(line2nd) 156 | path1.shift(DOWN*dt*rate) 157 | path.add_updater(path_upadate) 158 | path1.add_updater(path_upadate1) 159 | self.add(path,path1) 160 | 161 | 162 | self.wait(10) 163 | class cos2(Scene): 164 | def construct(self) -> None: 165 | vect=Vector(RIGHT) 166 | circ=Circle() 167 | t=ValueTracker(0) 168 | rate=0.5 169 | t.add_updater(lambda m,dt:m.increment_value( 170 | rate * dt 171 | )) 172 | self.add(vect,circ,t) 173 | vect.add_updater(lambda v:v.become(Vector(((np.exp(1*TAU*t.get_value()*1j) 174 | +np.exp(1*TAU*t.get_value()*1j))*1).real*RIGHT+UP*((np.exp(1*TAU*t.get_value()*1j) 175 | +np.exp(-1*TAU*t.get_value()*1j))*1).imag))) 176 | 177 | 178 | def get_vertically_falling_tracing(vector, color, stroke_width=3, rate=rate): 179 | path = VMobject() 180 | path.set_stroke(color, stroke_width) 181 | path.set_points_as_corners([vector.get_end(),vector.get_end()+UP*0.0001]) 182 | path.vector1 = vector 183 | def update_path(mob,dt): 184 | 185 | path.append_vectorized_mobject(Line(path.get_end(),vector.get_end())) 186 | path.shift(rate*dt* DOWN) 187 | mob.become(path) 188 | 189 | 190 | 191 | path.add_updater(update_path) 192 | return path 193 | path1=get_vertically_falling_tracing(vect,YELLOW) 194 | self.add(path1) 195 | self.wait(10) 196 | -------------------------------------------------------------------------------- /Gradient and Tangent vector lemma.py: -------------------------------------------------------------------------------- 1 | from manimlib import * 2 | def length_func(point): 3 | out=point 4 | norm=get_norm(out) 5 | func=lambda nor:0.45*sigmoid(nor) 6 | out*=func(norm)/norm 7 | return out 8 | 9 | 10 | class first(ThreeDScene): 11 | def construct(self) -> None: 12 | frame = self.camera.frame 13 | frame.set_euler_angles(theta=40 * DEGREES, phi=60 * DEGREES) 14 | surface=ParametricSurface(lambda x,y:np.array([x,y,x**2+y**2]),u_range=(-1.8,1.8),v_range=(-1.8,1.8),color=BLUE) 15 | surfaceplane=ParametricSurface(lambda x,y:np.array([x,y,1]),u_range=(-1.8,1.8),v_range=(-1.8,1.8),color=RED,opacity=0.4) 16 | axes=ThreeDAxes() 17 | 18 | 19 | curve=ParametricCurve(lambda t:np.array([np.sqrt(0.75)*np.cos(t),np.sqrt(0.75)*np.sin(t),0.75]),t_range=[0,TAU],color=PINK) 20 | curve1 = ParametricCurve(lambda t: np.array([0.5* np.cos(t), 0.5 * np.sin(t), (0.5)**2]), t_range=[0, TAU], color=GREEN) 21 | curve2 = ParametricCurve(lambda t: np.array([ 1* np.cos(t), 1 * np.sin(t), 1]), t_range=[0, TAU], 22 | color=YELLOW) 23 | 24 | # mes=SurfaceMesh(surface) 25 | func = Tex('f(x,y)=x^{2}+y^{2}').fix_in_frame().shift(4 * LEFT + 3 * UP) 26 | cost = Tex('f(x,y)=x^{2}+y^{2}=c=f^{-1}(c)').fix_in_frame().next_to(func, DOWN, aligned_edge=LEFT) 27 | grad=Tex("\\nabla f(x,y)=").fix_in_frame().next_to(cost, DOWN, aligned_edge=LEFT,buff=1) 28 | mat=matrix_to_mobject(['2x','2y']).fix_in_frame().next_to(grad, RIGHT) 29 | self.add(surface,axes,func) 30 | self.play(ShowCreation(surfaceplane),run_time=5) 31 | self.play(ShowCreation(curve2)) 32 | 33 | 34 | 35 | cylinder=ParametricSurface(lambda u,v:np.array([1*np.cos(u),1*np.sin(u),v]),u_range=(0,TAU),v_range=(0,1),opacity=1,color=PINK) 36 | self.play(ShowCreation(cylinder),ShowCreation(cost)) 37 | self.play(frame.increment_phi,40*DEGREES,rate_func=there_and_back,run_time=3) 38 | 39 | self.play(frame.increment_theta,180*DEGREES,run_time=3) 40 | 41 | 42 | 43 | 44 | 45 | 46 | self.play(ApplyPointwiseFunction(lambda p:np.array([p[0],p[1],0]),surface), 47 | 48 | ApplyPointwiseFunction(lambda p:np.array([p[0],p[1],0]),curve2), 49 | ApplyPointwiseFunction(lambda p:np.array([p[0],p[1],0]),cylinder), 50 | FadeOut(surfaceplane)) 51 | 52 | 53 | 54 | 55 | 56 | plane=NumberPlane(x_range=[-8,8,2],y_range=[-4,4,2]) 57 | 58 | realf = VectorField(lambda x, y: np.array([2 * x, 2 * y, 0]), plane) 59 | self.play(AnimationGroup(*[ShowCreation(i) for i in [realf,grad,mat]], lag_ratio=0.2), AnimationGroup(*[FadeOut(i) for i in [surface]], lag_ratio=0.2)) 60 | 61 | self.wait() 62 | self.play(ApplyMethod(frame.set_euler_angles, 0, 0), rate_func=linear, run_time=2) 63 | 64 | for i in np.linspace(0,TAU,9): 65 | 66 | vect=Vector(np.array([-1 * np.sin(i), 1 * np.cos(i),0]),color=BLUE).shift(np.array([1 * np.cos(i), 1 * np.sin(i),0])) 67 | vect.scale(0.6,about_point=vect.get_start()) 68 | 69 | self.play(ShowCreation(vect)) 70 | self.wait() 71 | self.play(AnimationGroup(*[FadeOut(i) for i in [func,cost,grad,mat]], lag_ratio=0.2)) 72 | 73 | 74 | 75 | 76 | 77 | class Texfun(Scene): 78 | def construct(self) -> None: 79 | func = Tex('f(x,y)=x^{2}+y^{2}').shift(4 * LEFT + 3 * UP) 80 | cost = Tex('f(x,y)=x^{2}+y^{2}=c=f^{-1}(c)').next_to(func, DOWN, aligned_edge=LEFT) 81 | 82 | param = Tex('\\alpha(t)=r(\\cos(t),\\sin(t))').next_to(cost, DOWN, aligned_edge=LEFT) 83 | param1 = Tex('f(\\alpha(t))=r^{2}(\\cos(t))^{2}+r^{2}(\\sin(t))^{2}=r^{2}').next_to(param, DOWN, 84 | aligned_edge=LEFT) 85 | deri = Tex("\\frac{df(\\alpha(t))}{dt}=0").next_to(param1, DOWN, aligned_edge=LEFT) 86 | grad = Tex("\\nabla f(\\alpha(t)). \\dot\\alpha(t)=0").next_to(deri, DOWN, aligned_edge=LEFT) 87 | 88 | example=func.copy().next_to(grad, DOWN, aligned_edge=LEFT) 89 | param2=Tex('\\alpha(t)=(t,t^{2})').next_to(example, DOWN, aligned_edge=LEFT) 90 | 91 | comp=Tex('f(\\alpha(t)))=f((t,t^{2}))=t^{2}+(t^{2})^{2}').next_to(param2, DOWN, aligned_edge=LEFT) 92 | 93 | dcomp=Tex('\\frac{df(\\alpha(t)))}{dt}=\\frac{f((t,t^{2}))}{dt}=2t+4t^{3}').next_to(func, RIGHT) 94 | 95 | gf=matrix_to_mobject(["2t",'2t^{2}']).next_to(dcomp, DOWN) 96 | 97 | galpha=matrix_to_mobject(['1','2t']).next_to(gf, RIGHT,buff=0.1) 98 | 99 | final=Tex('=2t+4t^{3}').next_to(galpha, RIGHT,buff=0.1) 100 | 101 | 102 | 103 | 104 | 105 | self.play(AnimationGroup(*[ShowCreation(i) for i in [func, cost,param, param1, deri, grad]], 106 | lag_ratio=2)) 107 | 108 | class Texfunc2(Scene): 109 | def construct(self) -> None: 110 | example = Tex('f(x,y)=x^{2}+y^{2}').shift(4 * LEFT + 3 * UP) 111 | param2 = Tex('\\alpha(t)=(t,t^{2})').next_to(example, DOWN, aligned_edge=LEFT) 112 | 113 | comp = Tex('f(\\alpha(t)))=f((t,t^{2}))=t^{2}+(t^{2})^{2}').next_to(param2, DOWN, aligned_edge=LEFT) 114 | 115 | dcomp = Tex('\\frac{df(\\alpha(t)))}{dt}=\\frac{f((t,t^{2}))}{dt}=2t+4t^{3}').next_to(comp, DOWN) 116 | 117 | grad = Tex("\\nabla f(\\alpha(t)). \\dot\\alpha(t)=").next_to(dcomp, DOWN,aligned_edge=LEFT,buff=1) 118 | gf = matrix_to_mobject(["2t", '2t^{2}']).next_to(grad,RIGHT) 119 | 120 | galpha = matrix_to_mobject(['1', '2t']).next_to(gf, RIGHT, buff=0.2) 121 | 122 | final = Tex('=2t+4t^{3}').next_to(galpha, RIGHT, buff=0.1) 123 | 124 | 125 | self.play(AnimationGroup(*[ShowCreation(i) for i in [example, param2, comp, dcomp, grad,gf,galpha, final]],lag_ratio=2)) 126 | class helperDiff(Scene): 127 | def construct(self) -> None: 128 | tex=Text("The gradient of f at p").shift(3*LEFT) 129 | tex1=Tex("\\in f^{-1}(c)","f^{-1}(c)") 130 | tex1[0].next_to(tex, RIGHT) 131 | tex2=Text("is orthogonal to all vectors tangent to ").next_to(tex,DOWN,aligned_edge=LEFT) 132 | tex1[1].next_to(tex2,DOWN,aligned_edge=LEFT) 133 | 134 | text3=Text("at p").next_to(tex1[1],RIGHT) 135 | 136 | 137 | self.play(AnimationGroup(*[ShowCreation(i) for i in [tex,tex1[0],tex2,text3,tex1[1]]],lag_ratio=0.5)) 138 | 139 | -------------------------------------------------------------------------------- /GradientApproximation.py: -------------------------------------------------------------------------------- 1 | from manim import * 2 | class gradientApproximation(MovingCameraScene): 3 | def construct(self): 4 | self.camera.frame.move_to(ORIGIN) 5 | self.camera.frame.scale(2) 6 | # boy=Boys().shift([-5,-1,0]).rotate(80*DEGREES,axis=UP) 7 | tittle=Tex("Gradient Descent Algorithm Helping Me").shift(6*UP) 8 | text=Tex("To Find Best Fit Approximated Curve").next_to(tittle,DOWN) 9 | 10 | # image=SVGMobject("Cartoon_thought_bubble.svg").next_to(boy,UP,buff=0,aligned_edge=LEFT) 11 | # hello = Text('Wow',color=RED).move_to(image) 12 | self.add(tittle,text) 13 | def func(x, a, b, c, d): 14 | return a * x ** 3 + b * x ** 2 + c * x + d 15 | 16 | def app(x): 17 | return np.sin(x) 18 | 19 | def grad(x, a, b, c, d): 20 | return [2 * x ** 3 * (func(x, a, b, c, d) - app(x)), 2 * x ** 2 * (func(x, a, b, c, d) - app(x)), 21 | 2 * x * (func(x, a, b, c, d) - app(x)), 2 * (func(x, a, b, c, d) - app(x))] 22 | 23 | axes = Axes(x_length=8,y_length=6).shift(DOWN) 24 | graph = axes.plot(lambda x: np.sin(x),color=YELLOW) 25 | a, b, c, d = 1, 1, 1, 1 26 | x = np.arange(-1.5, 1.5, 0.01) 27 | graph1=axes.plot(lambda x:a*x**3+b*x**2+c*x+d) 28 | text=MathTex(*[str(round(a,2)),"x^{3}+",str(round(b,2)),"x^{2}+",str(round(c,2)),"x+",str(round(d,2))]).shift(np.array([0,4,0])) 29 | self.add(axes, graph,text) 30 | self.wait() 31 | rate=0.8 32 | graphG=VGroup() 33 | textG=VGroup() 34 | for i in range(20): 35 | a = a - (rate* np.mean(grad(x, a, b, c, d)[0])) 36 | b = b - (rate* np.mean(grad(x, a, b, c, d)[1])) 37 | c = c - (rate* np.mean(grad(x, a, b, c, d)[2])) 38 | d = d - (rate* np.mean(grad(x, a, b, c, d)[3])) 39 | 40 | graph2 = axes.plot(lambda x: a * x ** 3 + b * x ** 2 + c * x + d,color=RED) 41 | text1 = MathTex( 42 | *[str(round(a, 2)), "x^{3}+", str(round(b, 2)), "x^{2}+", str(round(c, 2)), "x+", str(round(d, 2))],).shift(np.array([0, 4, 0])) 43 | graphG.add(graph2) 44 | textG.add(text1) 45 | for tex,gra in zip(textG[:10],graphG[:10]): 46 | self.play(Transform(text,tex),Transform(graph1,gra)) 47 | code=''' 48 | a, b, c, d = 1, 1, 1, 1 49 | x = np.arange(-1.5, 1.5, 0.01) 50 | def func(x, a, b, c, d): 51 | return a * x ** 3 + b * x ** 2 + c * x + d 52 | 53 | def app(x): 54 | return np.sin(x) 55 | 56 | def grad(x, a, b, c, d): 57 | return [2 * x ** 3 * (func(x, a, b, c, d) - app(x)), 58 | 2 * x ** 2 * (func(x, a, b, c, d) - app(x)), 59 | 2 * x * (func(x, a, b, c, d) - app(x)), 60 | 2 * (func(x, a, b, c, d) - app(x))] 61 | 62 | for i in range(20): 63 | a = a - (rate* np.mean(grad(x, a, b, c, d)[0])) 64 | b = b - (rate* np.mean(grad(x, a, b, c, d)[1])) 65 | c = c - (rate* np.mean(grad(x, a, b, c, d)[2])) 66 | d = d - (rate* np.mean(grad(x, a, b, c, d)[3])) 67 | ''' 68 | cod=Code(code=code,background="window",language="python",font="Monospace").stretch_to_fit_height(self.camera.frame.get_height()*0.8).\ 69 | stretch_to_fit_width(self.camera.frame.get_width()) 70 | coff=Tex("Code I Am Using To Find Cofficient").next_to(cod,UP,0.1) 71 | self.play(Write(coff)) 72 | self.play(Write(cod)) 73 | self.wait(2) 74 | self.play(FadeOut(cod),FadeOut(coff)) 75 | full_code=Tex("Full Manim Code Given In Comment").shift(3*DOWN) 76 | self.play(Write(full_code)) 77 | self.wait(2) -------------------------------------------------------------------------------- /Integral Curve of Vector Field.py: -------------------------------------------------------------------------------- 1 | from manimlib import * 2 | class Integral(Scene): 3 | def construct(self) -> None: 4 | x = lambda t: 1* np.cos(t) 5 | y = lambda t: 1 * np.sin(t) 6 | dx = lambda t: -1 * np.sin(t) 7 | dy = lambda t: 1 * np.cos(t) 8 | plane=NumberPlane() 9 | field=VectorField(lambda x,y:np.array([-y,x,0]),plane) 10 | strem=StreamLines(lambda x,y:np.array([-y,x,0]),plane) 11 | self.add(field) 12 | astrem=AnimatedStreamLines(strem).set_stroke(color=BLUE) 13 | self.add(astrem) 14 | self.wait(2) 15 | circle=Circle(color=PINK).set_stroke(width=2) 16 | self.play(ShowCreation(circle)) 17 | self.remove(astrem) 18 | self.play(circle.scale,5,run_time=5,rate_func=there_and_back) 19 | 20 | for i in [*np.arange(0, TAU, TAU / 4), TAU / 8]: 21 | t = i 22 | inp = x(t), y(t) 23 | out = dx(t), dy(t) 24 | vect = Vector(np.array([*out, 0]), color=BLUE).shift(np.array([*inp, 0])) 25 | vect.scale(0.4,about_point=vect.get_start()) 26 | self.play(ShowCreation(vect)) 27 | 28 | class Drawing(Scene): 29 | def construct(self) -> None: 30 | func=lambda x,y:np.array([-y,x,0]) 31 | plane=NumberPlane() 32 | self.add(plane) 33 | vectors=VGroup() 34 | for i in [[1,0],[0,1],[-1,0],[0,-1]]: 35 | out=func(*i) 36 | vect=Vector(out,color=YELLOW) 37 | vect.shift(np.array([*i,0])) 38 | self.play(ShowCreation(vect)) 39 | vect1=VMobject() 40 | fun=Tex('f(x,y)=').next_to(7*LEFT+3*UP) 41 | mat=matrix_to_mobject(['-y','x']).next_to(fun,RIGHT) 42 | equal=Tex('=').next_to(mat,RIGHT) 43 | self.add(mat,fun,equal) 44 | vect2=VMobject() 45 | vect3=VMobject() 46 | vect4=VMobject() 47 | vect5 = VMobject() 48 | for x,y in zip(random.choices(np.arange(-3,3,0.1),k=10),random.choices(np.arange(-3,3,0.1),k=10)): 49 | dot=Dot(np.array([x,y,0]),color=RED) 50 | out = func(x,y) 51 | vect = Vector(out, color=YELLOW) 52 | vect.shift(np.array([x,y, 0])) 53 | val=Tex(f"({round(x,2)},{round(y,2)})").next_to(dot,DOWN).scale(0.5) 54 | tfunc=Tex(f"f({round(x,2)},{round(y,2)})=").scale(0.5).move_to(fun) 55 | 56 | mat1 = matrix_to_mobject(['-y', 'x']).scale(0.5).next_to(tfunc, RIGHT) 57 | equal1=Tex('=').next_to(mat1,RIGHT) 58 | tout = matrix_to_mobject([f"{round(out[0], 2)}", f"{round(out[1], 2)}"]).scale(0.5).next_to(equal1, RIGHT) 59 | line1=DashedLine(dot.get_center(),dot.get_center()+np.array([out[0],0,0])) 60 | line2 = DashedLine(dot.get_center()+np.array([out[0],0,0]),dot.get_center()+np.array([out[0],0,0])+np.array([0,out[1],0])) 61 | label=Tex(f"{round(out[0],2)}",f"{round(out[1],2)}").scale(0.4) 62 | label[0].next_to(line1,DOWN) 63 | label[1].next_to(line2, RIGHT) 64 | group1=VGroup(line1,line2) 65 | group=VGroup(dot,vect) 66 | self.play(Transform(vect1,group),Transform(fun,tfunc),Transform(vect2,tout),Transform(mat,mat1),Transform(equal,equal1),Transform(vect3,group1) 67 | ,Transform(vect4,label),Transform(vect5,val)) 68 | 69 | class Parametric(Scene): 70 | def construct(self) -> None: 71 | x=lambda t:2*np.cos(t) 72 | y = lambda t: 2*np.sin(t) 73 | dx=lambda t:-2*np.sin(t) 74 | dy = lambda t: 2*np.cos(t) 75 | xl=Tex('x(t)=2\\cos(t)').shift(5*LEFT+3*UP) 76 | yl =Tex('y(t)=2\\sin(t)').next_to(xl,DOWN,aligned_edge=LEFT) 77 | xld=Tex('x(t)^{\\prime}=-2\\sin(t)').next_to(yl,DOWN,aligned_edge=LEFT,buff=2) 78 | yld = Tex('y(t)^{\\prime}=2\\cos(t)').next_to(xld,DOWN,aligned_edge=LEFT) 79 | 80 | 81 | curve=ParametricCurve(lambda t:np.array([2*np.cos(t),2*np.sin(t),0]),t_range=[0,TAU,0.1],color=YELLOW) 82 | tl = Tex('t=',color=GREEN) 83 | val=ValueTracker(0) 84 | tval=DecimalNumber(val.get_value()).scale(0.6).next_to(tl,RIGHT) 85 | self.play(AnimationGroup(*[ShowCreation(i) for i in [xl,yl]],lag_ratio=2)) 86 | self.add(tl,tval) 87 | tval.add_updater(lambda v:v.set_value(val.get_value())) 88 | 89 | tl.add_updater(lambda v:v.next_to(curve.get_end(),RIGHT,buff=0)) 90 | tval.add_updater(lambda v: v.next_to(tl, RIGHT)) 91 | self.add(curve) 92 | self.play(ApplyMethod(val.set_value,TAU),ShowCreation(curve),run_time=2,rate_func=linear) 93 | 94 | ax1 = NumberLine(x_range=[-2.0, 2.0, 1.0]) 95 | ax2 = NumberLine(x_range=[-2.0, 2.0, 1.0]).rotate(PI / 2, about_point=ORIGIN) 96 | self.add(ax1,ax2) 97 | 98 | 99 | self.play(AnimationGroup(*[ShowCreation(i) for i in [xld,yld]],lag_ratio=1)) 100 | 101 | for i in [*np.arange(0,TAU,TAU/4),TAU/8]: 102 | t = i 103 | inp = x(t), y(t) 104 | out = dx(t), dy(t) 105 | vect = Vector(np.array([*out, 0]), color=RED).shift(np.array([*inp, 0])) 106 | self.play(ShowCreation(vect)) 107 | class FinalCurve(Scene): 108 | def construct(self) -> None: 109 | tex=Tex('x=x_{1}(t)','y=x_{2}(t)').shift(4*LEFT+3*UP) 110 | tex[1].next_to(tex[0],DOWN) 111 | tex1=Tex('\\frac{dx}{dt}=-y','\\frac{dy}{dt}=x') 112 | 113 | tex1[0].next_to(tex, DOWN) 114 | tex1[1].next_to(tex1[0], DOWN) 115 | dou = Tex('\\frac{d^{2}x}{dt^{2}}=-','\\frac{dy}{dt}') 116 | self.play(AnimationGroup(*[ShowCreation(i) for i in [tex, tex1]], lag_ratio=1)) 117 | point=VectorizedPoint(tex1[1].get_center()) 118 | point1=VectorizedPoint(tex1[0].get_center()) 119 | self.play(ApplyMethod(tex1[0].move_to,point),ApplyMethod(tex1[1].move_to,point1)) 120 | dou.next_to(tex1[0],RIGHT,buff=2) 121 | self.play(ShowCreation(dou)) 122 | cop=tex1[1][-1].copy().move_to(dou[1]) 123 | self.play(Transform(dou[1],cop)) 124 | ex=Tex('x=\\cos(t)','\\frac{dx}{dt}=-\\sin(t)','\\frac{d^{2}x}{dt^{2}}=-\\cos(t)=-x',color=YELLOW).next_to(dou,DOWN,aligned_edge=LEFT,buff=1) 125 | 126 | self.add(ex[0]) 127 | for i in range(1,3): 128 | ex[i].move_to(ex[i-1]) 129 | for i in range(1,3): 130 | self.play(Transform(ex[0],ex[i])) 131 | self.remove(ex) 132 | ex1 = Tex('x=\\sin(t)', '\\frac{dx}{dt}=\\cos(t)', '\\frac{d^{2}x}{dt^{2}}=-\\sin(t)=-x', color=YELLOW).next_to(dou, 133 | DOWN, 134 | aligned_edge=LEFT, 135 | buff=1) 136 | 137 | self.add(ex1[0]) 138 | for i in range(1, 3): 139 | ex1[i].move_to(ex1[i - 1]) 140 | for i in range(1, 3): 141 | self.play(Transform(ex1[0], ex1[i])) 142 | 143 | self.remove(ex1) 144 | ex2 = Tex('x=\\cos(t)+\\sin(t)', '\\frac{dx}{dt}=-\\sin(t)+\\cos(t)', '\\frac{d^{2}x}{dt^{2}}=-\\cos(t)-\\sin(t)=-x', color=YELLOW).next_to( 145 | dou, 146 | DOWN, 147 | aligned_edge=LEFT, 148 | buff=1) 149 | 150 | self.add(ex2[0]) 151 | for i in range(1, 3): 152 | ex2[i].move_to(ex2[i - 1]) 153 | for i in range(1, 3): 154 | self.play(Transform(ex2[0], ex2[i])) 155 | self.remove(ex2) 156 | sol=Tex('x=','A\\cos(t)+B\\sin(t)',color=RED).next_to( 157 | dou, 158 | DOWN, 159 | aligned_edge=LEFT, 160 | buff=1) 161 | self.add(sol) 162 | solc=sol[1].copy().move_to(tex1[1][-1],aligned_edge=LEFT) 163 | self.play(Transform(tex1[1][-1],solc)) 164 | self.play(tex1[1][3:5].next_to,sol[1].copy().move_to(tex1[1][-1]),RIGHT) 165 | self.remove(tex1[1][2]) 166 | sol1=Tex('y=A\\sin(t)-B\\cos(t)',color=RED).move_to(tex1[1],aligned_edge=LEFT) 167 | solg=VGroup(tex1[1],solc) 168 | self.play(Transform(solg,sol1)) 169 | self.play(solg.next_to,sol,DOWN,aligned_edge=LEFT) 170 | self.remove(dou) 171 | self.play(ApplyMethod(sol.shift,5*UP),ApplyMethod(solg.shift,5*UP)) 172 | initial=Tex('(x(0),y(0))=(1,0)').next_to(solg,DOWN,aligned_edge=LEFT) 173 | appl=Tex('1=A\\cos(0)+B\\sin(0)','0=A\\sin(0)-B\\cos(0)') 174 | appl[0].next_to(initial,DOWN,aligned_edge=LEFT) 175 | appl[1].next_to(appl[0], DOWN, aligned_edge=LEFT) 176 | 177 | ulfinsol=Tex('x=\\cos(t)','y=\\sin(t)') 178 | ulfinsol[0].next_to(appl[1], DOWN, aligned_edge=LEFT) 179 | ulfinsol[1].next_to(ulfinsol[0], DOWN, aligned_edge=LEFT) 180 | 181 | for i in [initial,appl,ulfinsol]: 182 | self.play(ShowCreation(i)) 183 | 184 | 185 | -------------------------------------------------------------------------------- /Projectile Motion/Projectile_cairo_backened.py: -------------------------------------------------------------------------------- 1 | from manimlib.imports import * 2 | 3 | #use this command to run this file in cairo backened 4 | #python -m manim Projectile_cairo_backened.py -l -r "670,1024" --video_dir F:/ 5 | 6 | class projectile(ThreeDScene): 7 | CONFIG = { 8 | "camera_config": { 9 | "background_image": "grass.jpg", 10 | } 11 | } 12 | 13 | def construct(self) -> None: 14 | self.set_camera_orientation(phi=45 * DEGREES, theta=-45 * DEGREES) 15 | self.first(7,45*DEGREES) 16 | 17 | self.play(self.camera.theta_tracker.increment_value, -20 * DEGREES) 18 | 19 | self.play(self.camera.phi_tracker.increment_value, 20 * DEGREES) 20 | self.first(7, 60 * DEGREES) 21 | self.first(7, 75 * DEGREES) 22 | self.first(7, 30 * DEGREES) 23 | 24 | 25 | def first(self,u,theta): 26 | axes = ThreeDAxes(x_range=[0, 5, 1], y_range=[0, 5, 1], width=3, height=4) 27 | self.add(axes) 28 | total = 2 * u * np.sin(theta) / 9.8 29 | ball = Sphere(radius=0.1 ).move_to(axes.c2p(0, 0)) 30 | # curve = ParametricCurve( 31 | # lambda t: axes.c2p(t*u*np.cos(theta),t*u*np.sin(theta)-0.5*9.8*t**2), 32 | # t_range=(0, total)) 33 | path = Line(ball.get_center(), ball.get_center() + UP * 0.0001) 34 | 35 | def update_path(mob): 36 | line = Line(path.get_end(), ball.get_center()) 37 | path.append_vectorized_mobject(line) 38 | mob.become(path) 39 | 40 | def update(mob, alpha): 41 | intial_t = interpolate(0, total, alpha) 42 | inarr = axes.c2p(intial_t * u * np.cos(theta), intial_t * u * np.sin(theta) - 0.5 * 9.8 * intial_t ** 2) 43 | finarr = np.array([inarr[0], 0, inarr[1]]) 44 | mob.move_to(finarr) 45 | 46 | self.add(path) 47 | path.add_updater(update_path) 48 | self.play(UpdateFromAlphaFunc(ball, update), run_time=4) 49 | 50 | 51 | -------------------------------------------------------------------------------- /Projectile Motion/Projectile_manimgl.py: -------------------------------------------------------------------------------- 1 | from manimlib import * 2 | class projectile(Scene): 3 | 4 | def construct(self) -> None: 5 | frame = self.camera.frame 6 | frame.scale(0.6) 7 | frame.set_euler_angles(theta=45 * DEGREES, phi=-45 * DEGREES) 8 | for index,angle in enumerate([45,65,85,30]): 9 | self.launch_at_angle(7,angle) 10 | if index==0: 11 | self.play(frame.increment_theta, -20 * DEGREES) 12 | self.play(frame.increment_phi, -20 * DEGREES) 13 | 14 | def launch_at_angle(self,u,theta): 15 | 16 | surface = ParametricSurface(lambda x, y: np.array([x, y, 0]), u_range=(-0.5, 0.5), v_range=(-0.5, 0.5)) 17 | texture = TexturedSurface(surface, 'grass.jpg') 18 | thetlabel=theta 19 | theta=theta*DEGREES 20 | axes = ThreeDAxes() 21 | total = 2 * u * np.sin(theta) / 9.8 22 | ball = Sphere(radius=0.1, color=RED).move_to(axes.c2p(0, 0)) 23 | # ball=SVGMobject('rocket.svg').scale(0.5).move_to(axes.c2p(0, 0)).fix_in_frame() 24 | 25 | # curve = ParametricCurve( 26 | # lambda t: axes.c2p(t*u*np.cos(theta),t*u*np.sin(theta)-0.5*9.8*t**2), 27 | # t_range=(0, total)) 28 | path = Line(ball.get_center(), ball.get_center() + UP * 0.0001) 29 | 30 | self.add(texture, axes) 31 | 32 | def update_path(mob): 33 | line = Line(path.get_end(), ball.get_center()) 34 | path.append_vectorized_mobject(line) 35 | mob.become(path) 36 | 37 | def update(mob, alpha): 38 | intial_t = interpolate(0, total, alpha) 39 | inarr = axes.c2p(intial_t * u * np.cos(theta), intial_t * u * np.sin(theta) - 0.5 * 9.8 * intial_t ** 2) 40 | finarr = np.array([inarr[0], 0, -inarr[1]]) 41 | mob.move_to(finarr) 42 | 43 | self.add(path) 44 | self.add(ball) 45 | path.add_updater(update_path) 46 | 47 | 48 | self.play(UpdateFromAlphaFunc(ball, update), run_time=4) 49 | vector = Arrow(axes.c2p(0, 0), axes.c2p(2, 0), buff=0,color=YELLOW) 50 | vector.rotate(theta, axis=UP, about_point=axes.c2p(0, 0)) 51 | self.play(ShowCreation(vector)) 52 | 53 | arc = ArcBetweenPoints(0.5 * RIGHT, vector.point_from_proportion(0.3), angle=theta) 54 | self.play(ShowCreation(arc)) 55 | label = Tex(f"{thetlabel}","^{\\circ}",color=YELLOW).scale(0.5).next_to(arc.point_from_proportion(0.5), RIGHT,buff=0.5).fix_in_frame() 56 | self.play(ShowCreation(label)) 57 | self.wait() 58 | self.play(FadeOut(arc),FadeOut(vector),FadeOut(label)) 59 | 60 | -------------------------------------------------------------------------------- /Projectile Motion/Readme.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Projectile Motion/grass.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MathAnimation1198/ManimTutorial/1b708cc5cef09b1ceebfcacab36885840cb0091b/Projectile Motion/grass.jpg -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ManimTutorial 2 | this is my Git Repository 3 |
4 | Author-Ashish Sain 5 | -------------------------------------------------------------------------------- /Solar_System.py: -------------------------------------------------------------------------------- 1 | from manimlib import * 2 | class Planetry(ThreeDScene): 3 | def construct(self) -> None: 4 | 5 | 6 | frame=self.camera.frame 7 | frame.set_euler_angles( 8 | theta=50 * DEGREES, 9 | phi=60 * DEGREES, 10 | ) 11 | frame.scale(0.4) 12 | axes=ThreeDAxes() 13 | self.add(axes) 14 | 15 | 16 | g=1 17 | m1=1 18 | m2=1 19 | r1=2 20 | v1=0.5 21 | 22 | sun1=Sphere(radius=0.3,color=YELLOW) 23 | link='https://dm0qx8t0i9gc9.cloudfront.net/thumbnails/image/rDtN98Qoishumwih/sun-texture_GJFqg5qO_thumb.jpg' 24 | sun=TexturedSurface(sun1,link) 25 | planet1=Sphere(radius=0.2,color=YELLOW).move_to(r1*RIGHT) 26 | day_texture = "https://upload.wikimedia.org/wikipedia/commons/thumb/4/4d/Whole_world_-_land_and_oceans.jpg/1280px-Whole_world_-_land_and_oceans.jpg" 27 | 28 | planet=TexturedSurface(planet1,day_texture) 29 | 30 | planet.p=m2*np.array([0.0,v1,0.0]) 31 | 32 | self.add(sun,planet) 33 | 34 | self.t=0 35 | self.step=0.001 36 | self.counter=0 37 | def update(mob,dt): 38 | if self.counter==0: 39 | r = planet.get_center() - sun.get_center() 40 | f = -g * m1 * m2 * normalize(r) / get_norm(r) ** 2 41 | 42 | planet.p = planet.p + f * dt 43 | 44 | mob.move_to(planet.get_center() + planet.p * dt / m2) 45 | self.start=planet.get_center() + planet.p * dt / m2 46 | 47 | elif self.counter==100: 48 | r = planet.get_center() - sun.get_center() 49 | f = -g * m1 * m2 * normalize(r) / get_norm(r) ** 2 50 | 51 | planet.p = planet.p + f * dt 52 | 53 | mob.move_to(planet.get_center() + planet.p * dt / m2) 54 | end=planet.get_center() + planet.p * dt / m2 55 | arc=ArcBetweenPoints(self.start,end,angle=50*DEGREES).set_stroke(width=0).set_fill(color=BLUE,opacity=0.5) 56 | line=Polygon(ORIGIN,self.start,end).set_stroke(width=0).set_fill(color=BLUE,opacity=0.5) 57 | 58 | group=VGroup(line,arc) 59 | self.add(group) 60 | self.add(planet) 61 | elif self.counter==220: 62 | r = planet.get_center() - sun.get_center() 63 | f = -g * m1 * m2 * normalize(r) / get_norm(r) ** 2 64 | 65 | planet.p = planet.p + f * dt 66 | 67 | mob.move_to(planet.get_center() + planet.p * dt / m2) 68 | self.start=planet.get_center() + planet.p * dt / m2 69 | 70 | elif self.counter==320: 71 | r = planet.get_center() - sun.get_center() 72 | f = -g * m1 * m2 * normalize(r) / get_norm(r) ** 2 73 | 74 | planet.p = planet.p + f * dt 75 | 76 | mob.move_to(planet.get_center() + planet.p * dt / m2) 77 | end=planet.get_center() + planet.p * dt / m2 78 | arc=ArcBetweenPoints(self.start,end,angle=117*DEGREES).set_stroke(width=0).set_fill(color=BLUE,opacity=0.5) 79 | line=Polygon(ORIGIN,self.start,end).set_stroke(width=0).set_fill(color=BLUE,opacity=0.5) 80 | 81 | group=VGroup(line,arc) 82 | self.add(group) 83 | self.add(sun) 84 | self.add(planet) 85 | else: 86 | r = planet.get_center() - sun.get_center() 87 | f = -g * m1 * m2 * normalize(r) / get_norm(r) ** 2 88 | 89 | planet.p = planet.p + f * dt 90 | 91 | mob.move_to(planet.get_center() + planet.p * dt / m2) 92 | 93 | 94 | 95 | 96 | 97 | self.t+=self.step 98 | 99 | self.counter+=1 100 | planet.add_updater(update) 101 | 102 | path=Line(planet.get_center(),planet.get_center()).set_stroke(width=0.05) 103 | def path_update(mob): 104 | line=Line(path.get_end(),planet.get_center()).set_stroke(width=0.05) 105 | path.append_vectorized_mobject(line) 106 | mob.become(path) 107 | def planet_update(mob,dt): 108 | mob.rotate(dt) 109 | 110 | 111 | path.add_updater(path_update) 112 | 113 | planet.add_updater(planet_update) 114 | self.add(path) 115 | 116 | self.wait(60) 117 | -------------------------------------------------------------------------------- /change_of_basis.py: -------------------------------------------------------------------------------- 1 | from manim import * 2 | import random 3 | 4 | class change_basis(Scene): 5 | def construct(self): 6 | value1=ValueTracker(1) 7 | value2=ValueTracker(1) 8 | 9 | x_hat=always_redraw(lambda : Vector(RIGHT*value1.get_value(),color=GREEN)) 10 | y_hat=always_redraw(lambda :Vector(UP*value2.get_value(),color=YELLOW)) 11 | self.add(x_hat) 12 | 13 | # point=Dot(self.plane.coords_to_point(2,1),color=RED) 14 | 15 | # self.add(point,point_label) 16 | # self.play(Indicate(point)) 17 | # value= ValueTracker(0) 18 | # r_line= always_redraw(lambda: DashedLine(ORIGIN,2*RIGHT*value.get_value(),color=RED)) 19 | # 20 | x_axis = Matrix([ 21 | [1], 22 | [0] 23 | ]).set_color(GREEN).to_corner(LEFT+UP).shift(RIGHT) 24 | mltply=MathTex("\\times",font_size=30,color=PINK).next_to(x_axis,LEFT,0) 25 | label1 = always_redraw(lambda: DecimalNumber(value1.get_value(),font_size=24).next_to(mltply,LEFT,buff=0)) 26 | self.add(x_axis,label1,mltply) 27 | self.play(value1.animate.set_value(4),rate_func=there_and_back,run_time=2) 28 | self.play(value1.animate.set_value(-4),rate_func=there_and_back,run_time=2) 29 | 30 | # 31 | # x_axis.add_updater(lambda v:v.next_to(r_line,RIGHT)) 32 | # 33 | # self.add(r_line,x_axis) 34 | # self.play(value.animate.set_value(1)) 35 | # x_axis.clear_updaters() 36 | # r_line.clear_updaters() 37 | # self.remove(x_axis) 38 | # r_line1 = always_redraw(lambda: DashedLine(2 * RIGHT, 2 * RIGHT + UP * value.get_value(), color=YELLOW)) 39 | y_axis = Matrix([ 40 | [0], 41 | [1] 42 | ]).set_color(YELLOW).next_to(x_axis,RIGHT,buff=1.5) 43 | mltply_copy = mltply.copy().next_to(y_axis, LEFT, 0) 44 | label2 = always_redraw(lambda: DecimalNumber(value2.get_value(), font_size=24).next_to(mltply_copy, LEFT, buff=0)) 45 | plus = MathTex("+","=") 46 | plus[0].next_to(x_axis,RIGHT) 47 | self.add(y_axis, label2, mltply_copy,y_hat,plus[0]) 48 | self.play(value2.animate.set_value(4),rate_func=there_and_back,run_time=2) 49 | self.play(value2.animate.set_value(-4),rate_func=there_and_back,run_time=2) 50 | plane=NumberPlane() 51 | self.play(Create(plane)) 52 | point=Dot(2*RIGHT+3*UP) 53 | point_label = MathTex("(2,3)").next_to(point, UP) 54 | self.add(point,point_label) 55 | self.play(value1.animate.set_value(2), run_time=2) 56 | self.play(value2.animate.set_value(3), run_time=2) 57 | copy_j_hat=y_hat.copy() 58 | self.play(ApplyMethod(copy_j_hat.next_to,2*RIGHT,UP,0,path_arc=-1)) 59 | equal=plus[1].next_to(y_axis,RIGHT) 60 | self.add(equal) 61 | result = Matrix([ 62 | [2], 63 | [3] 64 | ]).set_color(RED).next_to(equal, RIGHT) 65 | self.play(Create(result)) 66 | self.wait() 67 | # y_axis.add_updater(lambda v: v.next_to(r_line1, UP)) 68 | # self.add(r_line1, y_axis) 69 | # value.set_value(0) 70 | # self.play(value.animate.set_value(1)) 71 | # self.remove(y_axis) 72 | # line=DashedLine(ORIGIN,2 * RIGHT + UP,color=YELLOW) 73 | # self.play(Create(line)) 74 | # bases_mat=Matrix([ 75 | # [1,0], 76 | # [0,1] 77 | # ]).set_color(RED).scale(0.5).to_edge(LEFT+UP) 78 | # self.play(Create(bases_mat)) 79 | # self.wait() 80 | 81 | class change_of_basis_matrix(LinearTransformationScene,MovingCameraScene): 82 | def __init__(self, *kwargs): 83 | LinearTransformationScene.__init__(self, show_basis_vectors=True,background_plane_kwargs={"x_range":(-20,20,1),"y_range":(-10,10,1)}) 84 | MovingCameraScene.__init__(self) 85 | def construct(self): 86 | # self.camera.background_color=WHITE 87 | self.camera.frame.save_state() 88 | self.camera.frame.scale(2.2) 89 | # line=Line(-15*np.array([1,-1,0]),15*np.array([1,-1,0])) 90 | # line1 = Line(-15 * np.array([1, -2, 0]), 15 * np.array([1, -2, 0])) 91 | # # self.add(line,line1) 92 | # vect=Vector(np.array([-1,1,0])) 93 | # vect1 = Vector(np.array([1, -2, 0])) 94 | # self.moving_vectors += list(vect) 95 | c_b_m=[ 96 | [1,-1], 97 | [2,4] 98 | ] 99 | self.apply_matrix(c_b_m) 100 | mat=Matrix(c_b_m).set_column_colors(GREEN,RED) 101 | self.play(mat.animate.shift(np.array([-11,6,0]))) 102 | column = mat.get_columns() 103 | mat1 = Matrix( 104 | [ 105 | [1], 106 | [2] 107 | ] 108 | ).set_column_colors(GREEN).move_to(column[0]) 109 | mat2 = Matrix( 110 | [ 111 | [-1], 112 | [4] 113 | ] 114 | ).set_column_colors(RED).move_to(column[1]) 115 | self.play(mat1.animate.next_to(self.i_hat,UP)) 116 | self.play(mat2.animate.next_to(self.j_hat, UP)) 117 | self.wait(2) 118 | self.remove(mat1,mat2) 119 | self.moving_mobjects=[] 120 | self.apply_inverse(c_b_m) 121 | matn=always_redraw(lambda : Matrix( 122 | [ 123 | [1], 124 | [2] 125 | ] 126 | ).set_column_colors(GREEN).move_to(self.i_hat.get_end(),aligned_edge=LEFT)) 127 | matn1 = always_redraw(lambda: Matrix( 128 | [ 129 | [-1], 130 | [4] 131 | ] 132 | ).set_column_colors(RED).move_to(self.j_hat.get_end(), aligned_edge=RIGHT)) 133 | # points = [] 134 | # for p_p in range(-20, 20, 1): 135 | # for p in range(-10, 10, 1): 136 | # points.append(np.array([p_p, p, 0])) 137 | # dots = VGroup(*[Dot(p) for p in points]) 138 | # self.play(Create(dots)) 139 | example1=Matrix([ 140 | [2], 141 | [1] 142 | ]).next_to(mat,RIGHT,buff=0.1) 143 | 144 | example_point=Dot(np.array([2,1,0]),color=YELLOW) 145 | example_label=MathTex("=","(2,1)","(1,8)") 146 | example_label[0].next_to(example1,RIGHT,buff=0.1) 147 | example1_result = Matrix(np.dot(c_b_m, [[2], [1]])).next_to(example_label[0],RIGHT,buff=0.2) 148 | example_point_label=always_redraw(lambda :example_label[1].next_to(example_point,UP)) 149 | self.wait(2) 150 | self.play(Create(example_label[0]), Create(example_point_label),Create(example1)) 151 | self.moving_mobjects=[example_point] 152 | # self.add_transformable_mobject(dots) 153 | self.apply_matrix(c_b_m) 154 | self.play(Transform(example_point_label,example_label[2].move_to(example_point_label)),Create(example1_result)) 155 | self.wait() 156 | x_v,y_v=self.basis_vectors 157 | x_x_v,y_v_c=x_v.copy(),y_v.copy() 158 | self.play(Wiggle(mat,scale_value=1.5,rotation_angle=0.04*TAU)) 159 | self.play(ApplyMethod(x_x_v.shift,x_v.get_end(),path_arc=PI)) 160 | self.play(ApplyMethod(y_v_c.shift, x_x_v.get_end(), path_arc=PI)) 161 | label1=BraceBetweenPoints(ORIGIN,x_x_v.get_end()) 162 | label2=BraceBetweenPoints(x_x_v.get_end(),y_v_c.get_end()) 163 | lab=MathTex("2","1") 164 | pnt=x_v.get_end() 165 | rpnt=np.array([-pnt[1],pnt[0],0]) 166 | lab[0].move_to(pnt-rpnt*0.4) 167 | lab[1].next_to(label2,RIGHT) 168 | self.play(Create(label1),Create(label2),Create(lab)) 169 | self.wait() 170 | self.remove(label1,label2,lab,x_x_v,y_v_c) 171 | self.moving_mobjects=[] 172 | self.apply_inverse(c_b_m) 173 | self.wait() 174 | x_v, y_v = self.basis_vectors 175 | x_x_v, y_v_c = x_v.copy(), VGroup(*[y_v.copy() for _ in range(8)]) 176 | first=y_v_c[0] 177 | self.play(ApplyMethod(first.shift,RIGHT,path_arc=PI)) 178 | for i in y_v_c[1:]: 179 | self.play(ApplyMethod(i.next_to,first.get_end(),UP,0,path_arc=PI),run_time=0.5) 180 | first=i 181 | self.wait() 182 | label1 = BraceBetweenPoints(ORIGIN, x_x_v.get_end()) 183 | label2 = BraceBetweenPoints(x_x_v.get_end(), y_v_c[-1].get_end()) 184 | lab = MathTex("8", "1") 185 | lab[0].next_to(label2,RIGHT) 186 | lab[1].next_to(label1, DOWN) 187 | our=np.array([ 188 | [1,0], 189 | [0,1] 190 | ]) 191 | our_m=Matrix(our).set_column_colors(GREEN,RED).next_to(label2,RIGHT,buff=2) 192 | self.add(label1, label2, lab,our_m) 193 | self.wait() 194 | # self.moving_mobjects=[] 195 | # self.apply_inverse(c_b_m) 196 | # points = [] 197 | # for p_p in range(-20, 20, 1): 198 | # for p in range(-10, 10, 1): 199 | # points.append(np.array([p_p, p, 0])) 200 | # dots = VGroup(*[Dot(p) for p in points]) 201 | # self.play(Create(dots)) 202 | # self.moving_mobjects=[dot for dot in dots] 203 | class MatrixMultiplication(Scene): 204 | def construct(self): 205 | c_b_m = [ 206 | [1, -1], 207 | [2, 4] 208 | ] 209 | c_b = [ 210 | [2], 211 | [1] 212 | ] 213 | mat=Matrix(c_b_m).shift(LEFT).set_column_colors(GREEN, RED) 214 | v=Matrix(c_b).next_to(mat,RIGHT,0.1) 215 | equal=MathTex("=").next_to(v,RIGHT) 216 | taget_mat=MobjectMatrix([ 217 | [MathTex("(1)(2)+","(-1)(1)")], 218 | [MathTex("(2)(2)+","(4)(1)")] 219 | ] 220 | ).next_to(equal,RIGHT) 221 | self.play(Create(mat),Create(v)) 222 | bracket=taget_mat.get_brackets() 223 | self.add(bracket,equal) 224 | element=mat.get_rows() 225 | v_elemet=v.get_entries() 226 | taget_value=taget_mat.get_entries() 227 | for row in range(2): 228 | sour=SurroundingRectangle(element[row]).set_fill(color=YELLOW,opacity=0.4) 229 | sour1 = SurroundingRectangle(v_elemet).set_fill(color=YELLOW, opacity=0.4) 230 | self.play(FadeIn(sour),FadeIn(sour1),rate_func=there_and_back,run_time=2) 231 | for col in range(2): 232 | elem=element[row][col] 233 | cir=Circle(color=YELLOW).surround(elem) 234 | v_ele_a=v_elemet[col] 235 | v_ele=Circle().surround(v_ele_a) 236 | self.play(Create(cir),Create(v_ele),rate_func=there_and_back) 237 | group=VGroup(elem,v_ele_a) 238 | target_v=taget_value[row][col] 239 | self.play(TransformFromCopy(group,target_v)) 240 | final_value=MathTex("1","8") 241 | final_value[0].move_to(taget_value[0]) 242 | final_value[1].move_to(taget_value[1]) 243 | for i in range(2): 244 | self.play(Transform(taget_value[i],final_value[i])) 245 | # for i in range(2): 246 | # for j in range(2): 247 | taget_value[1].next_to(taget_value[0],DOWN,0.5) 248 | self.play(ApplyMethod(taget_value.shift,1.9*LEFT),ApplyMethod(bracket[1].shift,3.5*LEFT)) 249 | self.wait() 250 | 251 | 252 | # class MatrixMultiplication(Scene): 253 | # def construct(self): 254 | # # Define matrices 255 | # matrix_a = [[1, 2], 256 | # [3, 4]] 257 | # matrix_b = [[5, 6], 258 | # [7, 8]] 259 | # 260 | # # Display matrices 261 | # matrix_a_tex = Matrix(matrix_a) 262 | # matrix_b_tex = Matrix(matrix_b) 263 | # 264 | # matrix_a_tex.next_to(ORIGIN, LEFT) 265 | # matrix_b_tex.next_to(matrix_a_tex, RIGHT) 266 | # 267 | # self.play(Write(matrix_a_tex), Write(matrix_b_tex)) 268 | # 269 | # # Calculate multiplication result 270 | # result = [[0, 0], 271 | # [0, 0]] 272 | # for i in range(2): 273 | # for j in range(2): 274 | # for k in range(2): 275 | # result[i][j] += matrix_a[i][k] * matrix_b[k][j] 276 | # 277 | # # Display result matrix 278 | # result_tex = Matrix(result) 279 | # result_tex.next_to(matrix_b_tex, RIGHT) 280 | # 281 | # self.play(Write(result_tex)) 282 | # 283 | # # Animate multiplication arrows 284 | # for i in range(2): 285 | # for j in range(2): 286 | # for k in range(2): 287 | # arrow_a = Arrow(matrix_a_tex.get_rows()[i][k].get_center(), matrix_b_tex.get_rows()[k][j].get_center(), buff=SMALL_BUFF) 288 | # arrow_b = Arrow(matrix_b_tex.get_rows()[k][j].get_center(), result_tex.get_rows()[i][j].get_center(), buff=SMALL_BUFF) 289 | # self.play(GrowArrow(arrow_a), GrowArrow(arrow_b)) 290 | # 291 | # self.wait(2) 292 | 293 | class Reverse_matrix_quetion(LinearTransformationScene,MovingCameraScene): 294 | def __init__(self, *kwargs): 295 | LinearTransformationScene.__init__(self, show_basis_vectors=True,background_plane_kwargs={"x_range":(-20,20,1),"y_range":(-10,10,1)}) 296 | MovingCameraScene.__init__(self) 297 | def construct(self): 298 | self.camera.frame.save_state() 299 | self.camera.frame.scale(2.2) 300 | c_b_m = [ 301 | [1, -1], 302 | [2, 4] 303 | ] 304 | # self.apply_matrix(c_b_m) 305 | mat = Matrix(c_b_m).set_column_colors(GREEN, RED) 306 | mat.shift(np.array([-11, 6, 0])) 307 | mat_c=mat.copy() 308 | self.play(Create(mat)) 309 | # dot=Dot(2*RIGHT+UP,color=YELLOW) 310 | # vect1,vect2=Vector(np.array([1,-2,0])),Vector(np.array([-1,1,0])) 311 | # line1,line2=Line(10*np.array([1,-2,0]),-10*np.array([1,-2,0])),Line(10*np.array([-1,1,0]),-10*np.array([-1,1,0])) 312 | # self.add(line1,line2) 313 | self.moving_mobjects=[] 314 | # self.moving_vectors+=[vect1,vect2] 315 | self.apply_matrix(c_b_m) 316 | unlnown_vector=Matrix(np.array([ 317 | ["x"], 318 | ["y"] 319 | ])).next_to(mat,RIGHT,buff=0.2) 320 | self.add(unlnown_vector) 321 | equal=MathTex("=").next_to(unlnown_vector,RIGHT,buff=0.2) 322 | target=Matrix(np.array([ 323 | [1], 324 | [8] 325 | ])).next_to(equal,RIGHT) 326 | target_point=Dot(RIGHT+8*UP,color=YELLOW) 327 | self.add(target,equal,target_point) 328 | mat_inv_o=Matrix(c_b_m,right_bracket="]^{-1}").set_column_colors(GREEN, RED).next_to(equal,RIGHT,0.1) 329 | mat_inv=Matrix([ 330 | ["\\frac{4}{6}","\\frac{1}{6}"], 331 | ["\\frac{-2}{6}","\\frac{1}{6}"] 332 | ],v_buff=1.5) 333 | x_v, y_v = self.basis_vectors 334 | x_x_v, y_v_c = x_v.copy(), y_v.copy() 335 | mat_inv.next_to(equal,RIGHT,0.2).set_column_colors(GREEN, RED) 336 | self.play(ApplyMethod(target.next_to,mat_inv,RIGHT,0.1)) 337 | self.play(AnimationGroup(Wiggle(x_v, scale_value=1.5, rotation_angle=0.03 * TAU),Wiggle(y_v, scale_value=1.5, rotation_angle=0.03 * TAU),lag_ratio=0.5),run_time=2) 338 | self.play(Transform(mat,mat_inv_o,path_arc=1),rate_func=linear,run_time=2) 339 | # self.play(Transform(mat,mat_c)) 340 | self.wait() 341 | self.play(Transform(mat,mat_inv),run_time=2) 342 | known_vector=Matrix([ 343 | [2], 344 | [1] 345 | ]).move_to(unlnown_vector) 346 | equal_c=equal.copy().next_to(target,RIGHT) 347 | known_vector.next_to(equal_c,RIGHT) 348 | 349 | self.play(Create(equal_c)) 350 | self.play(Create(known_vector)) 351 | # x_v, y_v = self.basis_vectors 352 | # x_x_v, y_v_c = x_v.copy(), y_v.copy() 353 | self.play(ApplyMethod(x_x_v.shift, x_v.get_end(), path_arc=PI)) 354 | self.play(ApplyMethod(y_v_c.shift, x_x_v.get_end(), path_arc=PI)) 355 | label1 = BraceBetweenPoints(ORIGIN, x_x_v.get_end()) 356 | label2 = BraceBetweenPoints(x_x_v.get_end(), y_v_c.get_end()) 357 | lab = MathTex("2", "1") 358 | pnt = x_v.get_end() 359 | rpnt = np.array([-pnt[1], pnt[0], 0]) 360 | lab[0].move_to(pnt - rpnt * 0.4) 361 | lab[1].next_to(label2, RIGHT) 362 | self.play(Create(label1)) 363 | self.play(Create(label2)) 364 | self.play(Create(lab)) 365 | self.wait() 366 | from functools import reduce 367 | class eigen_basis(Scene): 368 | 369 | def construct(self): 370 | plane=NumberPlane() 371 | self.background_plane_kwargs = { 372 | "color": GREY, 373 | "axis_config": { 374 | "color": GREY, 375 | }, 376 | "background_line_style": { 377 | "stroke_color": GREY, 378 | "stroke_width": 1, 379 | }, 380 | 381 | } 382 | backgroun=NumberPlane(**self.background_plane_kwargs) 383 | self.add(backgroun) 384 | 385 | eigen = np.array([ 386 | [1, -1], 387 | [0, 1] 388 | ]) 389 | vect1=Vector(RIGHT,color=GREEN) 390 | vect2=Vector(UP,color=YELLOW) 391 | transformable=[plane,backgroun] 392 | new_matrix = np.identity(3) 393 | new_matrix[:2, :2] = eigen 394 | moving_vector=[vect1,vect2] 395 | anim=[ApplyPointwiseFunction(lambda p:np.dot(p,new_matrix.T),t) for t in transformable]+[self.moving_vectors(moving_vector,new_matrix)] 396 | print(anim) 397 | self.play(*anim,run_time=2) 398 | eigen1=np.array([ 399 | [3,0], 400 | [0,2] 401 | ] 402 | ) 403 | new_matrix1 = np.identity(3) 404 | new_matrix1[:2, :2] = eigen1 405 | line=Line(ORIGIN,LEFT+UP,color=YELLOW) 406 | transformable1=[plane,vect2,vect1] 407 | self.final_mat = reduce(np.dot, [new_matrix, new_matrix1, np.linalg.inv(new_matrix)]) 408 | def change_of_base(p): 409 | # in_him=np.dot(p,np.linalg.inv(new_matrix).T) 410 | # t_p=np.dot(in_him,new_matrix1.T) 411 | return np.dot(p,self.final_mat.T) 412 | anim1=[ApplyPointwiseFunction(change_of_base,t) for t in transformable1]+[self.moving_vectors([vect2,vect1],self.final_mat)] 413 | self.play(*anim1,run_time=2) 414 | self.wait() 415 | 416 | def get_piece_movement(self, pieces): 417 | 418 | start = VGroup(*pieces) 419 | target = VGroup(*(mob.target for mob in pieces)) 420 | return Transform(start, target) 421 | 422 | def moving_vectors(self,moving_vectors,matrix): 423 | for v in moving_vectors: 424 | v.target = Vector(np.dot(v.get_end(),matrix.T), color=v.get_color()) 425 | norm = np.linalg.norm(v.target.get_end()) 426 | if norm < 0.1: 427 | v.target.get_tip().scale(norm) 428 | return self.get_piece_movement(moving_vectors) 429 | 430 | 431 | class eigen_basis1(Scene): 432 | 433 | def construct(self): 434 | plane=NumberPlane() 435 | self.background_plane_kwargs = { 436 | "color": GREY, 437 | "axis_config": { 438 | "color": GREY, 439 | }, 440 | "background_line_style": { 441 | "stroke_color": GREY, 442 | "stroke_width": 1, 443 | }, 444 | 445 | } 446 | backgroun=NumberPlane(**self.background_plane_kwargs) 447 | self.add(backgroun) 448 | 449 | eigen = np.array([ 450 | [1, -1], 451 | [0, 1] 452 | ]) 453 | vect1=Vector(RIGHT,color=GREEN) 454 | vect2=Vector(UP,color=YELLOW) 455 | transformable=[plane,backgroun] 456 | new_matrix = np.identity(3) 457 | new_matrix[:2, :2] = eigen 458 | moving_vector=[vect1,vect2] 459 | anim=[ApplyPointwiseFunction(lambda p:np.dot(p,new_matrix.T),t) for t in transformable]+[self.moving_vectors(moving_vector,new_matrix)] 460 | print(anim) 461 | self.play(*anim,run_time=2) 462 | eigen1=np.array([ 463 | [3,0], 464 | [0,2] 465 | ] 466 | ) 467 | new_matrix1 = np.identity(3) 468 | new_matrix1[:2, :2] = eigen1 469 | line=Line(ORIGIN,LEFT+UP,color=YELLOW) 470 | transformable1=[plane,vect2,vect1] 471 | self.final_mat = reduce(np.dot, [new_matrix, new_matrix1, np.linalg.inv(new_matrix)]) 472 | def change_of_base(p): 473 | # in_him=np.dot(p,np.linalg.inv(new_matrix).T) 474 | # t_p=np.dot(in_him,new_matrix1.T) 475 | return np.dot(p,self.final_mat.T) 476 | anim1=[ApplyPointwiseFunction(change_of_base,t) for t in transformable1]+[self.moving_vectors([vect2,vect1],self.final_mat)] 477 | self.play(*anim1,run_time=2) 478 | self.wait() 479 | 480 | def get_piece_movement(self, pieces): 481 | 482 | start = VGroup(*pieces) 483 | target = VGroup(*(mob.target for mob in pieces)) 484 | return Transform(start, target) 485 | 486 | def moving_vectors(self,moving_vectors,matrix): 487 | for v in moving_vectors: 488 | v.target = Vector(np.dot(v.get_end(),matrix.T), color=v.get_color()) 489 | norm = np.linalg.norm(v.target.get_end()) 490 | if norm < 0.1: 491 | v.target.get_tip().scale(norm) 492 | return self.get_piece_movement(moving_vectors) 493 | 494 | 495 | 496 | 497 | class Introduction_of(): 498 | def construct(self): 499 | tx=MathTex("AMA^{-1}",color=YELLOW) 500 | 501 | class Rotating_with_matix(Animation): 502 | def __init__( 503 | self, 504 | mobject: Mobject, 505 | axis: np.ndarray = OUT, 506 | radians: np.ndarray = TAU, 507 | about_point: np.ndarray = None, 508 | about_edge: np.ndarray= None, 509 | run_time: float = 5, 510 | rate_func= linear, 511 | **kwargs, 512 | ) -> None: 513 | self.axis = axis 514 | self.radians = radians 515 | self.about_point = about_point 516 | self.about_edge = about_edge 517 | super().__init__(mobject, run_time=run_time, rate_func=rate_func, **kwargs) 518 | 519 | def interpolate_mobject(self, alpha: float) -> None: 520 | self.mobject.become(self.starting_mobject) 521 | rotation_m = rotation_about_z(self.rate_func(alpha)*-self.radians) 522 | #axis about which you want to rotate and we consider it as z axis 523 | rotation_about_arr = normalize(self.axis) 524 | # y-axis with respect to rotation axis 525 | y_arr = normalize(np.cross(RIGHT, rotation_about_arr)) 526 | # x-axis with respect to rotation axis 527 | x_arr = np.cross(rotation_about_arr, y_arr) 528 | #new coordinate system 529 | perp_vect = np.array([ 530 | x_arr, 531 | y_arr, 532 | rotation_about_arr 533 | ]).T 534 | #rotation matrix about given axis in our coordinate system in our coordinate system 535 | final_mat = reduce(np.dot, [perp_vect, rotation_m, np.linalg.inv(perp_vect)]) 536 | self.mobject.apply_matrix(final_mat 537 | ) 538 | class Crossp(Scene): 539 | def construct(self): 540 | mat=Matrix([ 541 | [1], 542 | [1], 543 | [1] 544 | ]) 545 | nor=normalize(np.array([1,1,1])) 546 | normal=[[round(i,2)] for i in nor] 547 | matn=Matrix(normal).set_color(YELLOW) 548 | self.play(Transform(mat,matn)) 549 | cros_sign = MathTex("\\times").next_to(mat,RIGHT,0.1) 550 | riight=Matrix([ 551 | [1], 552 | [0], 553 | [0] 554 | ]).next_to(cros_sign,RIGHT,0.1) 555 | self.add(riight,cros_sign) 556 | cross_product=np.cross(RIGHT,nor) 557 | equal=MathTex("=").next_to(riight,RIGHT,0.1) 558 | cros_mat=Matrix([[round(j,2)] for j in cross_product]).next_to(equal,RIGHT,0.1) 559 | self.add(equal,cros_mat) 560 | norm_cross=normalize(cross_product) 561 | norm_y=Matrix([[round(k,2)] for k in norm_cross],color=RED).move_to(cros_mat).set_color(RED) 562 | self.play(Transform(cros_mat,norm_y)) 563 | normal_axis=VGroup(mat,cros_mat).arrange(RIGHT).to_corner(UP+LEFT) 564 | self.add(normal_axis) 565 | self.remove(equal,cros_sign,riight) 566 | c_mat,c_cros_mat=mat.copy(),cros_mat.copy() 567 | c_mat.move_to(ORIGIN) 568 | cros_sign.next_to(c_mat,RIGHT,0.1) 569 | c_cros_mat.next_to(cros_sign,RIGHT,0.1) 570 | equal.next_to(c_cros_mat,RIGHT,0.1) 571 | x_vect=Matrix([[round(p,2)] for p in np.cross(norm_cross,nor)]).next_to(equal,RIGHT,0.1).set_color(GREEN) 572 | self.add(cros_sign,c_cros_mat,c_mat,equal,x_vect) 573 | x_vect.next_to(cros_mat,RIGHT) 574 | self.remove(cros_sign,c_cros_mat,c_mat,equal) 575 | self.wait() 576 | 577 | 578 | class Application(ThreeDScene): 579 | 580 | def construct(self): 581 | self.set_camera_orientation(60*DEGREES,-45*DEGREES) 582 | axes=ThreeDAxes() 583 | # three=z_to_vector(np.array([1,1,1])) 584 | self.play(Create(axes)) 585 | self.wait() 586 | rotation_about_arr=normalize(np.array([1,1,1])) 587 | rotation_about=Arrow3D(ORIGIN,rotation_about_arr,color=BLUE) 588 | y_arr=normalize(np.cross(RIGHT,rotation_about_arr)) 589 | y_vect=Arrow3D(ORIGIN,y_arr,color=RED) 590 | self.play(Create(rotation_about)) 591 | self.play(Flash(rotation_about)) 592 | rotation_about_mat=Matrix([ 593 | [1], 594 | [1], 595 | [1] 596 | ]).scale(0.4).to_corner(corner=UP+LEFT).set_color(PINK).shift(2*RIGHT) 597 | self.add_fixed_in_frame_mobjects(rotation_about_mat) 598 | self.play(rotation_about_mat.animate.to_corner(corner=UP+LEFT).set_color(PINK).shift(2*RIGHT)) 599 | # self.begin_3dillusion_camera_rotation() 600 | # self.wait(2) 601 | # self.stop_3dillusion_camera_rotation() 602 | # self.play(FadeIn(rotation_about_mat)) 603 | # self.play(Indicate(rotation_about)) 604 | # self.play(FadeIn(y_vect)) 605 | ############################################################# 606 | self.play(Create(y_vect)) 607 | self.play(FadeOut(rotation_about_mat)) 608 | mat = Matrix([ 609 | [1], 610 | [1], 611 | [1] 612 | ]) 613 | self.add_fixed_in_frame_mobjects(mat) 614 | # mat.shift(3*UP+3*RIGHT).scale(0.5) 615 | self.play(mat.animate.shift(3*UP+3*RIGHT).scale(0.5)) 616 | nor = normalize(np.array([1, 1, 1])) 617 | normal = [[round(i, 2)] for i in nor] 618 | matn = Matrix(normal).set_color(YELLOW) 619 | matn.move_to(mat).scale(0.5) 620 | self.wait() 621 | self.add_fixed_in_frame_mobjects(matn) 622 | self.play(Create(matn),FadeOut(mat)) 623 | self.wait() 624 | cros_sign = MathTex("\\times") 625 | self.add_fixed_in_frame_mobjects(cros_sign) 626 | cros_sign.scale(0.5).next_to(matn, LEFT, 0.1) 627 | riight = Matrix([ 628 | [1], 629 | [0], 630 | [0] 631 | ]) 632 | self.play(Create(cros_sign)) 633 | self.add_fixed_in_frame_mobjects(riight) 634 | riight.scale(0.5).next_to(cros_sign, LEFT, 0.1) 635 | right_vect=Arrow3D(ORIGIN,RIGHT,color=PINK) 636 | #self.add(riight, cros_sign) 637 | self.play(Create(riight),Create(right_vect)) 638 | self.wait() 639 | cross_product = np.cross(RIGHT, nor) 640 | equal = MathTex("=") 641 | self.add_fixed_in_frame_mobjects(equal) 642 | equal.scale(0.5).next_to(matn, RIGHT, 0.1) 643 | self.play(Create(equal)) 644 | cros_mat = Matrix([[round(j, 2)] for j in cross_product]).next_to(equal, RIGHT, 0.1) 645 | self.add_fixed_in_frame_mobjects(cros_mat) 646 | cros_mat.scale(0.5).next_to(equal, RIGHT, 0.1) 647 | self.play(Create(cros_mat)) 648 | #self.add(equal, cros_mat) 649 | self.wait() 650 | norm_cross = normalize(cross_product) 651 | norm_y = Matrix([[round(k, 2)] for k in norm_cross], color=RED).move_to(cros_mat).set_color(RED) 652 | 653 | self.play(FadeOut(cros_mat)) 654 | self.add_fixed_in_frame_mobjects(norm_y) 655 | norm_y.scale(0.5).move_to(cros_mat) 656 | self.play(Create(norm_y)) 657 | self.play(Indicate(y_vect,scale_factor=1.5,color=RED)) 658 | self.play(FadeOut(right_vect)) 659 | self.wait(2) 660 | self.remove(norm_y,matn,riight,cros_sign,equal) 661 | 662 | # ###################x_vect################################## 663 | x_arr=np.cross(rotation_about_arr,y_arr) 664 | basis_vect_arr=np.array([ 665 | [round(i,2) for i in x_arr], 666 | [round(j,2) for j in y_arr], 667 | [round(k,2) for k in rotation_about_arr] 668 | ]).T 669 | x_vect=Arrow3D(ORIGIN,x_arr,color=GREEN) 670 | self.play(FadeIn(x_vect)) 671 | c_mat,c_cros_mat = Matrix(normal).set_color(YELLOW), Matrix([[round(k, 2)] for k in norm_cross], color=RED) 672 | self.add_fixed_in_frame_mobjects(c_mat) 673 | # c_mat.move_to(ORIGIN) 674 | c_mat.scale(0.5).shift(3*UP+2*RIGHT) 675 | self.play(Create(c_mat)) 676 | self.wait(2) 677 | cros_sign.next_to(c_mat, RIGHT, 0.1) 678 | self.play(Create(cros_sign)) 679 | self.add_fixed_in_frame_mobjects(c_cros_mat) 680 | c_cros_mat.scale(0.5) 681 | c_cros_mat.next_to(cros_sign, RIGHT, 0.1) 682 | self.play(Create(c_cros_mat)) 683 | equal.next_to(c_cros_mat, RIGHT, 0.1) 684 | self.play(Indicate(rotation_about,scale_factor=1.5,color=BLUE),Indicate(y_vect,scale_factor=1.5,color=RED)) 685 | self.play(Create(equal)) 686 | x_vect_mat = Matrix([[round(p, 2)] for p in np.cross(norm_cross, nor)]).next_to(equal, RIGHT, 0.1).set_color(GREEN) 687 | self.add_fixed_in_frame_mobjects(x_vect_mat) 688 | x_vect_mat.scale(0.5).next_to(equal, RIGHT) 689 | self.play(Create(x_vect_mat)) 690 | self.play(Indicate(x_vect_mat,color=GREEN),Indicate(x_vect,color=GREEN)) 691 | self.wait() 692 | basis_vect_mat=Matrix(basis_vect_arr).set_column_colors(GREEN,RED,BLUE) 693 | self.add_fixed_in_frame_mobjects(basis_vect_mat) 694 | basis_vect_mat.scale(0.5) 695 | self.play(basis_vect_mat.animate.to_corner(UP+LEFT).shift(4*RIGHT)) 696 | rotation_about_z_matrix=Matrix([ 697 | 698 | ]) 699 | self.wait() 700 | ###########################x_vect_end################################### 701 | ############################################################# 702 | # x_arr=np.cross(rotation_about_arr,y_arr) 703 | # basis_vect_arr=np.array([ 704 | # x_arr, 705 | # y_arr, 706 | # rotation_about_arr 707 | # ]).T 708 | # print(basis_vect_arr) 709 | # x_vect=Arrow3D(ORIGIN,x_arr,color=GREEN) 710 | # self.play(FadeIn(x_vect)) 711 | # # self.begin_ambient_camera_rotation(rate=1) 712 | # # self.wait(2) 713 | cube=Cube(side_length=1) 714 | self.play(Create(cube)) 715 | 716 | # matrix in differnt coordinate syytem 717 | rotation_about_axis=normalize(np.array([-1,1,0])) 718 | y_axis_with_respect_to_rotation_about_axis=normalize(np.cross(RIGHT,rotation_about_axis)) 719 | x_axis_with_respect_to_rotation_about_axis =np.cross(y_axis_with_respect_to_rotation_about_axis, 720 | rotation_about_axis) 721 | #different coordinate system matrix 722 | matrix_in_different_coordinate_system=np.array([ 723 | x_axis_with_respect_to_rotation_about_axis, 724 | y_axis_with_respect_to_rotation_about_axis, 725 | rotation_about_axis 726 | ] 727 | ).T 728 | rotation_about_z_axis_matrix=rotation_about_z(45*DEGREES) 729 | rotation_matrix_about_axis=reduce(np.dot,[matrix_in_different_coordinate_system, 730 | rotation_about_z_axis_matrix, 731 | np.linalg.inv(matrix_in_different_coordinate_system)]) 732 | 733 | self.play(cube.animate.apply_matrix(rotation_matrix_about_axis)) 734 | 735 | #Created Rotating_with_matix animated class to rotate Mobject with matrix as explained 736 | self.play(Rotating_with_matix(cube,axis=np.array([1,1,1]),run_time=2)) 737 | # self.move_camera(zoom=2) 738 | # self.play(Rotating_with_matix(cube, axis=np.array([1, 1, 1]))) 739 | # self.wait() 740 | 741 | class Application_Explanation1(ThreeDScene): 742 | def construct(self): 743 | self.set_camera_orientation(60 * DEGREES, -45 * DEGREES) 744 | axes = ThreeDAxes() 745 | 746 | self.play(Create(axes)) 747 | rotation_about_arr = normalize(np.array([1, 1, 1])) 748 | rotation_about = Arrow3D(ORIGIN, rotation_about_arr, color=BLUE) 749 | y_arr = normalize(np.cross(RIGHT, rotation_about_arr)) 750 | y_vect = Arrow3D(ORIGIN, y_arr, color=RED) 751 | self.play(FadeIn(rotation_about)) 752 | x_arr = np.cross(rotation_about_arr, y_arr) 753 | x_vect = Arrow3D(ORIGIN, x_arr, color=GREEN) 754 | self.play(FadeIn(y_vect)) 755 | self.play(FadeIn(x_vect)) 756 | basis_vect_arr = np.array([ 757 | [round(i, 2) for i in x_arr], 758 | [round(j, 2) for j in y_arr], 759 | [round(k, 2) for k in rotation_about_arr] 760 | ]).T 761 | basis_vect_mat = Matrix(basis_vect_arr).set_column_colors(GREEN, RED, BLUE) 762 | self.add_fixed_in_frame_mobjects(basis_vect_mat) 763 | basis_vect_mat.scale(0.5).to_corner(LEFT+UP).shift(4*RIGHT) 764 | self.play(Create(basis_vect_mat)) 765 | self.wait() 766 | ####################################################basis visual######################### 767 | all_lines = VGroup() 768 | for i in np.arange(-2, 2 + 1, 1): 769 | line = Line(2 * UP + i * RIGHT, -2 * UP + i * RIGHT, color=RED) 770 | all_lines.add(line) 771 | for i in np.arange(-2, 2+ 1, 1): 772 | line = Line(2* RIGHT + i * UP, -2* RIGHT + i * UP, color=GREEN) 773 | all_lines.add(line) 774 | plane = VGroup() 775 | for k in np.arange(-2, 2 + 1, 1): 776 | p = all_lines.copy() 777 | p.shift(k * OUT) 778 | plane.add(p) 779 | z_lines = VGroup() 780 | for i in np.arange(-2, 2 + 1, 1): 781 | for j in np.arange(-2, 2 + 1, 1): 782 | line = Line(2 * OUT + i * UP + j * RIGHT, -2 * OUT + i * UP + j * RIGHT, color=BLUE) 783 | z_lines.add(line) 784 | self.play(Create(z_lines), Create(plane)) 785 | self.play(ApplyPointwiseFunction(lambda p: np.dot(p, basis_vect_arr.T), z_lines), 786 | ApplyPointwiseFunction(lambda p: np.dot(p, basis_vect_arr.T), plane), run_time=3) 787 | all_group=VGroup(z_lines,plane,x_vect,y_vect,rotation_about,axes) 788 | self.play(all_group.animate.shift(-0.8*OUT)) 789 | self.begin_3dillusion_camera_rotation(rate=1) 790 | # self.begin_ambient_camera_rotation(rate=1) 791 | # self.wait(5) 792 | #####################################################end ################################ 793 | basis_vect_mat_inv = Matrix(basis_vect_arr,right_bracket="]^{-1}").set_column_colors(GREEN, RED, BLUE) 794 | self.add_fixed_in_frame_mobjects(basis_vect_mat_inv) 795 | basis_vect_mat_inv.scale(0.45).move_to(basis_vect_mat) 796 | self.play(AnimationGroup(FadeOut(basis_vect_mat),Create(basis_vect_mat_inv))) 797 | self.wait() 798 | rotation_about_z_axis=Matrix([ 799 | ["\\cos(\\theta)","-\\sin(\\theta)",0], 800 | ["\\sin(\\theta)","\\cos(\\theta)",0], 801 | [0,0,1] 802 | ],element_alignment_corner=ORIGIN,h_buff=1.7).set_column_colors(GREEN, RED, BLUE) 803 | self.add_fixed_in_frame_mobjects(rotation_about_z_axis) 804 | rotation_about_z_axis.scale(0.4).next_to(basis_vect_mat_inv,LEFT,0.1) 805 | self.play(Create(rotation_about_z_axis)) 806 | self.wait() 807 | basis_vect_mat.next_to(rotation_about_z_axis,LEFT,0) 808 | self.play(Create(basis_vect_mat.scale(0.8))) 809 | self.wait() 810 | 811 | class Application_Explanation(ThreeDScene): 812 | def construct(self): 813 | self.set_camera_orientation(60 * DEGREES, -45 * DEGREES) 814 | axes = ThreeDAxes() 815 | 816 | self.play(Create(axes)) 817 | rotation_about_arr = normalize(np.array([1, 1, 1])) 818 | rotation_about = Arrow3D(ORIGIN, rotation_about_arr, color=BLUE) 819 | y_arr = normalize(np.cross(RIGHT, rotation_about_arr)) 820 | y_vect = Arrow3D(ORIGIN, y_arr, color=RED) 821 | self.play(Create(rotation_about)) 822 | x_arr = np.cross(rotation_about_arr, y_arr) 823 | x_vect = Arrow3D(ORIGIN, x_arr, color=GREEN) 824 | self.play(FadeIn(y_vect)) 825 | self.play(FadeIn(x_vect)) 826 | basis_vect_arr = np.array([ 827 | [round(i, 2) for i in x_arr], 828 | [round(j, 2) for j in y_arr], 829 | [round(k, 2) for k in rotation_about_arr] 830 | ]).T 831 | basis_vect_mat = Matrix(basis_vect_arr).set_column_colors(GREEN, RED, BLUE) 832 | self.add_fixed_in_frame_mobjects(basis_vect_mat) 833 | basis_vect_mat.scale(0.5).to_corner(LEFT+UP) 834 | self.play(Create(basis_vect_mat)) 835 | all_lines=VGroup() 836 | for i in np.arange(-2,2+1,1): 837 | line=Line(2*UP+i*RIGHT,-2*UP+i*RIGHT,color=RED) 838 | all_lines.add(line) 839 | for i in np.arange(-2, 2 + 1, 1): 840 | line = Line(2 * RIGHT + i * UP, -2 * RIGHT + i * UP, color=GREEN) 841 | all_lines.add(line) 842 | plane=VGroup() 843 | for k in np.arange(-2,2+1,1): 844 | p=all_lines.copy() 845 | p.shift(k*OUT) 846 | plane.add(p) 847 | z_lines=VGroup() 848 | for i in np.arange(-2, 2 + 1, 1): 849 | for j in np.arange(-2, 2 + 1, 1): 850 | line = Line(2 * OUT + i * UP+j*RIGHT, -2 * OUT + i * UP+j*RIGHT, color=BLUE) 851 | z_lines.add(line) 852 | 853 | 854 | self.play(Create(z_lines),Create(plane)) 855 | self.play(ApplyPointwiseFunction(lambda p:np.dot(p,basis_vect_arr.T),z_lines),ApplyPointwiseFunction(lambda p:np.dot(p,basis_vect_arr.T),plane),run_time=3) 856 | 857 | self.begin_ambient_camera_rotation(rate=1) 858 | self.wait(5) 859 | 860 | class D2_Example(LinearTransformationScene): 861 | def __init__(self): 862 | LinearTransformationScene.__init__(self,show_basis_vectors=False,leave_ghost_vectors=False,include_background_plane=False, 863 | include_foreground_plane=False) 864 | def construct(self): 865 | vect=np.array([1,2,0]) 866 | line=Line(-5*vect,5*vect,color=RED) 867 | axes=Axes() 868 | self.play(Create(axes)) 869 | self.play(Create(line)) 870 | pop=list(np.arange(-5,5,0.05)) 871 | pop1 = list(np.arange(-4, 4, 0.1)) 872 | rand_p=[(x,y) for x,y in zip(random.sample(list(np.linspace(-3,3,100)),k=20),random.sample(list(np.linspace(-3,3,100)),k=20))] 873 | point=[] 874 | for x,y in rand_p: 875 | if y>2*x: 876 | point.append(np.array([x,y,0])) 877 | point=np.array(point) 878 | dots=VGroup(*[Dot(p,color=YELLOW) for p in point]) 879 | self.play(Create(dots)) 880 | basis_mat=np.array([ 881 | vect, 882 | [-vect[1],vect[0],0], 883 | [0,0,1] 884 | ]).T 885 | reflection_mat=np.array([ 886 | [1,0,0], 887 | [0,-1,0], 888 | [0,0,1] 889 | ]) 890 | projection=np.array([ 891 | [1,0,0], 892 | [0,0,0], 893 | [0,0,1] 894 | ]) 895 | ref_final_mat=reduce(np.dot,[basis_mat,reflection_mat,np.linalg.inv(basis_mat)]) 896 | final_proje=reduce(np.dot,[basis_mat,projection,np.linalg.inv(basis_mat)]) 897 | final_point=np.dot(point,ref_final_mat) 898 | project_point=np.dot(point,final_proje) 899 | lines=VGroup(*[DashedLine(p,q,color=PINK) for p,q in zip(point,project_point)]) 900 | self.play(Create(lines)) 901 | t_dot=[Dot(point[i],color=BLUE) for i in range(len(point))] 902 | for j in range(len(point)): 903 | first=DashedLine(point[j],t_dot[j].get_center(),color=PINK) 904 | first.add_updater(lambda v,k=j:v.become(DashedLine(point[k],t_dot[k].get_center(),color=PINK))) 905 | self.add(first) 906 | self.play(t_dot[j].animate.move_to(final_point[j])) 907 | # self.add_foreground_mobjects(first) 908 | 909 | self.wait() 910 | 911 | class D2_Example1(LinearTransformationScene): 912 | def __init__(self): 913 | LinearTransformationScene.__init__(self,show_basis_vectors=True,leave_ghost_vectors=False,include_background_plane=True, 914 | include_foreground_plane=True) 915 | 916 | def construct(self): 917 | self.remove(self.plane,self.background_plane,self.basis_vectors) 918 | vect = np.array([1, 2, 0]) 919 | unit_vect=normalize(vect) 920 | 921 | 922 | line = Line(-5 * vect, 5 * vect, color=RED) 923 | line_euation = MathTex("y","=","2","x") 924 | line_euation.set_color_by_tex("y",RED) 925 | line_euation.set_color_by_tex("x", YELLOW).next_to(np.array([1,2,0]),RIGHT,0.1) 926 | self.play(Create(line)) 927 | self.play(Create(self.plane),Create(self.background_plane)) 928 | self.play(Create(line_euation)) 929 | x_vector=Vector(unit_vect,color=GREEN) 930 | pepr_unit_y=np.array([-unit_vect[1],unit_vect[0],0]) 931 | y_vector=Vector(pepr_unit_y,color=RED) 932 | self.play(Create(x_vector)) 933 | self.play(Create(y_vector)) 934 | bases_matrix=np.array([ 935 | unit_vect, 936 | pepr_unit_y, 937 | OUT 938 | ] 939 | ).T 940 | self.moving_mobjects=[] 941 | self.moving_vectors=[] 942 | self.apply_matrix(bases_matrix) 943 | self.play(Wiggle(x_vector,scale_value=1.25,rotation_angle=0.025*TAU),Wiggle(y_vector,scale_value=1.25,rotation_angle=0.025*TAU)) 944 | point=np.dot(np.array([1,1,0]),bases_matrix.T) 945 | dot=Dot(point,color=PINK) 946 | new_bases=Matrix([ 947 | [round(unit_vect[0],2),round(pepr_unit_y[0],2)], 948 | [round(unit_vect[1],2), round(pepr_unit_y[1],2)] 949 | ],include_background_rectangle=True).scale(0.7).set_column_colors(GREEN,RED).to_corner(UP+LEFT) 950 | ncol1,ncol2=new_bases.get_columns() 951 | self.play(Create(new_bases)) 952 | our_basis=Matrix(np.array([ 953 | [*RIGHT[:2]], 954 | [*UP[:2]] 955 | ]).T,include_background_rectangle=True).scale(0.7).next_to(new_bases,RIGHT,2.5).set_column_colors(GREEN,RED) 956 | bracket=our_basis.get_brackets() 957 | 958 | self.play(Create(bracket)) 959 | col1,col2=our_basis.get_columns() 960 | self.play(TransformFromCopy(ncol1,col1)) 961 | self.play(TransformFromCopy(ncol2,col2)) 962 | self.play(Create(dot)) 963 | self.play(Wiggle(x_vector,scale_value=1.25,rotation_angle=0.025*TAU)) 964 | target_vect=y_vector.copy() 965 | self.play(ApplyMethod(target_vect.shift,x_vector.get_end(),path_arc=-PI)) 966 | rect1=SurroundingRectangle(col1,fill_color=YELLOW,fill_opacity=0.5) 967 | rect2 = SurroundingRectangle(col2, fill_color=YELLOW, fill_opacity=0.5) 968 | self.play(ShowCreationThenFadeOut(rect1),Wiggle(x_vector,scale_value=1.25,rotation_angle=0.025*TAU)) 969 | self.play(ShowCreationThenFadeOut(rect2), Wiggle(target_vect, scale_value=1.25, rotation_angle=0.025 * TAU)) 970 | self.play(AnimationGroup(*[FadeOut(i) for i in [x_vector,y_vector,target_vect,line,line_euation]],lag_ratio=0.2)) 971 | self.moving_mobjects = [] 972 | self.apply_matrix(np.linalg.inv(bases_matrix)) 973 | x_value=ValueTracker(1) 974 | y_value = ValueTracker(1) 975 | x,y=always_redraw(lambda :Vector(RIGHT*x_value.get_value(),color=GREEN,max_tip_length_to_length_ratio=0.3,max_stroke_width_to_length_ratio=10)),always_redraw(lambda :Vector(UP*y_value.get_value(),color=RED)) 976 | self.play(Create(x),Create(y)) 977 | self.play(x_value.animate.set_value(point[0])) 978 | self.play(y_value.animate.set_value(point[1])) 979 | copy_y=y.copy() 980 | self.play(ApplyMethod(copy_y.shift,x.get_end(),path_arc=PI)) 981 | point_mat=Matrix([[round(point[0],2)],[round(point[1],2)]],bracket_h_buff=0.05).set_row_colors(GREEN,RED).scale(0.5).next_to(dot,UP,0) 982 | self.play(Create(point_mat)) 983 | self.play(AnimationGroup(*[FadeOut(i) for i in [x,y,point_mat,copy_y,our_basis]], lag_ratio=0.2)) 984 | self.play(Create(self.basis_vectors)) 985 | self.moving_mobjects = [] 986 | self.moving_vectors=list(self.basis_vectors) 987 | self.apply_matrix(bases_matrix) 988 | reflection_mat = np.array([ 989 | [1, 0, 0], 990 | [0, -1, 0], 991 | [0, 0, 1] 992 | ]) 993 | 994 | ref_final_mat = reduce(np.dot, [bases_matrix, reflection_mat, np.linalg.inv(bases_matrix)]) 995 | reflected_point=np.dot(point,ref_final_mat.T) 996 | dotc=dot.copy() 997 | rpoint=reflected_point-point 998 | new_bases_inv = Matrix([ 999 | [round(unit_vect[0], 2), round(pepr_unit_y[0], 2)], 1000 | [round(unit_vect[1], 2), round(pepr_unit_y[1], 2)] 1001 | ], include_background_rectangle=True,right_bracket="]^{-1}").scale(0.7).set_column_colors(GREEN, RED).to_corner(UP + LEFT) 1002 | self.play(Transform(new_bases,new_bases_inv)) 1003 | self.play(new_bases.animate.shift(4*RIGHT)) 1004 | ref_mat=Matrix([ 1005 | [*reflection_mat[0,:2]], 1006 | [*reflection_mat[1,:2]] 1007 | ],include_background_rectangle=True).scale(0.6).set_column_colors(GREEN,RED).next_to(new_bases,LEFT,0.1) 1008 | self.play(Create(ref_mat)) 1009 | new_bases1 = Matrix([ 1010 | [round(unit_vect[0], 2), round(pepr_unit_y[0], 2)], 1011 | [round(unit_vect[1], 2), round(pepr_unit_y[1], 2)] 1012 | ], include_background_rectangle=True).scale(0.6).set_column_colors(GREEN, RED).next_to(ref_mat,LEFT,0.1) 1013 | self.play(ApplyMethod(dotc.shift, rpoint)) 1014 | self.play(Create(new_bases1)) 1015 | equal=MathTex("=").scale(0.7).next_to(new_bases,RIGHT,0.3) 1016 | final_mat=Matrix([ 1017 | [*ref_final_mat[0,:2]], 1018 | [round(ref_final_mat[1,0],2),round(ref_final_mat[1,1],2)] 1019 | ]).scale(0.7).set_column_colors(GREEN,RED).next_to(equal,RIGHT,0.2) 1020 | self.play(Create(equal)) 1021 | self.play(Create(final_mat)) 1022 | self.wait() 1023 | 1024 | class D2_Example2(LinearTransformationScene): 1025 | def __init__(self): 1026 | LinearTransformationScene.__init__(self,show_basis_vectors=True,leave_ghost_vectors=False,include_background_plane=True, 1027 | include_foreground_plane=True) 1028 | 1029 | def construct(self): 1030 | value=ValueTracker(2) 1031 | point=always_redraw(lambda :Dot(2*RIGHT+value.get_value()*UP,color=YELLOW)) 1032 | label=MathTex("(2,2)").next_to(point,UP,buff=0.1) 1033 | self.play(Create(point),Create(label)) 1034 | label2=MathTex("(2,-2)").next_to(np.array([2,-2,0]),UP,buff=0.1) 1035 | self.play(ApplyMethod(value.set_value,-2),Transform(label,label2)) 1036 | self.wait(2) 1037 | class Agenda(Scene): 1038 | def construct(self): 1039 | self.wait(2) 1040 | title=Title("Agenda") 1041 | bulleted_point=BulletedList(r"What Is Change Of Basis", 1042 | r"Intuition Behind $AMA^{-1}$ and $A^{-1}MA$", 1043 | r"Application Of Change Of Basis",buff=1.5).scale(0.7).to_corner(LEFT) 1044 | bulleted_point.set_color(YELLOW) 1045 | self.play(Create(title)) 1046 | self.play(Write(bulleted_point),run_time=3) 1047 | self.wait() 1048 | -------------------------------------------------------------------------------- /create animation engine like 3blue1brown.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import os 3 | import subprocess as sp 4 | import cairo 5 | total=60 6 | Width=1000 7 | Height=1000 8 | 9 | def draw(t,width,height): 10 | surface=cairo.ImageSurface(cairo.FORMAT_ARGB32,width,height) 11 | ctx=cairo.Context(surface) 12 | ctx.save() 13 | ctx.set_source_rgb(0.05,0,0) 14 | ctx.paint() 15 | ctx.restore() 16 | 17 | 18 | ctx.set_matrix(cairo.Matrix(width/8,0, 19 | 0,-height/8, 20 | width/2,height/2)) 21 | 22 | ctx.arc(0,0,t*2/total,0,2*np.pi) 23 | ctx.set_line_width(0.05) 24 | ctx.set_source_rgb(0,1,0) 25 | ctx.stroke() 26 | 27 | surface.write_to_png(F"ashish{t+1}.png") 28 | 29 | draw(0,Width,Height) 30 | 31 | for i in range(60): 32 | draw(i,Width,Height) 33 | 34 | 35 | 36 | pro=sp.Popen([ 37 | 'ffmpeg', 38 | '-y', 39 | '-r','30', 40 | '-i','ashish%d.png', 41 | '-vcodec', 42 | 'libx264', 43 | '-pix_fmt', 44 | 'yuv420p', 45 | 'my.mp4' 46 | ]) 47 | pro.wait() 48 | 49 | for i in range(60): 50 | os.remove(f"ashish{i+1}.png") 51 | os.startfile('my.mp4') 52 | 53 | -------------------------------------------------------------------------------- /double_pendulum.py: -------------------------------------------------------------------------------- 1 | from manimlib import * 2 | 3 | class Pendulum(VGroup): 4 | def __init__(self,x,y,length=1,mass=1): 5 | self.mass=mass 6 | self.length=length 7 | VGroup.__init__(self) 8 | self.wall=self.get_wall() 9 | self.wall.shift(x*RIGHT+y*UP) 10 | self.pendulum=self.get_pendulum() 11 | self.line1=Line(self.dot.get_center(),self.dot.get_center()+self.length*DOWN) 12 | self.dot1=Dot(radius=self.mass).move_to(self.line1.get_end()).set_fill(color=YELLOW,opacity=1) 13 | self.second=VGroup(self.line1,self.dot1) 14 | def get_wall(self): 15 | line=Line(2*LEFT,2*RIGHT) 16 | line.shift(3*UP) 17 | return line 18 | def get_pendulum(self): 19 | line=Line(ORIGIN,self.length*DOWN) 20 | line.move_to(self.wall.get_center(),aligned_edge=UP) 21 | dot=Circle(radius=self.mass).set_fill(color=RED,opacity=1) 22 | dot.move_to(line.get_edge_center(DOWN),aligned_edge=ORIGIN) 23 | self.dot=dot 24 | self.line=line 25 | return self.add(dot,line) 26 | 27 | class SingleP(Scene): 28 | def construct(self) -> None: 29 | pend=Pendulum(-3,0,length=3,mass=0.3) 30 | pend.pendulum.save_state() 31 | pend.second.save_state() 32 | theta=Tex('\\theta') 33 | 34 | 35 | value=ValueTracker(0) 36 | value.add_updater(lambda mob,dt: mob.increment_value(dt)) 37 | 38 | 39 | 40 | self.add(pend.pendulum,pend.wall,value) 41 | 42 | 43 | 44 | self.play(ShowCreation(pend.pendulum)) 45 | self.play(Rotate(pend.pendulum,75*DEGREES,about_point=pend.wall.get_center())) 46 | dline=DashedLine(pend.wall.get_center(),pend.wall.get_center()+DOWN) 47 | arc = ArcBetweenPoints(pend.pendulum[1].point_from_proportion(0.2), pend.wall.get_center()+0.6*DOWN,angle=-40*DEGREES) 48 | theta.next_to(arc.get_center()+0.5*DOWN,buff=0) 49 | self.add(arc,dline,theta) 50 | 51 | 52 | self.wait(2) 53 | 54 | 55 | 56 | b = 0.03 57 | m = 0.1 58 | g = 9.81 59 | l = 1 60 | x0 = 75*DEGREES 61 | v0 = 0 62 | 63 | dt = 0.015 64 | point=[] 65 | dot_tra=[] 66 | 67 | label=TexText('Damping Factor/Air Resistance','g','m','L','Initial angle','initial velocity').scale(0.7).shift(-2*RIGHT+0.8*DOWN) 68 | label.arrange(DOWN,center=False,aligned_edge=LEFT) 69 | outp=Tex('=0.3','=9.81','=0.1','=1','=75^{\\circ}','=0') 70 | for lab,o in zip(label,outp): 71 | o.next_to(lab,RIGHT) 72 | 73 | self.add(label,outp) 74 | 75 | axes=Axes(x_range=[0,15,1],y_range=[-1.5,1.5,1],width=6,height=2).shift(3*RIGHT) 76 | 77 | for h in np.arange(0, 200, dt): 78 | dot_tra.append([h,x0]) 79 | v0 = v0 + (-b * (v0 / m) - (g / l) * np.sin(x0)) * dt 80 | x0 = x0 + v0 * dt 81 | point.append([x0,v0]) 82 | 83 | self.counter=0 84 | def update(mob): 85 | pend.pendulum.restore() 86 | mob.rotate(point[self.counter][0], about_point=pend.wall.get_center()) 87 | self.counter+=1 88 | path=Line(axes.c2p(*dot_tra[0]),axes.c2p(*dot_tra[0])) 89 | self.tracker=1 90 | self.other1=0 91 | def line_upda(mob): 92 | line=Line(path.get_end(),axes.c2p(*dot_tra[self.tracker])) 93 | mob.become(path.append_vectorized_mobject(line)) 94 | self.tracker+=1 95 | path.add_updater(line_upda) 96 | self.add(path,axes) 97 | arc.add_updater(lambda v,dt:v.become(ArcBetweenPoints(pend.pendulum[1].point_from_proportion(0.2), pend.wall.get_center()+0.6*DOWN,angle=-np.sign(point[self.counter][0])*40*DEGREES))) 98 | theta.add_updater(lambda v:v.next_to(arc.get_center()+0.5*DOWN,buff=0)) 99 | pend.pendulum.add_updater(update) 100 | self.wait(40) 101 | 102 | class DoubleP(Scene): 103 | def construct(self) -> None: 104 | g=9.8 105 | M1=0.15 106 | M2=0.15 107 | R1=1.8 108 | R2=1.8 109 | 110 | theta1=90*DEGREES 111 | theta1dot=0 112 | theta2=10*DEGREES 113 | theta2dot=0 114 | gravity=Tex('G=9.8','M_{1}=0.15','M_{1}=0.15','R_{1}=1.8','R_{2}=1.8','\\theta_{1}=90^{\circ}','\\dot{\\theta_{1}}=0','\\theta_{2}=10^{\circ}','\\dot{\\theta_{2}}=0').shift(1*RIGHT+3*UP) 115 | gravity.arrange(DOWN,center=False,aligned_edge=LEFT) 116 | self.add(gravity) 117 | 118 | mas1=Dot(radius=M1,color=YELLOW).move_to(np.array([R1*np.sin(theta1),-R1*np.cos(theta1),0])) 119 | string = Line(ORIGIN,mas1.get_center() ) 120 | mas2 = Dot(radius=M2,color=RED).move_to(mas1.get_center()+np.array([R2 * np.sin(theta2), -R2* np.cos(theta2), 0])) 121 | string1 = Line(mas1.get_center(), mas2.get_center()) 122 | 123 | self.add(mas1,mas2,string,string1) 124 | 125 | t = 0 126 | dt = 0.04 127 | point=[] 128 | 129 | while t < 300: 130 | 131 | num=-g*(2*M1+M2)*np.sin(theta1) - M2*g*np.sin(theta1-2*theta2) - 2*np.sin(theta1-theta2)*M2*(theta2dot**2*R2+theta1dot**2*R1*np.cos(theta1-theta2)) 132 | den=R1*(2*M1+M2-M2*np.cos(2*theta1-2*theta2)) 133 | 134 | num1=2*np.sin(theta1-theta2)*(theta1dot**2*R1*(M1+M2)+g*(M1+M2)*np.cos(theta1)+theta2dot**2*R2*M2*np.cos(theta1-theta2)) 135 | den1=R2*(2*M1+M2-M2*np.cos(2*theta1-2*theta2)) 136 | 137 | theta2ddot=num1/den1 138 | theta2dot = theta2dot + theta2ddot * dt 139 | theta2 = theta2 + theta2dot * dt 140 | 141 | theta1ddot=num/den 142 | theta1dot = theta1dot + theta1ddot * dt 143 | theta1 = theta1 + theta1dot * dt 144 | 145 | point.append([theta1,theta2]) 146 | t = t + dt 147 | self.counter=0 148 | def massupd(mob,dt): 149 | mob.move_to(np.array([R1*np.sin(point[self.counter][0]),-R1*np.cos(point[self.counter][0]),0])) 150 | self.counter+=1 151 | self.counter1=0 152 | def mass1upd(mob,dt): 153 | mob.move_to(mas1.get_center()+np.array([R2 * np.sin(point[self.counter][1]), -R2* np.cos(point[self.counter][1]), 0])) 154 | self.counter1+=1 155 | mas1.add_updater(massupd) 156 | mas2.add_updater(mass1upd) 157 | string.add_updater(lambda v:v.become(Line(ORIGIN,mas1.get_center() ))) 158 | string1.add_updater(lambda v: v.become(Line(mas1.get_center(), mas2.get_center()))) 159 | self.wait(100) 160 | 161 | -------------------------------------------------------------------------------- /why_sin^2_cos^2_1.py: -------------------------------------------------------------------------------- 1 | from manim import * 2 | class Sincos(MovingCameraScene): 3 | def construct(self): 4 | self.camera.frame.scale(0.8) 5 | self.border = Rectangle(width=self.camera.frame.get_width(), 6 | height=self.camera.frame.get_height()) 7 | self.add(self.border) 8 | title=Title("WHY").scale(0.4).shift(DOWN) 9 | sub_tile=MathTex("\\cos^{2}(\\theta)","+","\\sin^{2}(\\theta)","=1").set_color_by_tex_to_color_map({"cos":PINK,"sin":GREEN}).scale(0.4).next_to(title,DOWN) 10 | self.play(Write(title)) 11 | self.play(Write(sub_tile)) 12 | circ=Circle() 13 | line=always_redraw(lambda:Line(ORIGIN,circ.get_end())) 14 | self.play(Write(line)) 15 | self.play(Create(circ)) 16 | dot=Dot(np.array([np.cos(PI/4),np.sin(PI/4),0])).scale(0.5) 17 | self.play(FadeIn(dot)) 18 | dashed_line=DashedLine(ORIGIN,dot.get_center()) 19 | unit_label=MathTex("1").scale(0.3).next_to(dashed_line,UP,buff=0).shift(0.1*LEFT+0.3*DOWN) 20 | angel=Angle(line,dashed_line,radius=0.2).set_stroke(width=2) 21 | label=MathTex("\\theta").next_to(angel,RIGHT,buff=0).scale(0.3).shift(0.1*UP) 22 | sincoslabel=MathTex("(","\\cos(\\theta)",",","\\sin(\\theta)",")")\ 23 | .scale(0.3).next_to(dot.get_center(),RIGHT,buff=0.1)\ 24 | .set_color_by_tex("\\cos(\\theta)",BLUE)\ 25 | .set_color_by_tex("\\sin(\\theta)",YELLOW) 26 | dashed_line_v=DashedLine(RIGHT*np.cos(PI/4),UP*np.cos(PI/4)+RIGHT*np.cos(PI/4)) 27 | # self.add(dashed_line,angel,label,sincoslabel,dashed_line_v,unit_label) 28 | self.play(Create(dashed_line_v)) 29 | self.play(Create(dashed_line)) 30 | self.play(Create(sincoslabel)) 31 | alu=VGroup(angel,label,unit_label) 32 | self.play(Create(alu)) 33 | self.play(ApplyMethod(sincoslabel[1].next_to,line,DOWN,0.1)) 34 | self.remove(sincoslabel[2],sincoslabel[0],sincoslabel[-1]) 35 | self.play(ApplyMethod(sincoslabel[3].next_to, dashed_line_v, RIGHT, 0.1)) 36 | tri=VMobject(color=YELLOW).set_points_as_corners([ORIGIN,RIGHT*np.cos(PI/4),UP*np.cos(PI/4)+RIGHT*np.cos(PI/4),ORIGIN]).set_stroke(width=2) 37 | self.play(Create(tri)) 38 | group=VGroup(tri,angel,label,sincoslabel[1],sincoslabel[3],unit_label) 39 | self.play(AnimationGroup(*[FadeOut(i) for i in [circ,dashed_line_v,dashed_line,line,dot]])) 40 | self.play(group.animate.scale(2).move_to(ORIGIN),run_time=2) 41 | tri_point=tri.get_start_anchors() 42 | p_point=(1-np.cos(PI/4)**2)*tri_point[0]+np.cos(PI/4)**2*tri_point[2] 43 | p_line=DashedLine(p_point,tri_point[1]).set_stroke(width=2) 44 | second=Line(tri_point[0],tri_point[2]) 45 | p_angle=RightAngle(p_line,second,quadrant=(1,-1)).scale(0.2,about_point=p_point).set_stroke(width=2) 46 | # self.add(p_line,p_angle) 47 | self.play(Create(p_line)) 48 | self.play(Create(p_angle)) 49 | tri2=VMobject(color=PINK).set_points_as_corners([tri_point[0],tri_point[1],p_point,tri_point[0]]) 50 | la_c=sincoslabel[1].copy() 51 | cos2=MathTex("\\cos^{2}(\\theta)",color=PINK).scale(0.4).next_to((tri_point[0]+p_point)/2,UP,0.2).shift(0.3*LEFT+0.2*DOWN) 52 | cos2c=cos2.copy() 53 | self.play(Create(tri2)) 54 | tri2_group=VGroup(la_c,tri2,p_angle.copy(),angel.copy(),label.copy()).save_state() 55 | self.play(tri2_group.animate.shift(1.5*DOWN+0.5*LEFT).rotate((135*DEGREES)))#.rotate((135*DEGREES) 56 | self.play(tri2_group[0].animate.shift(0.2*DOWN+0.2*RIGHT).rotate(-135*DEGREES)) 57 | tri2_cirner=tri2.get_start_anchors() 58 | tri2_label=MathTex("A","B","C").set_color_by_tex_to_color_map({"A":RED,"B":PINK,"C":GREEN}).scale(0.4) 59 | tri2_label[0].next_to(tri2_cirner[0],DOWN,0.1) 60 | tri2_label[1].next_to(tri2_cirner[1], UP, 0.1) 61 | tri2_label[2].next_to(tri2_cirner[2], DOWN, 0.1) 62 | cos2c.next_to(tri2_group,DOWN,0.1) 63 | soleve_eq = MathTex("\\frac{AC}{BA}", "=", "\\cos(\\theta)").scale(0.4).next_to(tri2, RIGHT, 0.3) 64 | # self.add(tri2_label,soleve_eq) 65 | self.play(Create(tri2_label)) 66 | self.play(Create(soleve_eq)) 67 | copy_solv=MathTex(".\\cos(\\theta)").scale(0.4).next_to(soleve_eq[-1],RIGHT,0.1) 68 | self.play(ApplyMethod(soleve_eq[0][3:].move_to,copy_solv),FadeOut(soleve_eq[0][2]),run_time=2) 69 | self.play(ApplyMethod(soleve_eq[0][3:].become, copy_solv)) 70 | result_eq=VGroup(soleve_eq[0][3:],soleve_eq[-1]) 71 | cos2c1=MathTex("\\cos^{2}(\\theta)").scale(0.4).move_to(result_eq,aligned_edge=LEFT) 72 | self.play(Transform(result_eq,cos2c1),ApplyMethod(soleve_eq[0][0:2].shift,0.1*DOWN)) 73 | self.play(ApplyMethod(result_eq.move_to,cos2c),FadeOut(soleve_eq[0][0:2]),run_time=2) 74 | self.play(ApplyMethod(tri2_group.restore),ApplyMethod(cos2c.become,cos2)) 75 | self.remove(tri2_label,soleve_eq,result_eq,soleve_eq[1]) 76 | sin_lin = Line(tri_point[1], tri_point[2]) 77 | sin_lin2 = Line(tri_point[2], tri_point[0]) 78 | sin_angle = Angle(sin_lin, sin_lin2, quadrant=(-1, 1), other_angle=True) 79 | sin_angle_l=MathTex("90-\\theta").scale(0.25).next_to(sin_angle,DOWN,0.1).shift(0.1*LEFT) 80 | self.add(sin_angle,sin_angle_l) 81 | tri2_s = VMobject(color=GREEN).set_points_as_corners([tri_point[1], tri_point[2], p_point, tri_point[1]]) 82 | sin2=MathTex("\\sin^{2}(\\theta)",color=GREEN).scale(0.4).next_to((tri_point[2]+p_point)/2,UP,0.2).shift(0.3*LEFT+0.2*DOWN) 83 | sin2c=MathTex("\\cos^{2}(90-\\theta)",color=GREEN).scale(0.4) 84 | sin2c1=sin2.copy() 85 | sin2c2 = sin2.copy() 86 | self.play(Create(tri2_s)) 87 | tri2_s_group=VGroup(tri2_s,sin_angle,sin_angle_l,sincoslabel[3]).save_state() 88 | self.play(tri2_s_group.animate.shift(2*DOWN).rotate((135*DEGREES))) 89 | self.play(ApplyMethod(tri2_s_group[2].rotate,-135 * DEGREES),ApplyMethod(tri2_s_group[-1].shift(0.2*DOWN).rotate,-135 * DEGREES)) 90 | sin2c.next_to(tri2_s_group,DOWN,0.1).shift(0.2*RIGHT) 91 | sin2c1.next_to(tri2_s_group, DOWN, 0.1).shift(0.2 * RIGHT) 92 | tri2_cirner2 = tri2_s.get_start_anchors() 93 | tri2_label2 = MathTex("A", "B", "C").set_color_by_tex_to_color_map({"A": RED, "B": PINK, "C": YELLOW}).scale(0.4) 94 | tri2_label2[0].next_to(tri2_cirner2[0], UP, 0.1) 95 | tri2_label2[1].next_to(tri2_cirner2[1], DOWN, 0.1) 96 | tri2_label2[2].next_to(tri2_cirner2[2], DOWN, 0.1).shift(0.2*RIGHT) 97 | # cos2c.next_to(tri2_group, DOWN, 0.1) 98 | self.add(tri2_label2) 99 | soleve_eq_s = MathTex("\\frac{BC}{AB}", "=", "\\cos(90-\\theta)").scale(0.32).next_to(tri2_s_group, LEFT, 0.1) 100 | self.play(Write(soleve_eq_s),ApplyMethod(tri2_s_group.shift,0.6*RIGHT),ApplyMethod(tri2_label2.shift,0.6*RIGHT)) 101 | self.play(ApplyMethod(soleve_eq_s[0][3:].next_to,soleve_eq_s[-1],RIGHT,0.1),FadeOut(soleve_eq_s[0][2]),ApplyMethod(soleve_eq_s[0][:2].shift,0.1*DOWN)) 102 | sin_t=always_redraw(lambda :MathTex("\\sin(\\theta)").scale(0.4).next_to(soleve_eq_s[-1],RIGHT,0.1)) 103 | sin_tc=MathTex("\\sin(\\theta)").scale(0.4).move_to(soleve_eq_s[-1],aligned_edge=LEFT) 104 | self.play(Transform(soleve_eq_s[0][3:],sin_t),run_time=2) 105 | self.play(Transform(soleve_eq_s[-1],sin_tc),run_time=2) 106 | sint_group=VGroup(soleve_eq_s[0][3:],soleve_eq_s[-1]) 107 | sin2c2.move_to(sint_group,aligned_edge=LEFT) 108 | self.play(Transform(sint_group,sin2c2),run_time=2) 109 | self.play(ApplyMethod(sint_group.move_to,sin2c.shift(0.6*RIGHT)),run_time=2) 110 | # self.play(Write(sin2c.shift(0.6*RIGHT))) 111 | self.play(ApplyMethod(tri2_s_group.restore),ApplyMethod(sint_group.become,sin2)) 112 | # self.play(soleve_eq_s[0].animate.shift(UP),rate_func=there_and_back) 113 | self.remove(tri2_label2,soleve_eq_s[1],soleve_eq_s[0]) 114 | self.play(FadeOut(soleve_eq_s[0][0:2])) 115 | # self.play(ApplyMethod(cos2c.shift,DOWN),ApplyMethod(cos2c.shift,DOWN)) 116 | new_cos=cos2c.copy() 117 | new_s=sint_group.copy() 118 | # new_s=always_redraw(lambda :new.next_to(new_cos,RIGHT,0.1)) 119 | # self.add(new_s) 120 | new_t_sin=MathTex("+","\\sin^{2}(\\theta)",color=GREEN) 121 | self.play(ApplyMethod(new_cos.shift,2*DOWN)) 122 | new_t_sin.scale(0.4).next_to(new_cos,RIGHT,0) 123 | self.play(Transform(new_s,new_t_sin),run_time=2) 124 | # self.play(ApplyMethod(new_s.become,new_t_sin)) 125 | equal_to1=MathTex("=1").scale(0.4).next_to(new_s,RIGHT,0.1) 126 | copy1=unit_label.copy() 127 | self.play(Transform(copy1,equal_to1),run_time=2) 128 | final_group=VGroup(equal_to1,new_s,new_cos) 129 | sourr_rect=SurroundingRectangle(final_group,color=YELLOW) 130 | self.play(Create(sourr_rect),rate_func=there_and_back) 131 | self.wait() --------------------------------------------------------------------------------