1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| from manim import *
def config_frame(width, height, dpi: int=300, frame_rate: int=30): CM_PER_INCH = 2.54 from math import ceil config.frame_rate = frame_rate dpc = dpi / CM_PER_INCH config.frame_width, config.frame_height = width, height config.pixel_width, config.pixel_height = map(ceil, [config.frame_width * dpc, config.frame_height * dpc]) if config.pixel_width & 1: config.pixel_width += 1 if config.pixel_height & 1: config.pixel_height += 1 class AVLTreeLL(Scene): config.format = "gif" config_frame(width=5, height=5, dpi=500, frame_rate=60) config.write_all = True def construct(self): self.next_section("LL", skip_animations=False) vertices = [2, 1, 0] edges = [(2, 1), (1, 0)] lt = {2: [0, 0, 0], 1: [-1, -1, 0], 0: [-2, -2, 0]} G = Graph(vertices, edges, layout=lt, labels=True) self.play(Create(G)) self.play(G.animate.center()) self.wait(1) self.play( G[2].animate.move_to([1, -1, 0]), G[1].animate.move_to([0, 0, 0]), G[0].animate.move_to([-1, -1, 0]) ) self.wait(1) self.play(FadeOut(G)) class AVLTreeLR(Scene): def construct(self): self.next_section("LR", skip_animations=False) vertices = [2, 0, 1] edges = [(2, 0), (0, 1)] lt = {2: [0, 0, 0], 0: [-1, -1, 0], 1: [0, -2, 0]} G = Graph(vertices, edges, layout=lt, labels=True) self.play(Create(G))
self.wait(1) self.play( G.animate.remove_edges((2, 0)), G.animate.add_edges((2, 1))) self.play( G[0].animate.move_to([-2, -2, 0]), G[1].animate.move_to([-1, -1, 0]) ) self.play(G.animate.center()) self.play( G[2].animate.move_to([1, -1, 0]), G[0].animate.move_to([-1, -1, 0]), G[1].animate.move_to([0, 0, 0]) ) self.wait(1) self.play(FadeOut(G)) class AVLTreeRR(Scene): def construct(self): self.next_section("RR", skip_animations=False) vertices = [0, 1, 2] edges = [(0, 1), (1, 2)] lt = {0: [0, 0, 0], 1: [1, -1, 0], 2: [2, -2, 0]} G = Graph(vertices, edges, layout=lt, labels=True) self.play(Create(G)) self.play(G.animate.center()) self.wait(1) self.play( G[0].animate.move_to([-1, -1, 0]), G[1].animate.move_to([0, 0, 0]), G[2].animate.move_to([1, -1, 0]) ) self.wait(1) self.play(FadeOut(G)) class AVLTreeRL(Scene): def construct(self): self.next_section("RL", skip_animations=False) vertices = [0, 2, 1] edges = [(0, 2), (2, 1)] lt = {0: [0, 0, 0], 2: [1, -1, 0], 1: [0, -2, 0]} G = Graph(vertices, edges, layout=lt, labels=True) self.play(Create(G)) self.wait(1) self.play( G.animate.remove_edges((0, 2)), G.animate.add_edges((0, 1)))
self.play( G[2].animate.move_to([2, -2, 0]), G[1].animate.move_to([1, -1, 0]) ) self.play(G.animate.center()) self.play( G[0].animate.move_to([-1, -1, 0]), G[2].animate.move_to([1, -1, 0]), G[1].animate.move_to([0, 0, 0]) ) self.wait(1) self.play(FadeOut(G))
|