file_path
stringclasses
9 values
content
stringclasses
9 values
Manim-Tutorials-2021_brianamedee/5Tutorial_Adv2D.py
from manim import * # HELPERS FOR COMPLEX SCENES, you can always create your own :) def get_horizontal_line_to_graph(axes, function, x, width, color): result = VGroup() line = DashedLine( start=axes.c2p(0, function.underlying_function(x)), end=axes.c2p(x, function.underlying_function(x)), stroke_width=width, stroke_color=color, ) dot = Dot().set_color(color).move_to(axes.c2p(x, function.underlying_function(x))) result.add(line, dot) return result def get_arc_lines_on_function( graph, plane, dx=1, line_color=WHITE, line_width=1, x_min=None, x_max=None ): dots = VGroup() lines = VGroup() result = VGroup(dots, lines) x_range = np.arange(x_min, x_max, dx) colors = color_gradient([BLUE_B, GREEN_B], len(x_range)) for x, color in zip(x_range, colors): p1 = Dot().scale(0.7).move_to(plane.input_to_graph_point(x, graph)) p2 = Dot().scale(0.7).move_to(plane.input_to_graph_point(x + dx, graph)) dots.add(p1, p2) dots.set_fill(colors, opacity=0.8) line = Line( p1.get_center(), p2.get_center(), stroke_color=line_color, stroke_width=line_width, ) lines.add(line) return result class Derivatives(Scene): def construct(self): k = ValueTracker(-3) # Tracking the end values of stuff to show # Adding Mobjects for the first plane plane1 = ( NumberPlane(x_range=[-3, 4, 1], x_length=5, y_range=[-8, 9, 2], y_length=5) .add_coordinates() .shift(LEFT * 3.5) ) func1 = plane1.get_graph( lambda x: (1 / 3) * x ** 3, x_range=[-3, 3], color=RED_C ) func1_lab = ( MathTex("f(x)=\\frac{1}{3} {x}^{3}") .set(width=2.5) .next_to(plane1, UP, buff=0.2) .set_color(RED_C) ) moving_slope = always_redraw( lambda: plane1.get_secant_slope_group( x=k.get_value(), graph=func1, dx=0.05, secant_line_length=4, secant_line_color=YELLOW, ) ) dot = always_redraw( lambda: Dot().move_to( plane1.c2p(k.get_value(), func1.underlying_function(k.get_value())) ) ) # Adding Mobjects for the second plane plane2 = ( NumberPlane(x_range=[-3, 4, 1], x_length=5, y_range=[0, 11, 2], y_length=5) .add_coordinates() .shift(RIGHT * 3.5) ) func2 = always_redraw( lambda: plane2.get_graph( lambda x: x ** 2, x_range=[-3, k.get_value()], color=GREEN ) ) func2_lab = ( MathTex("f'(x)={x}^{2}") .set(width=2.5) .next_to(plane2, UP, buff=0.2) .set_color(GREEN) ) moving_h_line = always_redraw( lambda: get_horizontal_line_to_graph( axes=plane2, function=func2, x=k.get_value(), width=4, color=YELLOW ) ) # Adding the slope value stuff slope_value_text = ( Tex("Slope value: ") .next_to(plane1, DOWN, buff=0.1) .set_color(YELLOW) .add_background_rectangle() ) slope_value = always_redraw( lambda: DecimalNumber(num_decimal_places=1) .set_value(func2.underlying_function(k.get_value())) .next_to(slope_value_text, RIGHT, buff=0.1) .set_color(YELLOW) ).add_background_rectangle() # Playing the animation self.play( LaggedStart( DrawBorderThenFill(plane1), DrawBorderThenFill(plane2), Create(func1), Write(func1_lab), Write(func2_lab), run_time=5, lag_ratio=0.5, ) ) self.add(moving_slope, moving_h_line, func2, slope_value, slope_value_text, dot) self.play(k.animate.set_value(3), run_time=15, rate_func=linear) self.wait() class AdditiveFunctions(Scene): def construct(self): axes = ( Axes(x_range=[0, 2.1, 1], x_length=12, y_range=[0, 7, 2], y_length=7) .add_coordinates() .to_edge(DL, buff=0.25) ) func1 = axes.get_graph(lambda x: x ** 2, x_range=[0, 2], color=YELLOW) func1_lab = ( MathTex("y={x}^{2}").scale(0.8).next_to(func1, UR, buff=0).set_color(YELLOW) ) func2 = axes.get_graph(lambda x: x, x_range=[0, 2], color=GREEN) func2_lab = ( MathTex("y=x").scale(0.8).next_to(func2, UR, buff=0).set_color(GREEN) ) func3 = axes.get_graph(lambda x: x ** 2 + x, x_range=[0, 2], color=PURPLE_D) func3_lab = ( MathTex("y={x}^{2} + x") .scale(0.8) .next_to(func3, UR, buff=0) .set_color(PURPLE_D) ) self.add(axes, func1, func2, func3, func1_lab, func2_lab, func3_lab) self.wait() for k in np.arange(0.2, 2.1, 0.2): line1 = DashedLine( start=axes.c2p(k, 0), end=axes.c2p(k, func1.underlying_function(k)), stroke_color=YELLOW, stroke_width=5, ) line2 = DashedLine( start=axes.c2p(k, 0), end=axes.c2p(k, func2.underlying_function(k)), stroke_color=GREEN, stroke_width=7, ) line3 = Line( start=axes.c2p(k, 0), end=axes.c2p(k, func3.underlying_function(k)), stroke_color=PURPLE, stroke_width=10, ) self.play(Create(line1)) self.play(Create(line2)) if len(line1) > len(line2): self.play(line2.animate.shift(UP * line1.get_length())) else: self.play(line1.animate.shift(UP * line2.get_length())) self.play(Create(line3)) self.wait() # Explaining the area additive rule area1 = axes.get_riemann_rectangles( graph=func1, x_range=[0, 2], dx=0.1, color=[BLUE, GREEN] ) area2 = axes.get_riemann_rectangles( graph=func2, x_range=[0, 2], dx=0.1, color=[YELLOW, PURPLE] ) self.play(Create(area1)) self.play(area1.animate.set_opacity(0.5)) self.play(Create(area2)) self.wait() for k in range(20): self.play(area2[k].animate.shift(UP * area1[k].get_height())) self.wait() class ArcLength(Scene): def construct(self): axes = ( Axes(x_range=[-1, 4.1, 1], x_length=8, y_range=[0, 3.1, 1], y_length=6) .to_edge(DL) .add_coordinates() ) labels = axes.get_axis_labels(x_label="x", y_label="f(x)") graph = axes.get_graph( lambda x: 0.1 * x * (x + 1) * (x - 3) + 1, x_range=[-1, 4], color=BLUE ) # Mobjects for explaining construction of Line Integral dist = ValueTracker(1) dx = always_redraw( lambda: DashedLine( start=axes.c2p(2, graph.underlying_function(2)), end=axes.c2p(2 + dist.get_value(), graph.underlying_function(2)), stroke_color=GREEN, ) ) dx_brace = always_redraw(lambda: Brace(dx).next_to(dx, DOWN, buff=0.1)) dx_text = always_redraw( lambda: MathTex("dx").set(width=0.3).next_to(dx_brace, DOWN, buff=0) ) dy = always_redraw( lambda: DashedLine( start=axes.c2p( 2 + dist.get_value(), graph.underlying_function(2 + dist.get_value()), ), end=axes.c2p(2 + dist.get_value(), graph.underlying_function(2)), stroke_color=GREEN, ) ) dy_brace = always_redraw( lambda: Brace(dy, direction=RIGHT).next_to(dy, RIGHT, buff=0.1) ) dy_text = always_redraw( lambda: MathTex("dy").set(width=0.3).next_to(dy_brace, RIGHT, buff=0) ) dl = always_redraw( lambda: Line( start=axes.c2p(2, graph.underlying_function(2)), end=axes.c2p( 2 + dist.get_value(), graph.underlying_function(2 + dist.get_value()), ), stroke_color=YELLOW, ) ) dl_brace = always_redraw( lambda: BraceBetweenPoints(point_1=dl.get_end(), point_2=dl.get_start()) ) dl_text = always_redraw( lambda: MathTex("dL") .set(width=0.3) .next_to(dl_brace, UP, buff=0) .set_color(YELLOW) ) demo_mobjects = VGroup( dx, dx_brace, dx_text, dy, dy_brace, dy_text, dl, dl_brace, dl_text ) # Adding the Latex Mobjects for Mini-Proof helper_text = ( MathTex("dL \\ approximates \\ curve \\ as \\ dx\\ approaches \\ 0") .set(width=6) .to_edge(UR, buff=0.2) ) line1 = MathTex("{dL}^{2}={dx}^{2}+{dy}^{2}") line2 = MathTex("{dL}^{2}={dx}^{2}(1+(\\frac{dy}{dx})^{2})") line3 = MathTex( "dL = \\sqrt{ {dx}^{2}(1+(\\frac{dy}{dx})^{2}) }" ) # Then using surds line4 = MathTex("dL = \\sqrt{1 + (\\frac{dy}{dx})^{2} } dx") proof = ( VGroup(line1, line2, line3, line4) .scale(0.8) .arrange(DOWN, aligned_edge=LEFT) .next_to(helper_text, DOWN, buff=0.25) ) box = SurroundingRectangle(helper_text) # The actual line integral dx_tracker = ValueTracker(1) # Tracking the dx distance of line integral line_integral = always_redraw( lambda: get_arc_lines_on_function( graph=graph, plane=axes, dx=dx_tracker.get_value(), x_min=-1, x_max=4, line_color=RED, line_width=5, ) ) # Playing the animation self.add(axes, graph) self.wait() self.play(Create(dx), Create(dy)) self.play(Create(dl)) self.add(dx_brace, dx_text, dy_brace, dy_text, dl_brace, dl_text) self.play(Write(proof), run_time=5, rate_func=linear) self.wait() self.play(Write(helper_text)) self.play(Create(box), run_time=2) self.play(dist.animate.set_value(0.5), run_time=10) self.play( FadeOut(proof), demo_mobjects.animate.set_width(0.5).next_to(box, LEFT, buff=0.1), FadeOut(demo_mobjects), run_time=3, ) self.play(Create(line_integral)) self.play(dx_tracker.animate.set_value(0.2), run_time=10) self.wait()
Manim-Tutorials-2021_brianamedee/3Tutorial_LA.py
from re import L from manim import * import random class Matrix(LinearTransformationScene): def __init__(self): LinearTransformationScene.__init__( self, show_coordinates=True, leave_ghost_vectors=True, show_basis_vectors=True, ) def construct(self): matrix = [[1, 2], [2, 1]] matrix_tex = ( MathTex("A = \\begin{bmatrix} 1 & 2 \\\ 2 & 1 \\end{bmatrix}") .to_edge(UL) .add_background_rectangle() ) unit_square = self.get_unit_square() text = always_redraw( lambda: Tex("Det(A)").set(width=0.7).move_to(unit_square.get_center()) ) vect = self.get_vector([1, -2], color=PURPLE_B) rect1 = Rectangle( height=2, width=1, stroke_color=BLUE_A, fill_color=BLUE_D, fill_opacity=0.6 ).shift(UP * 2 + LEFT * 2) circ1 = Circle( radius=1, stroke_color=BLUE_A, fill_color=BLUE_D, fill_opacity=0.6 ).shift(DOWN * 2 + RIGHT * 1) self.add_transformable_mobject(vect, unit_square, rect1, circ1) self.add_background_mobject(matrix_tex, text) self.apply_matrix(matrix) self.wait() class Vectors(VectorScene): def construct(self): code = ( Code( "Tute3Vectors.py", style=Code.styles_list[12], background="window", language="python", insert_line_no=True, tab_width=2, line_spacing=0.3, scale_factor=0.5, font="Monospace", ) .set_width(6) .to_edge(UL, buff=0) ) plane = self.add_plane(animate=True).add_coordinates() self.play(Write(code), run_time=6) self.wait() vector = self.add_vector([-3, -2], color=YELLOW) basis = self.get_basis_vectors() self.add(basis) self.vector_to_coords(vector=vector) vector2 = self.add_vector([2, 2]) self.write_vector_coordinates(vector=vector2) class Tute1(Scene): def construct(self): plane = NumberPlane( x_range=[-5, 5, 1], y_range=[-4, 4, 1], x_length=10, y_length=7 ) plane.add_coordinates() plane.shift(RIGHT * 2) vect1 = Line( start=plane.coords_to_point(0, 0), end=plane.coords_to_point(3, 2), stroke_color=YELLOW, ).add_tip() vect1_name = ( MathTex("\\vec{v}").next_to(vect1, RIGHT, buff=0.1).set_color(YELLOW) ) vect2 = Line( start=plane.coords_to_point(0, 0), end=plane.coords_to_point(-2, 1), stroke_color=RED, ).add_tip() vect2_name = MathTex("\\vec{w}").next_to(vect2, LEFT, buff=0.1).set_color(RED) vect3 = Line( start=plane.coords_to_point(3, 2), end=plane.coords_to_point(1, 3), stroke_color=RED, ).add_tip() vect4 = Line( start=plane.coords_to_point(0, 0), end=plane.coords_to_point(1, 3), stroke_color=GREEN, ).add_tip() vect4_name = ( MathTex("\\vec{v} + \\vec{w}") .next_to(vect4, LEFT, buff=0.1) .set_color(GREEN) ) stuff = VGroup( plane, vect1, vect1_name, vect2, vect2_name, vect3, vect4, vect4_name ) box = RoundedRectangle( height=1.5, width=1.5, corner_radius=0.1, stroke_color=PINK ).to_edge(DL) self.play(DrawBorderThenFill(plane), run_time=2) self.wait() self.play( GrowFromPoint(vect1, point=vect1.get_start()), Write(vect1_name), run_time=2 ) self.wait() self.play( GrowFromPoint(vect2, point=vect2.get_start()), Write(vect2_name), run_time=2 ) self.wait() self.play( Transform(vect2, vect3), vect2_name.animate.next_to(vect3, UP, buff=0.1), run_time=2, ) self.wait() self.play( LaggedStart(GrowFromPoint(vect4, point=vect4.get_start())), Write(vect4_name), run_time=3, lag_ratio=1, ) self.wait() self.add(box) self.wait() self.play(stuff.animate.move_to(box.get_center()).set(width=1.2), run_time=3) self.wait()
Manim-Tutorials-2021_brianamedee/1Tutorial_Intro.py
from manim import * import numpy as np import random class Tute1(Scene): def construct(self): plane = NumberPlane(x_range=[-7,7,1], y_range=[-4,4,1]).add_coordinates() box = Rectangle(stroke_color = GREEN_C, stroke_opacity=0.7, fill_color = RED_B, fill_opacity = 0.5, height=1, width=1) dot = always_redraw(lambda : Dot().move_to(box.get_center())) code = Code("Tute1Code1.py", style=Code.styles_list[12], background ="window", language = "python", insert_line_no = True, tab_width = 2, line_spacing = 0.3, scale_factor = 0.5, font="Monospace").set_width(6).to_edge(UL, buff=0) self.play(FadeIn(plane), Write(code), run_time = 6) self.wait() self.add(box, dot) self.play(box.animate.shift(RIGHT*2), run_time=4) self.wait() self.play(box.animate.shift(UP*3), run_time=4) self.wait() self.play(box.animate.shift(DOWN*5+LEFT*5), run_time=4) self.wait() self.play(box.animate.shift(UP*1.5+RIGHT*1), run_time=4) self.wait() class Tute2(Scene): def construct(self): plane = NumberPlane(x_range=[-7,7,1], y_range=[-4,4,1]).add_coordinates() axes = Axes(x_range=[-3,3,1], y_range=[-3,3,1], x_length = 6, y_length=6) axes.to_edge(LEFT, buff=0.5) circle = Circle(stroke_width = 6, stroke_color = YELLOW, fill_color = RED_C, fill_opacity = 0.8) circle.set_width(2).to_edge(DR, buff=0) triangle = Triangle(stroke_color = ORANGE, stroke_width = 10, fill_color = GREY).set_height(2).shift(DOWN*3+RIGHT*3) code = Code("Tute1Code2.py", style=Code.styles_list[12], background ="window", language = "python", insert_line_no = True, tab_width = 2, line_spacing = 0.3, scale_factor = 0.5, font="Monospace").set_width(8).to_edge(UR, buff=0) self.play(FadeIn(plane), Write(code), run_time=6) self.wait() self.play(Write(axes)) self.wait() self.play(plane.animate.set_opacity(0.4)) self.wait() self.play(DrawBorderThenFill(circle)) self.wait() self.play(circle.animate.set_width(1)) self.wait() self.play(Transform(circle, triangle), run_time=3) self.wait() class Tute3(Scene): def construct(self): rectangle = RoundedRectangle(stroke_width = 8, stroke_color = WHITE, fill_color = BLUE_B, width = 4.5, height = 2).shift(UP*3+LEFT*4) mathtext = MathTex("\\frac{3}{4} = 0.75" ).set_color_by_gradient(GREEN, PINK).set_height(1.5) mathtext.move_to(rectangle.get_center()) mathtext.add_updater(lambda x : x.move_to(rectangle.get_center())) code = Code("Tute1Code3.py", style=Code.styles_list[12], background ="window", language = "python", insert_line_no = True, tab_width = 2, line_spacing = 0.3, scale_factor = 0.5, font="Monospace").set_width(8).to_edge(UR, buff=0) self.play(Write(code), run_time=6) self.wait() self.play(FadeIn(rectangle)) self.wait() self.play(Write(mathtext), run_time=2) self.wait() self.play(rectangle.animate.shift(RIGHT*1.5+DOWN*5), run_time=6) self.wait() mathtext.clear_updaters() self.play(rectangle.animate.shift(LEFT*2 + UP*1), run_time=6) self.wait() class Tute4(Scene): def construct(self): r = ValueTracker(0.5) #Tracks the value of the radius circle = always_redraw(lambda : Circle(radius = r.get_value(), stroke_color = YELLOW, stroke_width = 5)) line_radius = always_redraw(lambda : Line(start = circle.get_center(), end = circle.get_bottom(), stroke_color = RED_B, stroke_width = 10) ) line_circumference = always_redraw(lambda : Line(stroke_color = YELLOW, stroke_width = 5 ).set_length(2 * r.get_value() * PI).next_to(circle, DOWN, buff=0.2) ) triangle = always_redraw(lambda : Polygon(circle.get_top(), circle.get_left(), circle.get_right(), fill_color = GREEN_C) ) self.play(LaggedStart( Create(circle), DrawBorderThenFill(line_radius), DrawBorderThenFill(triangle), run_time = 4, lag_ratio = 0.75 )) self.play(ReplacementTransform(circle.copy(), line_circumference), run_time = 2) self.play(r.animate.set_value(2), run_time = 5) class testing(Scene): def construct(self): play_icon = VGroup(*[SVGMobject(f"{HOME2}\\youtube_icon.svg") for k in range(8)] ).set_height(0.75).arrange(DOWN, buff=0.2).to_edge(UL, buff=0.1) time = ValueTracker(0) l = 3 g = 10 w = np.sqrt(g/l) T = 2*PI / w theta_max = 20/180*PI p_x = -2 p_y = 3 shift_req = p_x*RIGHT+p_y*UP vertical_line = DashedLine(start = shift_req, end = shift_req+3*DOWN) theta = DecimalNumber().move_to(RIGHT*10) theta.add_updater(lambda m : m.set_value((theta_max)*np.sin(w*time.get_value()))) def get_ball(x,y): dot = Dot(fill_color = BLUE, fill_opacity = 1).move_to(x*RIGHT+y*UP).scale(3) return dot ball = always_redraw(lambda : get_ball(shift_req+l*np.sin(theta.get_value()), shift_req - l*np.cos(theta.get_value())) ) def get_string(): line = Line(color = GREY, start = shift_req, end = ball.get_center()) return line string = always_redraw(lambda : get_string()) def get_angle(theta): if theta != 0: if theta > 0: angle = Angle(line1 = string, line2 = vertical_line, other_angle = True, radius = 0.5, color = YELLOW) else: angle = VectorizedPoint() else: angle = VectorizedPoint() return angle angle = always_redraw(lambda : get_angle(theta.get_value())) guest_name = Tex("Manoj Dhakal").next_to(vertical_line.get_start(), RIGHT, buff=0.5) guest_logo = ImageMobject(f"{HOME}\\guest_logo.png").set_width(2).next_to(guest_name, DOWN, buff=0.1) pendulum = Group(string, ball, vertical_line, guest_name, guest_logo) self.play(DrawBorderThenFill(play_icon), run_time = 3) self.add(vertical_line, theta, ball, string, angle) self.wait() self.play(FadeIn(guest_name), FadeIn(guest_logo)) self.play(time.animate.set_value(2*T), rate_func = linear, run_time = 2*T) self.play(pendulum.animate.set_height(0.6).move_to(play_icon[7].get_center()), run_time = 2) self.remove(theta, angle, ball, string) self.wait() class parametric(ThreeDScene): def construct(self): axes = ThreeDAxes().add_coordinates() end = ValueTracker(-4.9) graph = always_redraw(lambda : ParametricFunction(lambda u : np.array([4*np.cos(u), 4*np.sin(u), 0.5*u]), color = BLUE, t_min = -3*TAU, t_range = [-5, end.get_value()]) ) line = always_redraw(lambda : Line(start = ORIGIN, end = graph.get_end(), color = BLUE).add_tip() ) self.set_camera_orientation(phi = 70*DEGREES, theta = -30*DEGREES) self.add(axes, graph, line) self.play(end.animate.set_value(5), run_time = 3) self.wait() class Test(Scene): def construct(self): self.camera.background_color = "#FFDE59" text = Tex("$3x \cdot 5x = 135$",color=BLACK).scale(1.4) text2 = MathTex("15x^2=135",color=BLACK).scale(1.4) a = [-2, 0, 0] b = [2, 0, 0] c = [0, 2*np.sqrt(3), 0] p = [0.37, 1.4, 0] dota = Dot(a, radius=0.06,color=BLACK) dotb = Dot(b, radius=0.06,color=BLACK) dotc = Dot(c, radius=0.06,color=BLACK) dotp = Dot(p, radius=0.06,color=BLACK) lineap = Line(dota.get_center(), dotp.get_center()).set_color(BLACK) linebp = Line(dotb.get_center(), dotp.get_center()).set_color(BLACK) linecp = Line(dotc.get_center(), dotp.get_center()).set_color(BLACK) equilateral = Polygon(a,b,c) triangle = Polygon(a,b,p) self.play(Write(equilateral)) self.wait() self.play(Write(VGroup(lineap,linebp,linecp,triangle))) self.wait() self.play(triangle.animate.rotate(0.4)) self.wait()
Manim-Tutorials-2021_brianamedee/2Tutorial_2D.py
from manim import * class GraphingIntro(Scene): def construct(self): backg_plane = NumberPlane(x_range=[-7,7,1], y_range=[-4,4,1]).add_coordinates() code = Code("Tute2Intro.py", style=Code.styles_list[12], background ="window", language = "python", insert_line_no = True, tab_width = 2, line_spacing = 0.3, scale_factor = 0.5, font="Monospace").set_width(7).to_edge(UL, buff=0) axes = Axes(x_range = [0,5,1], y_range = [0,3,1], x_length = 5, y_length = 3, axis_config = {"include_tip": True, "numbers_to_exclude": [0]} ).add_coordinates() axes.to_edge(UR) axis_labels = axes.get_axis_labels(x_label = "x", y_label = "f(x)") graph = axes.get_graph(lambda x : x**0.5, x_range = [0,4], color = YELLOW) graphing_stuff = VGroup(axes, graph, axis_labels) self.play(FadeIn(backg_plane), Write(code), run_time=6) self.play(backg_plane.animate.set_opacity(0.3)) self.wait() self.play(DrawBorderThenFill(axes), Write(axis_labels), run_time = 2) self.wait() self.play(Create(graph), run_time = 2) self.play(graphing_stuff.animate.shift(DOWN*4), run_time = 3) self.wait() self.play(axes.animate.shift(LEFT*3), run_time = 3) self.wait() class Tute1(Scene): #ILLUSTRATING HOW TO PUT A NUMBER PLANE ON SCENE WITH A GRAPH, and a line using c2p def construct(self): backg_plane = NumberPlane(x_range=[-7,7,1], y_range=[-4,4,1]) backg_plane.add_coordinates() code = Code("Tute2Code2.py", style=Code.styles_list[12], background ="window", language = "python", insert_line_no = True, tab_width = 2, line_spacing = 0.3, scale_factor = 0.5, font="Monospace").set_width(7).to_edge(UL, buff=0) my_plane = NumberPlane(x_range = [-6,6], x_length = 5, y_range = [-10,10], y_length=5) my_plane.add_coordinates() my_plane.shift(RIGHT*3) my_function = my_plane.get_graph(lambda x : 0.1*(x-5)*x*(x+5), x_range=[-6,6], color = GREEN_B) label = MathTex("f(x)=0.1x(x-5)(x+5)").next_to( my_plane, UP, buff=0.2) area = my_plane.get_area(graph = my_function, x_range = [-5,5], color = [BLUE,YELLOW]) horiz_line = Line( start = my_plane.c2p(0, my_function.underlying_function(-2)), end = my_plane.c2p(-2, my_function.underlying_function(-2)), stroke_color = YELLOW, stroke_width = 10) self.play(FadeIn(backg_plane), Write(code), run_time=6) self.play(backg_plane.animate.set_opacity(0.2)) self.wait() self.play(DrawBorderThenFill(my_plane), run_time=2) self.wait() self.play(Create(my_function), Write(label), run_time=2) self.wait() self.play(FadeIn(area), run_time = 2) self.wait() self.play(Create(horiz_line), run_time = 2) self.wait() class Tute2(Scene): #ILLUSTRATING POLAR PLANE WITH A SINE CURVE def construct(self): e = ValueTracker(0.01) #Tracks the end value of both functions plane = PolarPlane(radius_max=3).add_coordinates() plane.shift(LEFT*2) graph1 = always_redraw(lambda : ParametricFunction(lambda t : plane.polar_to_point(2*np.sin(3*t), t), t_range = [0, e.get_value()], color = GREEN) ) dot1 = always_redraw(lambda : Dot(fill_color = GREEN, fill_opacity = 0.8).scale(0.5).move_to(graph1.get_end()) ) axes = Axes(x_range = [0, 4, 1], x_length=3, y_range=[-3,3,1], y_length=3).shift(RIGHT*4) axes.add_coordinates() graph2 = always_redraw(lambda : axes.get_graph(lambda x : 2*np.sin(3*x), x_range = [0, e.get_value()], color = GREEN) ) dot2 = always_redraw(lambda : Dot(fill_color = GREEN, fill_opacity = 0.8).scale(0.5).move_to(graph2.get_end()) ) title = MathTex("f(\\theta) = 2sin(3\\theta)", color = GREEN).next_to(axes, UP, buff=0.2) self.play(LaggedStart( Write(plane), Create(axes), Write(title), run_time=3, lag_ratio=0.5) ) self.add(graph1, graph2, dot1, dot2) self.play(e.animate.set_value(PI), run_time = 10, rate_func = linear) self.wait() class Tute3(Scene): #Showing how to call 2 planes, put graphs on each and call elements to each def construct(self): backg_plane = NumberPlane(x_range=[-7,7,1], y_range=[-4,4,1]).add_coordinates() plane = NumberPlane(x_range = [-4,4,1], x_length = 4, y_range= [0, 20, 5], y_length=4).add_coordinates() plane.shift(LEFT*3+DOWN*1.5) plane_graph = plane.get_graph(lambda x : x**2, x_range = [-4,4], color = GREEN) area = plane.get_riemann_rectangles(graph = plane_graph, x_range=[-2,2], dx=0.05) axes = Axes(x_range = [-4,4,1], x_length = 4, y_range= [-20,20,5], y_length=4).add_coordinates() axes.shift(RIGHT*3+DOWN*1.5) axes_graph = axes.get_graph(lambda x : 2*x, x_range=[-4,4], color = YELLOW) v_lines = axes.get_vertical_lines_to_graph( graph = axes_graph, x_range=[-3,3], num_lines = 12) code = Code("Tute2Code1.py", style=Code.styles_list[12], background ="window", language = "python", insert_line_no = True, tab_width = 2, line_spacing = 0.3, scale_factor = 0.5, font="Monospace").set_width(6).to_edge(UL, buff=0) self.play(FadeIn(backg_plane), Write(code), run_time=6) self.play(backg_plane.animate.set_opacity(0.3)) self.play(Write(plane), Create(axes)) self.wait() self.play(Create(plane_graph), Create(axes_graph), run_time = 2) self.add(area, v_lines) self.wait() class Tute4(Scene): def construct(self): plane = ComplexPlane(axis_config = {"include_tip": True, "numbers_to_exclude": [0]}).add_coordinates() labels = plane.get_axis_labels(x_label = "Real", y_label="Imaginary") quest = MathTex("Plot \\quad 2-3i").add_background_rectangle().to_edge(UL) dot = Dot() vect1 = plane.get_vector((2,0), stroke_color = YELLOW) vect2 = Line(start = plane.c2p(2,0), end = plane.c2p(2,-3), stroke_color = YELLOW).add_tip() self.play(DrawBorderThenFill(plane), Write(labels)) self.wait() self.play(FadeIn(quest)) self.play(GrowArrow(vect1), dot.animate.move_to(plane.c2p(2,0)), rate_func = linear, run_time = 2) self.wait() self.play(GrowFromPoint((vect2), point = vect2.get_start()), dot.animate.move_to(plane.c2p(2,-3)), run_time = 2, rate_func = linear) self.wait()
Manim-Tutorials-2021_brianamedee/Manim_Tute_DetailedUpdaters.py
from manim import * import random class Old1(Scene): def construct(self): box = Rectangle( height=4, width=6, fill_color=BLUE, fill_opacity=0.55, stroke_color=WHITE ) text = MathTex("ln(2)") text.add_updater(lambda m: text.move_to(box.get_center())) self.add(box, text) self.play(box.animate.scale(0.5).to_edge(UL), run_time=3) self.wait() text.clear_updaters() self.play(box.animate.to_edge(RIGHT), run_time=3) self.wait() class Old2(Scene): def construct(self): box = Rectangle( height=4, width=6, fill_color=BLUE, fill_opacity=0.55, stroke_color=WHITE ) text = always_redraw(lambda: MathTex("ln(2)").move_to(box.get_center())) self.add(box, text) self.play(box.animate.scale(0.5).to_edge(UL), run_time=3) self.wait() text.clear_updaters() self.play(box.animate.to_edge(RIGHT), run_time=3) self.wait() def get_helper_function(color): result = VGroup() box = Rectangle( height=4, width=6, fill_color=color, fill_opacity=0.5, stroke_color=color ) text = MathTex("ln(2)").move_to(box.get_center()) result.add(box, text) return result class New(Scene): # You will notice this renders faster def construct(self): stuff = get_helper_function(color=BLUE) self.play(Create(stuff)) self.play(stuff.animate.scale(1.5).to_edge(UL), run_time=3) self.play(stuff[0].animate.to_edge(RIGHT), run_time=2) class UpdateFunc(Scene): def construct(self): def get_coin(): res = VGroup() a = random.uniform(0, 1) if a <= 0.5: c = Circle(radius=1, fill_opacity=0.5) c.set_style(fill_color=RED, stroke_color=RED) text = Tex("T").move_to(c.get_center()) res.add(c, text) res.add(c, text) else: c = Circle(radius=1, fill_opacity=0.5) c.set_style(fill_color=BLUE, stroke_color=BLUE) text = Tex("H").move_to(c.get_center()) res.add(c, text) res.add(c, text) return res def animate_coin(coin): a = random.uniform(0, 1) res = VGroup() if a <= 0.5: coin.set_style(fill_color=RED, stroke_color=RED) text = Tex("T").move_to(coin.get_center()) res.add(coin, text) else: coin.set_style(fill_color=BLUE, stroke_color=BLUE) text = Tex("H").move_to(coin.get_center()) res.add(coin, text) p = random.uniform(-0.04, 0.04) k = random.uniform(-0.04, 0.04) coin.shift(UP * k + LEFT * p) coin = always_redraw(lambda: get_coin()) self.add(coin) self.wait() self.play(UpdateFromFunc(coin, animate_coin), run_time=2) self.wait()
Manim-Tutorials-2021_brianamedee/CalculusTute.py
from manim import * class CalculusSlopes(Scene): def construct(self): plane = NumberPlane( x_range=[-3, 3], y_range=[-4, 14], y_length=7, x_length=6 ).add_coordinates() graph1 = plane.get_graph(lambda x: x ** 2, x_range=[-3, 3], color=RED) graph1_lab = ( MathTex("f(x)={x}^{2}") .next_to(graph1, UR, buff=0.2) .set_color(RED) .scale(0.8) ) c = ValueTracker(-4) graph2 = always_redraw( lambda: plane.get_graph( lambda x: x ** 2 + c.get_value(), x_range=[-3, 3], color=YELLOW ) ) graph2_lab = always_redraw( lambda: MathTex("f(x)={x}^{2}+c") .next_to(graph2, UR, buff=0.2) .set_color(YELLOW) .scale(0.8) ) k = ValueTracker(-3) dot1 = always_redraw( lambda: Dot().move_to( plane.coords_to_point( k.get_value(), graph1.underlying_function(k.get_value()) ) ) ) slope1 = always_redraw( lambda: plane.get_secant_slope_group( x=k.get_value(), graph=graph1, dx=0.01, secant_line_length=5 ) ) slope2 = always_redraw( lambda: plane.get_secant_slope_group( x=k.get_value(), graph=graph2, dx=0.01, secant_line_length=5 ) ) dot2 = always_redraw( lambda: Dot().move_to( plane.coords_to_point( k.get_value(), graph2.underlying_function(k.get_value()) ) ) ) self.play( LaggedStart(DrawBorderThenFill(plane), Create(graph1), Create(graph2)), run_time=5, lag_ratio=1, ) self.add(slope1, slope2, dot1, dot2, graph1_lab, graph2_lab) self.play( k.animate.set_value(0), c.animate.set_value(2), run_time=5, rate_func=linear ) self.play( k.animate.set_value(3), c.animate.set_value(-2), run_time=5, rate_func=linear, ) self.wait() class CalculusArea(Scene): def construct(self): axes = Axes( x_range=[-5, 5], x_length=8, y_range=[-10, 10], y_length=7 ).add_coordinates() graph = axes.get_graph( lambda x: 0.1 * (x - 4) * (x - 1) * (x + 3), x_range=[-5, 5], color=YELLOW ) self.add(axes, graph) dx_list = [1, 0.5, 0.3, 0.1, 0.05, 0.025, 0.01] rectangles = VGroup( *[ axes.get_riemann_rectangles( graph=graph, x_range=[-5, 5], stroke_width=0.1, stroke_color=WHITE, dx=dx, ) for dx in dx_list ] ) first_area = rectangles[0] for k in range(1, len(dx_list)): new_area = rectangles[k] self.play(Transform(first_area, new_area), run_time=3) self.wait(0.5) self.wait() class VectorFields(Scene): def construct(self): k = ValueTracker(0) def func(k): function = lambda p: np.array([p + k, p ** 2 + k, 0]) return function v_field = always_redraw(lambda: ArrowVectorField(func(k.get_value()))) self.add(v_field) self.wait() self.play(k.animate.set_value(2), run_time=1) class VectorFields2(Scene): def construct(self): k = ValueTracker(0) def func(k): function = lambda p: np.array([p[0] + k, p[1] ** 2 + k, 0]) return function v_field = always_redraw(lambda: ArrowVectorField(func(k.get_value()))) self.add(v_field) self.wait() self.play(k.animate.set_value(2), run_time=1) class Thumbnail(Scene): def construct(self): text = MathTex("f(x)").set_color_by_tex_to_color_map() plane = Axes(x_range=[-4, 5], x_length=13, y_range=[-5, 5], y_length=7.5) graph = plane.get_graph( lambda x: 0.1 * (x - 4) * (x - 1) * (x + 3), x_range=[-4, 5], color=YELLOW ) area = plane.get_riemann_rectangles( graph=graph, x_range=[-4, 5], stroke_width=0.1, stroke_color=WHITE, dx=0.2, ) self.add(plane, graph, area) self.wait() class stuff(Scene): def construct(self): r = ValueTracker(0.2) g = ValueTracker(0.2) b = ValueTracker(0.2) line = always_redraw( lambda: Line().set_color( rgb_to_color((r.get_value(), g.get_value(), b.get_value())) ) ) self.add(line) self.wait() self.play(r.animate.set_value(1), run_time=2)
Manim-Tutorials-2021_brianamedee/6Tutorial_Prob.py
from manim import * import random HOME = "C:\manim\Manim_7_July\Projects\\assets\Images" HOME2 = "C:\manim\Manim_7_July\Projects\\assets\SVG_Images" # This is for SVG Files to be imported. It is the directory from my PC class RandomNumbers(Scene): def construct(self): numbers = VGroup() for x in range(28): num = DecimalNumber() numbers.add(num) def randomize_numbers(numbers): for num in numbers: value = random.uniform(0, 1) num.set_value(value) if value > 0.1: num.set_color(GREEN) else: num.set_color(RED) randomize_numbers(numbers) numbers.set(width=0.38) numbers.arrange(RIGHT, buff=0.1) numbers.to_edge(UR) def get_results(numbers): results = VGroup() for num in numbers: if num.get_value() > 0.1: result = ( SVGMobject(f"{HOME2}\\green_tick.svg") .set_color(GREEN_C) .set(width=0.3) ) else: result = ( SVGMobject(f"{HOME2}\\cross.svg") .set_color(RED_C) .set(width=0.3) ) result.next_to(num, DOWN, buff=0.2) results.add(result) return results for k in range(10): self.play(UpdateFromFunc(numbers, randomize_numbers)) self.wait() result = get_results(numbers) self.play(Write(result)) self.wait() k = 0 for num in numbers: if num.get_value() > 0.1: k += 1 total = k box = SurroundingRectangle(result) self.play(Create(box)) self.play(FadeOut(box), FadeOut(result)) self.wait() def get_data_1(self): w = 0.2 t_row1 = ( VGroup( *[ SVGMobject(f"{HOME2}\\green_tick.svg").set(width=w).set_color(GREEN) for i in range(10) ] ) .arrange(RIGHT, buff=0.2) .to_edge(UL, buff=0.25) ) t_row2 = ( VGroup( *[ SVGMobject(f"{HOME2}\\green_tick.svg").set(width=w).set_color(GREEN) for i in range(10) ] ) .arrange(RIGHT, buff=0.2) .next_to(t_row1, DOWN, buff=0.25) ) f_row1 = ( VGroup( *[ SVGMobject(f"{HOME2}\\cross.svg").set(width=w).set_color(RED) for i in range(10) ] ) .arrange(RIGHT, buff=0.2) .next_to(t_row2, DOWN, buff=0.25) ) f_row2 = ( VGroup( *[ SVGMobject(f"{HOME2}\\cross.svg").set(width=w).set_color(RED) for i in range(10) ] ) .arrange(RIGHT, buff=0.2) .next_to(f_row1, DOWN, buff=0.25) ) f_row3 = ( VGroup( *[ SVGMobject(f"{HOME2}\\cross.svg").set(width=w).set_color(RED) for i in range(10) ] ) .arrange(RIGHT, buff=0.2) .next_to(f_row2, DOWN, buff=0.25) ) result = VGroup(*t_row1, *t_row2, *f_row1, *f_row2, *f_row3) return result class CentralLimitTheorem(Scene): def construct(self): data = get_data_1(self) axes = ( Axes(x_range=[0, 1.2, 0.1], y_range=[0, 2.5], x_length=10, y_length=4) .to_edge(DL) .shift(UP * 0.2) ) labels = axes.get_axis_labels(x_label="\\hat{p}", y_label="") x_axis_nums = VGroup() for i in range(11): num = ( MathTex("\\frac{%3d}{10}" % i) .scale(0.6) .next_to(axes.x_axis.n2p(i / 10 + 0.05), DOWN, buff=0.1) ) x_axis_nums.add(num) sample_counter = Tex("Total samples: ").scale(0.6).to_edge(UR).shift(LEFT * 0.6) total_counter = ( Tex("Sum of Averages: ") .scale(0.6) .next_to(sample_counter, DOWN, aligned_edge=LEFT, buff=0.4) ) average_counter = ( MathTex("Average \\ \\hat{p}: ") .scale(0.6) .next_to(total_counter, DOWN, aligned_edge=LEFT, buff=0.4) ) self.play( LaggedStart( Create(data), Write(VGroup(sample_counter, total_counter, average_counter)), Create(axes), Write(x_axis_nums), run_time=4, lag_ratio=1, ) ) self.wait() data = get_data_1(self) sample_count = 10 possible_outcomes = sample_count + 1 counter_num = 0 counter_number = ( Integer(counter_num).scale(0.5).next_to(sample_counter, RIGHT, buff=0.2) ) counter_number.add_updater(lambda m: m.set_value(counter_num)) total_sum = 0 total_number = ( DecimalNumber(total_sum).scale(0.5).next_to(total_counter, RIGHT, buff=0.2) ) total_number.add_updater(lambda m: m.set_value(total_sum)) average = 0 average_num = ( DecimalNumber(average).scale(0.5).next_to(average_counter, RIGHT, buff=0.2) ) average_num.add_updater(lambda m: m.set_value(average)) self.add(counter_number, total_number, average_num) sums = [0] * possible_outcomes # This creates an array for possible totals /10 for s in range(15): # THIS IS CALLING A RANDOM SAMPLE OF NUMBERS TO SELECT FROM a = random.sample(range(0, 50), k=sample_count) # THIS IS A GROUP FOR THE RESULTS BASED ON THE DATA sample_results = VGroup() boxes = VGroup() for i, res in enumerate(a): res = data[a[i]] box = SurroundingRectangle(res) sample_results.add(res) boxes.add(box) moved_result = sample_results.copy() self.play(Create(boxes)) self.play( moved_result.animate.arrange(RIGHT * 0.3, buff=0).to_edge(UP), FadeOut(boxes), ) # THIS ASSIGNS A VALUE FOR HOW MANY CORRECT WERE SELECTED FROM DATA for i, value in enumerate(a): if value < 20: a[i] = 1 else: a[i] = 0 prop = DecimalNumber(num_decimal_places=1) tot = sum(a) prop.set_value(tot / sample_count).set(height=0.2) prop.next_to(moved_result, RIGHT, buff=0.3) axes_box = SurroundingRectangle( moved_result, stroke_color=WHITE, stroke_width=0.2, fill_color=BLUE, fill_opacity=0.8, buff=0.1, ) stack_in_axes = VGroup(axes_box, moved_result) self.play(DrawBorderThenFill(axes_box)) self.play(Write(prop)) counter_num += 1 total_sum += tot / sample_count average = (total_sum) / (counter_num) self.play( stack_in_axes.animate.next_to(x_axis_nums[tot], UP, buff=0) .set(width=0.77) .shift(UP * sums[tot]), FadeOut(prop), ) sums[tot] += stack_in_axes.get_height() self.wait() for s in range(85): # THIS IS CALLING A RANDOM SAMPLE OF NUMBERS TO SELECT FROM a = random.sample(range(0, 50), k=sample_count) # THIS IS A GROUP FOR THE RESULTS BASED ON THE DATA sample_results = VGroup() boxes = VGroup() for i, res in enumerate(a): res = data[a[i]] box = SurroundingRectangle(res) sample_results.add(res) boxes.add(box) moved_result = sample_results.copy() self.play(Create(boxes), run_time=0.1) self.play( moved_result.animate.arrange(RIGHT * 0.3, buff=0).to_edge(UP), FadeOut(boxes), run_time=0.1, ) # THIS ASSIGNS A VALUE FOR HOW MANY CORRECT WERE SELECTED FROM DATA for i, value in enumerate(a): if value < 20: a[i] = 1 else: a[i] = 0 prop = DecimalNumber(num_decimal_places=1) tot = sum(a) prop.set_value(tot / sample_count).set(height=0.2) prop.next_to(moved_result, RIGHT, buff=0.3) axes_box = SurroundingRectangle( moved_result, stroke_color=WHITE, stroke_width=0.2, fill_color=BLUE, fill_opacity=0.8, buff=0.1, ) stack_in_axes = VGroup(axes_box, moved_result) self.add(axes_box, prop) counter_num += 1 total_sum += tot / sample_count average = (total_sum) / (counter_num) self.play( stack_in_axes.animate.next_to(x_axis_nums[tot], UP, buff=0) .set(width=0.77) .shift(UP * sums[tot]), FadeOut(prop), run_time=0.3, ) sums[tot] += stack_in_axes.get_height() self.wait()
Manim-Tutorials-2021_brianamedee/7SAofRevolution.py
from re import L from manim import * # HELPERS def get_arc_lines( graph, plane, dx=1, x_min=None, x_max=None, line_color=RED, line_width=3 ): dots = VGroup() lines = VGroup() result = VGroup(dots, lines) x_range = np.arange(x_min, x_max, dx) colors = color_gradient([BLUE_B, GREEN_B], len(x_range)) for x, color in zip(x_range, colors): p1 = Dot().move_to(plane.input_to_graph_point(x, graph)) p2 = Dot().move_to(plane.input_to_graph_point(x + dx, graph)) dots.add(p1, p2) dots.set_fill(colors, opacity=0.8) line = Line( p1.get_center(), p2.get_center(), stroke_color=line_color, stroke_width=line_width, ) lines.add(line) return result def get_conic_approximations( axes, graph, x_min=0, x_max=1, dx=0.5, color_A=RED, color_B=GREEN, opacity=1 ): result = VGroup() for x in np.arange(x_min + dx, x_max + dx, dx): if graph.underlying_function(x) == 0: k = 0 conic_surface = VectorizedPoint() else: k = graph.underlying_function(x) / x conic_surface = ParametricSurface( lambda u, v: axes.c2p(v, k * v * np.cos(u), k * v * np.sin(u)), u_min=0, u_max=2 * PI, v_min=x - dx, v_max=x, checkerboard_colors=[color_A, color_B], fill_opacity=opacity, ) result.add(conic_surface) return result def get_riemann_truncated_cones( axes, graph, x_min=0, x_max=1, dx=0.5, color_A=RED, color_B=GREEN, stroke_color=WHITE, stroke_width=1, opacity=1, theta=45, ): result = VGroup() for x in np.arange(x_min, x_max, dx): points = VGroup() p1 = axes.c2p(x + dx, 0) p2 = axes.c2p(x + dx, graph.underlying_function(x + dx)) p3 = axes.c2p(x, graph.underlying_function(x)) p4 = axes.c2p(x, 0) truncated_conic = ArcPolygon( p1, p2, p3, p4, stroke_color=stroke_color, stroke_width=stroke_width, fill_color=[color_A, color_B], fill_opacity=opacity, arc_config=[ {"angle": theta * DEGREES, "color": stroke_color}, {"angle": 0, "color": stroke_color}, {"angle": -theta * DEGREES, "color": stroke_color}, {"angle": 0, "color": stroke_color}, ], ) result.add(truncated_conic) return result class One(ThreeDScene): def construct(self): l = 2 w = 4 h = 1 rect_prism = Prism(dimensions=[l, w, h]).to_edge(LEFT, buff=1) kwargs = {"stroke_color": BLUE_D, "fill_color": BLUE_B, "fill_opacity": 0.8} bottom = Rectangle(width=w, height=l, **kwargs) s1 = Rectangle(height=h, width=w, **kwargs).next_to(bottom, UP, buff=0) s2 = Rectangle(height=h, width=w, **kwargs).next_to(bottom, DOWN, buff=0) l1 = Rectangle(height=l, width=h, **kwargs).next_to(bottom, LEFT, buff=0) l2 = Rectangle(height=l, width=h, **kwargs).next_to(bottom, RIGHT, buff=0) top = Rectangle(width=w, height=l, **kwargs).next_to(s1, UP, buff=0) net = VGroup(top, bottom, s1, s2, l1, l2).rotate(-PI / 2).to_edge(RIGHT, buff=1) arrow = Line( start=rect_prism.get_right(), end=net.get_left(), buff=0.2 ).add_tip() self.begin_ambient_camera_rotation() self.set_camera_orientation(phi=45 * DEGREES, theta=-45 * DEGREES) self.play(Create(rect_prism)) self.play( LaggedStart(Create(arrow), Transform(rect_prism.copy(), net)), run_time=2, lag_ratio=0.5, ) self.wait() self.play(FadeOut(Group(*self.mobjects))) self.stop_ambient_camera_rotation() self.set_camera_orientation(phi=0, theta=-90 * DEGREES) text = Tex("Surface Area of a Solid Revolutions?") self.play(Write(text)) self.wait() self.play(FadeOut(text)) self.begin_ambient_camera_rotation() self.set_camera_orientation(phi=45 * DEGREES, theta=-45 * DEGREES) axes = ThreeDAxes( x_range=[0, 4.1, 1], x_length=5, y_range=[-4, 4.1, 1], y_length=5, z_range=[-4, 4, 1], z_length=5, ).add_coordinates() function = axes.get_graph(lambda x: 0.25 * x ** 2, x_range=[0, 4], color=YELLOW) area = axes.get_area(graph=function, x_range=[0, 4], color=[BLUE_B, BLUE_D]) e = ValueTracker(2 * PI) surface = always_redraw( lambda: ParametricSurface( lambda u, v: axes.c2p( v, 0.25 * v ** 2 * np.cos(u), 0.25 * v ** 2 * np.sin(u) ), u_min=0, u_max=e.get_value(), v_min=0, v_max=4, checkerboard_colors=[BLUE_B, BLUE_D], ) ) self.play( LaggedStart(Create(axes), Create(function), Create(area), Create(surface)), run_time=4, lag_ratio=0.5, ) self.play( Rotating( VGroup(function, area), axis=RIGHT, radians=2 * PI, about_point=axes.c2p(0, 0, 0), ), e.animate.set_value(2 * PI), run_time=5, rate_func=linear, ) self.wait(3) class Two(ThreeDScene): def construct(self): axes = ( ThreeDAxes( x_range=[0, 4.1, 1], x_length=5, y_range=[-4, 4.1, 1], y_length=5, z_range=[-4, 4, 1], z_length=5, ) .to_edge(LEFT) .add_coordinates() ) graph = axes.get_graph(lambda x: 0.25 * x ** 2, x_range=[0, 4], color=YELLOW) surface = always_redraw( lambda: ParametricSurface( lambda u, v: axes.c2p( v, 0.25 * v ** 2 * np.cos(u), 0.25 * v ** 2 * np.sin(u) ), u_min=0, u_max=2 * PI, v_min=0, v_max=4, checkerboard_colors=[BLUE_B, BLUE_D], ) ) self.set_camera_orientation(phi=30 * DEGREES, theta=-90 * DEGREES) self.begin_ambient_camera_rotation(rate=0.1) self.play( LaggedStart(Create(axes), Create(graph), Create(surface)), run_time=2, lag_ratio=0.4, ) cone = get_conic_approximations( axes=axes, graph=graph, x_min=0, x_max=4, dx=4, opacity=0.4 ) dist = ValueTracker(2) truncated_cone = always_redraw( lambda: ParametricSurface( lambda u, v: axes.c2p(v, v * np.cos(u), v * np.sin(u)), u_min=0, u_max=2 * PI, v_min=2, v_max=2 + dist.get_value(), checkerboard_colors=[RED, GREEN], opacity=0.6, ) ) self.play(Create(cone)) self.play(Create(truncated_cone)) self.wait() a = [0, 0, 0] b = [4, 0, 0] c = [3, 2, 0] d = [1, 2, 0] ##Now wanting to work on 2D Screen truncated_net = always_redraw( lambda: ArcPolygon( [0, 0, 0], [4, 0, 0], [3, dist.get_value(), 0], [1, dist.get_value(), 0], stroke_color=RED_B, fill_color=[GREEN_B, GREEN_D], fill_opacity=0.8, arc_config=[ {"angle": 45 * DEGREES, "color": RED}, {"angle": 0, "color": RED}, {"angle": -45 * DEGREES, "color": RED}, {"angle": 0, "color": RED}, ], ) .rotate(PI / 2) .next_to(axes, RIGHT, buff=1.5) ) self.play( ReplacementTransform(truncated_cone.copy(), truncated_net), run_time=2 ) self.wait(2) self.stop_ambient_camera_rotation() self.move_camera(phi=0, theta=-90 * DEGREES) anot1 = always_redraw( lambda: MathTex("2 \\pi {r}_{0}") .set(width=0.8) .next_to(truncated_net, LEFT, buff=0.2) ) anot2 = always_redraw( lambda: MathTex("2 \\pi {r}_{1}") .set(width=0.8) .next_to(truncated_net, RIGHT, buff=0.2) ) anot3 = always_redraw(lambda: MathTex("L").next_to(truncated_net, UP, buff=0.1)) annotations = VGroup(anot1, anot2, anot3) area = MathTex("Area = 2 \\pi r L").next_to(truncated_net, DOWN, buff=0.5) anot = MathTex("where \\ r=average \\ radius").next_to( area, DOWN, aligned_edge=LEFT ) formula = VGroup(area, anot) self.play(FadeOut(VGroup(surface, cone))) self.play(Write(annotations)) self.play(Write(formula)) bound_a = MathTex("a").next_to(axes.c2p(0, 0, 0), DOWN, buff=0.1) bound_b = MathTex("b").next_to(axes.c2p(4, 0, 0), DOWN, buff=0.1) dx = always_redraw( lambda: DashedLine( start=axes.c2p(2, graph.underlying_function(2)), end=axes.c2p(2 + dist.get_value(), graph.underlying_function(2)), stroke_color=GREEN, ) ) dx_brace = always_redraw(lambda: Brace(dx).next_to(dx, DOWN, buff=0.1)) dx_text = always_redraw( lambda: MathTex("dx").set(width=0.3).next_to(dx_brace, DOWN, buff=0) ) dy = always_redraw( lambda: DashedLine( start=axes.c2p( 2 + dist.get_value(), graph.underlying_function(2 + dist.get_value()), ), end=axes.c2p(2 + dist.get_value(), graph.underlying_function(2)), stroke_color=GREEN, ) ) dy_brace = always_redraw( lambda: Brace(dy, direction=RIGHT).next_to(dy, RIGHT, buff=0.1) ) dy_text = always_redraw( lambda: MathTex("dy").set(width=0.3).next_to(dy_brace, RIGHT, buff=0) ) dl = always_redraw( lambda: Line( start=axes.c2p(2, graph.underlying_function(2)), end=axes.c2p( 2 + dist.get_value(), graph.underlying_function(2 + dist.get_value()), ), stroke_color=YELLOW, ) ) dl_brace = always_redraw( lambda: BraceBetweenPoints(point_1=dl.get_end(), point_2=dl.get_start()) ) dl_text = always_redraw( lambda: MathTex("dL") .set(width=0.3) .next_to(dl_brace, UP, buff=0) .set_color(YELLOW) ) radius_line = Line( start=axes.c2p(2.25, 0), end=axes.c2p(2.25, graph.underlying_function(2.25)), stroke_color=BLUE, stroke_width=10, ) radius_text = ( MathTex("r") .set_color(BLUE) .set(width=0.3) .next_to(radius_line, RIGHT, buff=0.1) ) demo_mobjects = VGroup( bound_a, bound_b, dx, dx_brace, dx_text, dy, dy_brace, dy_text, dl, dl_brace, dl_text, ) self.play(Create(demo_mobjects)) self.play(dist.animate.set_value(0.5), run_time=5) intuition_text = MathTex( "As \\ L\\rightarrow 0, L \\ is \\ Arc \\ Length" ).to_edge(UR, buff=0.2) sa_formula = MathTex("SA = \\int_{a}^{b}2\pi", "x", "\\ dL").next_to( intuition_text, DOWN, buff=0.2, aligned_edge=LEFT ) self.play(Write(intuition_text)) self.play(Write(sa_formula)) self.wait() self.play(Create(radius_line), Write(radius_text)) self.wait() self.play(Transform(radius_text.copy(), sa_formula[1])) self.wait() class ThreePers1(ThreeDScene): def construct(self): axes = ThreeDAxes( x_range=[0, 4, 1], x_length=5, y_range=[-4, 4, 1], y_length=5, z_range=[-4, 4, 1], z_length=5, axis_config={"decimal_number_config": {"num_decimal_places": 0}}, ).to_edge(LEFT) graph = axes.get_graph(lambda x: 0.25 * x ** 2, x_range=[0, 4], color=YELLOW) surface = always_redraw( lambda: ParametricSurface( lambda u, v: axes.c2p( v, 0.25 * v ** 2 * np.cos(u), 0.25 * v ** 2 * np.sin(u) ), u_min=0, u_max=2 * PI, v_min=0, v_max=4, checkerboard_colors=[BLUE_B, BLUE_D], ) ) dx = ValueTracker(1) conic_approx = always_redraw( lambda: get_conic_approximations( axes=axes, graph=graph, x_min=0, x_max=4, dx=dx.get_value() ) ) num_text = MathTex("dx=").next_to(axes, UP, buff=0.5) num = always_redraw( lambda: DecimalNumber() .set_value(dx.get_value()) .next_to(num_text, RIGHT, buff=0.1) ) plane = NumberPlane( x_range=[0, 4, 1], x_length=5, y_range=[0, 61, 20], y_length=6, axis_config={"decimal_number_config": {"num_decimal_places": 0}}, ).to_edge(DR) plane.add_coordinates() def sa_func(x): return 6.2832 * x * (1 + (x ** 2 / 4)) ** 0.5 graph2 = plane.get_graph(sa_func, x_range=[0, 4], color=BLUE) graph2_lab = Tex("SA Function").next_to(plane, UP, buff=0.2) t = ValueTracker( 45 ) # Tracking the bowness in the sa of conics, mathematically incorrect but whatever truncated_area = always_redraw( lambda: get_riemann_truncated_cones( axes=plane, graph=graph2, x_min=0, x_max=4, dx=dx.get_value(), theta=t.get_value(), ) ) self.set_camera_orientation(phi=0 * DEGREES, theta=-90 * DEGREES) self.add(axes, graph, surface, conic_approx, num_text, num) self.play( LaggedStart( Create(conic_approx), Write(VGroup(num_text, num)), DrawBorderThenFill(plane), run_time=2, lag_ratio=0.25, ) ) self.wait() self.play(ReplacementTransform(conic_approx.copy(), truncated_area), run_time=2) self.play( dx.animate.set_value(0.1), t.animate.set_value(5), run_time=6 ) # set dx = 0.1, and t = 5 self.play( LaggedStart(Create(graph2), Write(graph2_lab), run_time=1, lag_ratio=0.3) ) self.wait() class ThreePers2(ThreeDScene): def construct(self): axes = ThreeDAxes( x_range=[0, 4.1, 1], x_length=5, y_range=[-4, 4.1, 1], y_length=5, z_range=[-4, 4, 1], z_length=5, axis_config={"decimal_number_config": {"num_decimal_places": 0}}, ).to_edge(LEFT) axes.add_coordinates() graph = axes.get_graph(lambda x: 0.25 * x ** 2, x_range=[0, 4], color=YELLOW) surface = always_redraw( lambda: ParametricSurface( lambda u, v: axes.c2p( v, 0.25 * v ** 2 * np.cos(u), 0.25 * v ** 2 * np.sin(u) ), u_min=0, u_max=2 * PI, v_min=0, v_max=4, checkerboard_colors=[BLUE_B, BLUE_D], ) ) dx = ValueTracker(1) conic_approx = always_redraw( lambda: get_conic_approximations( axes=axes, graph=graph, x_min=0, x_max=4, dx=dx.get_value() ) ) num_text = MathTex("dx=").next_to(axes, UP, buff=0.5) num = always_redraw( lambda: DecimalNumber() .set_value(dx.get_value()) .next_to(num_text, RIGHT, buff=0.1) ) axes2 = Axes( x_range=[0, 4, 1], x_length=5, y_range=[0, 60, 10], y_length=6 ).to_edge(DR) def sa_func(x): return 6.2832 * x * (1 + (x ** 2 / 4)) ** 0.5 graph2 = axes2.get_graph(sa_func, x_range=[0, 4], color=BLUE) graph2_lab = Tex("SA Function").next_to(axes2, UP, buff=0.2) t = ValueTracker( 45 ) # Tracking the bowness in the sa of conics, mathematically incorrect but whatever truncated_area = always_redraw( lambda: get_riemann_truncated_cones( axes=axes2, graph=graph2, x_min=0, x_max=4, dx=dx.get_value(), theta=t.get_value(), ) ) self.set_camera_orientation(phi=0 * DEGREES, theta=-90 * DEGREES) self.add(axes, graph, surface, conic_approx, num_text, num) self.move_camera(phi=30 * DEGREES, theta=-100 * DEGREES) self.begin_ambient_camera_rotation(rate=0.01) self.play( LaggedStart( Create(conic_approx), Write(VGroup(num_text, num)), DrawBorderThenFill(axes2), run_time=1, lag_ratio=0.25, ) ) self.play(ReplacementTransform(conic_approx.copy(), truncated_area), run_time=1) self.play( dx.animate.set_value(0.1), t.animate.set_value(5), run_time=3 ) # set dx = 0.1, and t = 5 self.add(graph2, graph2_lab) self.wait() class FourArcL(Scene): def construct(self): axes = Axes( x_range=[0, 4.1, 1], x_length=5, y_range=[-4, 4.1, 1], y_length=5, axis_config={"decimal_number_config": {"num_decimal_places": 0}}, ).to_edge(LEFT) axes.add_coordinates() graph = axes.get_graph(lambda x: 0.25 * x ** 2, x_range=[0, 4], color=YELLOW) # Mobjects for explaining construction of Line Integral dist = ValueTracker(1) dx = always_redraw( lambda: DashedLine( start=axes.c2p(2, graph.underlying_function(2)), end=axes.c2p(2 + dist.get_value(), graph.underlying_function(2)), stroke_color=GREEN, ) ) dx_brace = always_redraw(lambda: Brace(dx).next_to(dx, DOWN, buff=0.1)) dx_text = always_redraw( lambda: MathTex("dx").set(width=0.3).next_to(dx_brace, DOWN, buff=0) ) dy = always_redraw( lambda: DashedLine( start=axes.c2p( 2 + dist.get_value(), graph.underlying_function(2 + dist.get_value()), ), end=axes.c2p(2 + dist.get_value(), graph.underlying_function(2)), stroke_color=GREEN, ) ) dy_brace = always_redraw( lambda: Brace(dy, direction=RIGHT).next_to(dy, RIGHT, buff=0.1) ) dy_text = always_redraw( lambda: MathTex("dy").set(width=0.3).next_to(dy_brace, RIGHT, buff=0) ) dl = always_redraw( lambda: Line( start=axes.c2p(2, graph.underlying_function(2)), end=axes.c2p( 2 + dist.get_value(), graph.underlying_function(2 + dist.get_value()), ), stroke_color=YELLOW, ) ) dl_brace = always_redraw( lambda: BraceBetweenPoints(point_1=dl.get_end(), point_2=dl.get_start()) ) dl_text = always_redraw( lambda: MathTex("dL") .set(width=0.3) .next_to(dl_brace, UP, buff=0) .set_color(YELLOW) ) demo_mobjects = VGroup( dx, dx_brace, dx_text, dy, dy_brace, dy_text, dl, dl_brace, dl_text ) # Adding the Latex Mobjects for Mini-Proof helper_text = ( MathTex("dL \\ approximates \\ curve \\ as \\ dx\\ approaches \\ 0") .set(width=6) .to_edge(UR, buff=0.5) ) line1 = MathTex("{dL}^{2}=", "{dx}^{2}", "+{dy}^{2}") line2 = MathTex("{dL}^{2}=", "{dx}^{2}", "(1+(\\frac{dy}{dx})^{2})") line3 = MathTex( "dL = \\sqrt{", "{dx}^{2}", "(1+(\\frac{dy}{dx})^{2}) }" ) # Then using surds line4 = MathTex("dL = \\sqrt{", "1", " + (\\frac{dy}{dx})^{2} } dxx") proof = ( VGroup(line1, line2, line3, line4) .scale(0.8) .arrange(DOWN, aligned_edge=LEFT) .next_to(helper_text, DOWN, buff=0.25) ) box = SurroundingRectangle(helper_text) # The actual line integral dx_tracker = ValueTracker(1.5) # Tracking the dx distance of line integral line_integral = always_redraw( lambda: get_arc_lines( graph=graph, plane=axes, dx=dx_tracker.get_value(), x_min=0, x_max=4, line_color=RED, line_width=7, ) ) self.add(axes, graph) self.play(Write(helper_text)) self.wait() self.play(Write(line1)) self.wait() self.play(Write(line2[0])) self.play(ReplacementTransform(line1[1].copy(), line2[1])) self.play(Write(line2[2])) self.wait() self.play(Write(line3), run_time=2) self.wait() self.play(Write(line4)) self.wait() self.add(line_integral) self.play(dx_tracker.animate.set_value(0.2), Create(box), run_time=8) self.wait() class FiveSolving(ThreeDScene): # Need to render def construct(self): axes = ThreeDAxes( x_range=[0, 4, 1], x_length=5, y_range=[-4, 4, 1], y_length=5, z_range=[-4, 4, 1], z_length=5, ).to_edge(LEFT) axes.add_coordinates() graph = axes.get_graph(lambda x: 0.25 * x ** 2, x_range=[0, 4], color=YELLOW) graph_lab = ( MathTex("y=\\frac{x^2}{4}") .scale(0.8) .next_to(graph, UP, buff=0.2) .set_color(YELLOW) ) surface = always_redraw( lambda: ParametricSurface( lambda u, v: axes.c2p( v, 0.25 * v ** 2 * np.cos(u), 0.25 * v ** 2 * np.sin(u) ), u_min=0, u_max=2 * PI, v_min=0, v_max=4, checkerboard_colors=[BLUE_B, BLUE_D], ) ) dx = ValueTracker(0.5) truncated_conics = always_redraw( lambda: get_conic_approximations( axes=axes, graph=graph, x_min=0, x_max=4, dx=dx.get_value() ) ) self.add(axes, graph, surface, graph_lab) solve0 = MathTex( "SA = \\int_{a}^{b} 2 \\pi x dL, \\ dL = \\sqrt{ 1+(\\frac{dy}{dx})^{2}) }" ) solve1 = MathTex( "SA = \\int_{a}^{b} 2 \\pi x", "\\sqrt{ 1+(\\frac{dy}{dx})^{2}) }" ) solve2 = MathTex("y=\\frac{x^2}{4} , \\", "\\frac{dy}{dx} = \\frac{x}{2}") solve3 = MathTex("(\\frac{dy}{dx})^2 = ", "\\frac{x^2}{4}") solve4 = MathTex( "SA = \\int_{0}^{4} 2 \\pi x \\sqrt{ 1 + ", "\\frac{x^2}{4}", " }" ) solve5 = MathTex("SA = 85.29 \\ units^2").next_to(axes, DOWN, buff=0.3) solved = ( VGroup(solve0, solve1, solve2, solve3, solve4) .scale(0.75) .arrange(DOWN, buff=0.2, aligned_edge=LEFT) .to_edge(UR, buff=0.2) ) self.play(Write(solve0), run_time=0.5) self.wait() self.play(Write(solve1), run_time=0.5) self.play(ReplacementTransform(graph_lab.copy(), solve2[0]), run_time=0.5) self.wait() self.play(Write(solve2[1]), run_time=0.5) self.wait() self.play(Write(solve3[0]), run_time=0.5) self.play(ReplacementTransform(solve2[1].copy(), solve3[1]), run_time=0.5) self.wait() self.play(Write(solve4), run_time=0.5) self.wait() self.move_camera(phi=30 * DEGREES, theta=-90 * DEGREES) self.play(FadeIn(truncated_conics), run_time=0.5) self.play(dx.animate.set_value(0.1), Write(solve5), run_time=2) self.wait()
Manim-Tutorials-2021_brianamedee/4Tutorial_3D.py
from manim import * class Tute1(ThreeDScene): def construct(self): axes = ThreeDAxes( x_range=[-6, 6, 1], y_range=[-6, 6, 1], z_range=[-6, 6, 1], x_length=8, y_length=6, z_length=6, ) graph = axes.get_graph(lambda x: x ** 2, x_range=[-2, 2, 1], color=YELLOW) rects = axes.get_riemann_rectangles( graph=graph, x_range=[-2, 2], dx=0.1, stroke_color=WHITE ) graph2 = axes.get_parametric_curve( lambda t: np.array([np.cos(t), np.sin(t), t]), t_range=[-2 * PI, 2 * PI], color=RED, ) self.add(axes, graph) self.wait() ##THE CAMERA IS AUTO SET TO PHI = 0 and THETA = -90 self.move_camera(phi=60 * DEGREES) self.wait() self.move_camera(theta=-45 * DEGREES) self.begin_ambient_camera_rotation( rate=PI / 10, about="theta" ) # Rotates at a rate of radians per second self.wait() self.play(Create(rects), run_time=3) self.play(Create(graph2)) self.wait() self.stop_ambient_camera_rotation() self.wait() self.begin_ambient_camera_rotation( rate=PI / 10, about="phi" ) # Rotates at a rate of radians per second self.wait(2) self.stop_ambient_camera_rotation() class Tute2(ThreeDScene): def construct(self): code = ( Code( "Tute4Code1.py", style=Code.styles_list[12], background="window", language="python", insert_line_no=True, tab_width=2, line_spacing=0.3, scale_factor=0.5, font="Monospace", ) .set(width=5) .to_edge(UR, buff=0) ) axes = ThreeDAxes() graph = axes.get_graph(lambda x: x ** 2, x_range=[-2, 2], color=YELLOW) surface = ParametricSurface( lambda u, v: axes.c2p(v * np.cos(u), v * np.sin(u), 0.5 * v ** 2), u_min=0, u_max=2 * PI, v_min=0, v_max=3, checkerboard_colors=[GREEN, RED], ) three_d_stuff = VGroup(axes, graph, surface) self.play(Write(code), run_time=5) self.wait() self.set_camera_orientation(phi=60 * DEGREES, theta=-45 * DEGREES) self.add(axes, graph) self.begin_ambient_camera_rotation(rate=PI / 20) self.play(Create(surface)) self.play(three_d_stuff.animate.shift(LEFT * 5)) class Tute3(ThreeDScene): def construct(self): self.set_camera_orientation(phi=45 * DEGREES, theta=-45 * DEGREES) axes = ThreeDAxes(y_range=[-3, 10, 3], y_length=7).add_coordinates() graph = axes.get_graph(lambda x: x, x_range=[0, 3], color=RED_B) area = axes.get_area(graph=graph, x_range=[0, 3]) e = ValueTracker(0) surface = always_redraw( lambda: ParametricSurface( lambda u, v: axes.c2p(v, v * np.cos(u), v * np.sin(u)), u_min=0, u_max=e.get_value(), v_min=0, v_max=3, checkerboard_colors=[GREEN, PURPLE], ) ) self.add(axes, surface) self.begin_ambient_camera_rotation(rate=PI / 15) self.play(LaggedStart(Create(graph), Create(area))) self.play( Rotating(area, axis=RIGHT, radians=2 * PI, about_point=axes.c2p(0, 0, 0)), e.animate.set_value(2 * PI), run_time=6, rate_func=linear, ) self.stop_ambient_camera_rotation() self.wait()
README.md exists but content is empty. Use the Edit dataset card button to edit it.
Downloads last month
36
Edit dataset card