diff --git a/.gitignore b/.gitignore index 998218d..a3fe1ae 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# -----> manim +media/ + # ---> Python # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/binary-search/binary-search-manim.py b/binary-search/binary-search-manim.py new file mode 100644 index 0000000..8f383de --- /dev/null +++ b/binary-search/binary-search-manim.py @@ -0,0 +1,112 @@ +from manim import * + + +class Main(Scene): + def construct(self): + n = 13 + k = 8 + + def check(x): + return 1 if x <= k else 0 + + # initial situation + seq = Matrix([[1] + ["?" for _ in range(n-1)] + [0]], h_buff=1.0).elements.move_to(0.5*UP) + self.play(Create(seq)) + + # source code binary search + + # while(high - low > 1){ // line_while + # int mid = (low + high) / 2; // line_mid + # if(check(mid)) // line_check + # low = mid; // line_low + # else + # high = mid; // line_high + # } + code = Code(file_name="binary-search.cpp", language="c++", insert_line_no=False, style='monokai', tab_width=4, line_spacing=0.5).code + code.move_to(4*RIGHT + 2.5*DOWN) + while_line, mid_line, check_line, low_line, __, high_line, __ = code + + # check code + check_code = Code(code="check(x)", language="c++", insert_line_no=False, style='monokai', tab_width=4, line_spacing=0.5).code + check_background, check_code = check_code.move_to(2.5*UP).add_background_rectangle(opacity=1.0) + self.play(Create(check_background), Write(check_code), Write(code)) + + def pointer(name): + arrow = Arrow(start=ORIGIN, end=UP) + label = Tex(name).next_to(arrow, DOWN) + return VGroup(arrow, label).scale(0.7) + + # initialize borders + low = 0 + high = n + low_pointer = pointer("low").next_to(seq[low], DOWN) + high_pointer = pointer("high").next_to(seq[high], DOWN) + self.play(Write(low_pointer), Create(high_pointer)) + + mid_pointer = pointer("mid") + + def indicate_start(line): + line.save_state() + self.play(line.animate.scale(1.3).set_color(YELLOW)) + + def indicate_end(line): + self.play(Restore(line)) + + def high_low_test(): + indicate_start(while_line) + brace = BraceText(VGroup(*seq[low:high]), "high - low = " + str(high - low), brace_direction=UP) + brace.label.scale(0.7) + self.play(FadeIn(brace)) + self.wait(1) + self.play(FadeOut(brace)) + indicate_end(while_line) + + while high_low_test() or high - low > 1: + + # calculate mid + indicate_start(mid_line) + mid = (low + high) // 2 + mid_pointer.next_to(seq[mid], DOWN) + self.play(Write(mid_pointer)) + indicate_end(mid_line) + + # check + indicate_start(check_line) + + seq[mid].add_background_rectangle(opacity=1.0) + path = Line(start=seq[mid].get_center(), end=check_background.get_center()) + rpath = Line(start=path.get_end(), end=path.get_start()) + laser = path.copy().set_length(0.3) + + self.bring_to_back(laser) + self.play(MoveAlongPath(laser, path)) + + color = GREEN if (ans := check(mid)) else RED + self.play(Indicate(check_code, color=color)) + + self.bring_to_back(laser) + self.play(MoveAlongPath(laser, rpath)) + self.remove(laser) + + # TODO: mark tested + range_seq = seq[low+1:mid+1] if ans else seq[mid:high] + self.play(*[Transform(s, Tex(ans).move_to(s)) for s in range_seq]) + + indicate_end(check_line) + + if check(mid): + # update low + indicate_start(low_line) + low = mid + low_pointer.generate_target().move_to(mid_pointer) + self.play(FadeOut(mid_pointer), MoveToTarget(low_pointer)) + indicate_end(low_line) + else: + # update high + indicate_start(high_line) + high = mid + high_pointer.generate_target().move_to(mid_pointer) + self.play(FadeOut(mid_pointer), MoveToTarget(high_pointer)) + indicate_end(high_line) + + self.wait(2) diff --git a/binary-search/binary-search.cpp b/binary-search/binary-search.cpp new file mode 100644 index 0000000..140ad9f --- /dev/null +++ b/binary-search/binary-search.cpp @@ -0,0 +1,7 @@ +while(high - low > 1){ + int mid = (low + high) / 2; + if(check(mid)) + low = mid; + else + high = mid; +} diff --git a/manim-resources.org b/manim-resources.org new file mode 100644 index 0000000..4fd0d81 --- /dev/null +++ b/manim-resources.org @@ -0,0 +1,51 @@ +* Commands +=$ manim -pql scene.py SquareToCircle= + +** -n, --from_animation_number TEXT +Start rendering from n_0 until n_1. If n_1 +is left unspecified, renders all scenes +after n_0. + + +* Transform vs ReplacementTransform +[[https://www.reddit.com/r/manim/comments/bq5bk2/manim_tutorial_difference_between_transform_and/]] + + +* updater +useful to connecting animations? +[[https://docs.manim.community/en/stable/reference/manim.mobject.mobject.Mobject.html?highlight=add_updater#manim.mobject.mobject.Mobject.add_updater]] + + +* Value Tracker (e.g. for pointers) +[[https://docs.manim.community/en/stable/reference/manim.mobject.value_tracker.ValueTracker.html#manim.mobject.value_tracker.ValueTracker]] + + +* Matrix +maybe implement sequences and arrays with this? +[[https://docs.manim.community/en/stable/reference/manim.mobject.matrix.MobjectMatrix.html#manim.mobject.matrix.MobjectMatrix]] + + +* Indication +[[https://docs.manim.community/en/stable/reference/manim.animation.indication.html]] + + +* Graphs +[[https://docs.manim.community/en/stable/reference/manim.mobject.graph.Graph.html#manim.mobject.graph.Graph]] + + +* Animations +[[https://azarzadavila-manim.readthedocs.io/en/latest/animation.html]] + + +* [[https://eulertour.com/][Online Editor]] + + +* Examples +** [[https://www.youtube.com/channel/UCVefHPaA35CQUzBYZLDXoxQ/videos][Matterhorn - YouTube]] +** [[https://github.com/mitkonikov/manim-code][Manim Extension for Code Explanatory Videos]] +** [[https://manim.flipdot.org/detail/02_animations/custom_VMobject/][VMobject - Flipdot]] + +** https://github.com/Elteoremadebeethoven/ManimCE-changes/blob/main/mce-new-features.py + +* Reference Manual +[[https://docs.manim.community/en/stable/reference.html]]