Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Final animation for Hilbert, I think

Grant Sanderson5ae6e8eb542e
diff --git a/hilbert/curves.py b/hilbert/curves.py
index 8d7011f94..fa3e015b5 100644
--- a/hilbert/curves.py
+++ b/hilbert/curves.py
@@ -261,6 +261,25 @@ class Sierpinski(LindenmayerCurve):
"angle" : -np.pi/3,
}
+class KochCurve(LindenmayerCurve):
+ DEFAULT_CONFIG = {
+ "start_color" : BLUE_D,
+ "end_color" : WHITE,
+ "axiom" : "A--A--A--",
+ "rule" : {
+ "A" : "A+A--A+A"
+ },
+ "radius" : 4,
+ "scale_factor" : 3,
+ "start_step" : RIGHT,
+ "angle" : np.pi/3
+ }
+
+ def __init__(self, **kwargs):
+ digest_config(self, kwargs)
+ self.scale_factor = 2*(1+np.cos(self.angle))
+ LindenmayerCurve.__init__(self, **kwargs)
+
class StellarCurve(LindenmayerCurve):
DEFAULT_CONFIG = {
@@ -319,21 +338,31 @@ class SpaceFillingCurveScene(Scene):
return CurveClass, int(order_str)
class TransformOverIncreasingOrders(SpaceFillingCurveScene):
- def construct(self, CurveClass, max_order):
+ def setup(self, CurveClass):
sample = CurveClass(order = 1)
- self.curve = Line(sample.radius*LEFT, sample.radius*RIGHT)
+ self.curve = Line(3*LEFT, 3*RIGHT)
self.curve.gradient_highlight(
sample.start_color,
sample.end_color
)
- for order in range(1, max_order):
- new_curve = CurveClass(order = order)
- self.play(
- Transform(self.curve, new_curve),
- run_time = 3/np.sqrt(order),
- )
+ self.CurveClass = CurveClass
+ self.order = 0
+
+ def construct(self, CurveClass, max_order):
+ self.setup(CurveClass)
+ while self.order < max_order:
+ self.increase_order()
self.dither()
+ def increase_order(self, *other_anims):
+ self.order += 1
+ new_curve = self.CurveClass(order = self.order)
+ self.play(
+ Transform(self.curve, new_curve),
+ *other_anims,
+ run_time = 3/np.sqrt(self.order)
+ )
+
class DrawSpaceFillingCurve(SpaceFillingCurveScene):
def construct(self, CurveClass, order):
diff --git a/hilbert/fractal_porn.py b/hilbert/fractal_porn.py
new file mode 100644
index 000000000..ee9c27be5
--- /dev/null
+++ b/hilbert/fractal_porn.py
@@ -0,0 +1,347 @@
+from mobject import Mobject, Point, Mobject1D
+from scene import Scene
+from animation.transform import \
+ Transform, ShimmerIn, FadeIn, FadeOut, ApplyMethod
+from animation.simple_animations import \
+ ShowCreation, DelayByOrder
+from topics.geometry import Line, Arc, Arrow
+from mobject.tex_mobject import TexMobject, TextMobject
+
+from helpers import *
+from hilbert.curves import *
+
+class Intro(TransformOverIncreasingOrders):
+ @staticmethod
+ def args_to_string(*args):
+ return ""
+
+ @staticmethod
+ def string_to_args(string):
+ raise Exception("string_to_args Not Implemented!")
+
+ def construct(self):
+ words1 = TextMobject(
+ "If you watched my video about Hilbert's space-filling curve\\dots"
+ )
+ words2 = TextMobject(
+ "\\dots you might be curious to see what a few other space-filling curves look like."
+ )
+ words2.scale(0.8)
+ for words in words1, words2:
+ words.to_edge(UP, buff = 0.2)
+
+ self.setup(HilbertCurve)
+ self.play(ShimmerIn(words1))
+ for x in range(4):
+ self.increase_order()
+ self.remove(words1)
+ self.increase_order(
+ ShimmerIn(words2)
+ )
+ for x in range(4):
+ self.increase_order()
+
+
+
+class BringInPeano(Intro):
+ def construct(self):
+ words1 = TextMobject("""
+ For each one, see if you can figure out what
+ the pattern of construction is.
+ """)
+ words2 = TextMobject("""
+ This one is the Peano curve.
+ """)
+ words3 = TextMobject("""
+ It is the original space-filling curve.
+ """)
+ self.setup(PeanoCurve)
+ self.play(ShimmerIn(words1))
+ self.dither(5)
+ self.remove(words1)
+ self.add(words2.to_edge(UP))
+ for x in range(3):
+ self.increase_order()
+ self.remove(words2)
+ self.increase_order(ShimmerIn(words3.to_edge(UP)))
+ for x in range(2):
+ self.increase_order()
+
+
+class FillOtherShapes(Intro):
+ def construct(self):
+ words1 = TextMobject("""
+ But of course, there's no reason we should limit
+ ourselves to filling in squares.
+ """)
+ words2 = TextMobject("""
+ Here's a simple triangle-filling curve I defined
+ in a style reflective of a Hilbert curve.
+ """)
+ words1.to_edge(UP)
+ words2.scale(0.8).to_edge(UP, buff = 0.2)
+
+ self.setup(TriangleFillingCurve)
+ self.play(ShimmerIn(words1))
+ for x in range(3):
+ self.increase_order()
+ self.remove(words1)
+ self.add(words2)
+ for x in range(5):
+ self.increase_order()
+
+class SmallerFlowSnake(FlowSnake):
+ DEFAULT_CONFIG = {
+ "radius" : 4
+ }
+
+class MostDelightfulName(Intro):
+ def construct(self):
+ words1 = TextMobject("""
+ This one has the most delightful name,
+ thanks to mathematician/programmer Bill Gosper:
+ """)
+ words2 = TextMobject("``Flow Snake''")
+ words3 = TextMobject("""
+ What makes this one particularly interesting
+ is that the boundary itself is a fractal.
+ """)
+ for words in words1, words2, words3:
+ words.to_edge(UP)
+
+ self.setup(SmallerFlowSnake)
+ self.play(ShimmerIn(words1))
+ for x in range(3):
+ self.increase_order()
+ self.remove(words1)
+ self.add(words2)
+ for x in range(3):
+ self.increase_order()
+ self.remove(words2)
+ self.play(ShimmerIn(words3))
+
+
+
+class SurpriseFractal(Intro):
+ def construct(self):
+ words = TextMobject("""
+ It might come as a surprise how some well-known
+ fractals can be described with curves.
+ """)
+ words.to_edge(UP)
+
+ self.setup(Sierpinski)
+ self.add(TextMobject("Speaking of other fractals\\dots"))
+ self.dither(3)
+ self.clear()
+ self.play(ShimmerIn(words))
+ for x in range(9):
+ self.increase_order()
+
+
+class IntroduceKoch(Intro):
+ def construct(self):
+ words = map(TextMobject, [
+ "This is another famous fractal.",
+ "The ``Koch Snowflake''",
+ "Let's finish things off by seeing how to turn \
+ this into a space-filling curve"
+ ])
+ for text in words:
+ text.to_edge(UP)
+
+ self.setup(KochCurve)
+ self.add(words[0])
+ for x in range(3):
+ self.increase_order()
+ self.remove(words[0])
+ self.add(words[1])
+ for x in range(4):
+ self.increase_order()
+ self.remove(words[1])
+ self.add(words[2])
+ self.dither(6)
+
+class StraightKoch(KochCurve):
+ DEFAULT_CONFIG = {
+ "axiom" : "A"
+ }
+
+class SharperKoch(StraightKoch):
+ DEFAULT_CONFIG = {
+ "angle" : 0.9*np.pi/2,
+ }
+
+class DullerKoch(StraightKoch):
+ DEFAULT_CONFIG = {
+ "angle" : np.pi/6,
+ }
+
+class SpaceFillingKoch(StraightKoch):
+ DEFAULT_CONFIG = {
+ "angle" : np.pi/2,
+ }
+
+
+
+class FromKochToSpaceFilling(Scene):
+ def construct(self):
+ self.max_order = 7
+
+ self.revisit_koch()
+ self.show_angles()
+ self.show_change_side_by_side()
+
+
+ def revisit_koch(self):
+ words = map(TextMobject, [
+ "First, look at how one section of this curve is made.",
+ "This pattern of four lines is the ``seed''",
+ "With each iteration, every straight line is \
+ replaced with an appropriately small copy of the seed",
+ ])
+ for text in words:
+ text.to_edge(UP)
+
+ self.add(words[0])
+ curve = StraightKoch(order = self.max_order)
+ self.play(Transform(
+ curve,
+ StraightKoch(order = 1),
+ run_time = 5
+ ))
+ self.remove(words[0])
+ self.add(words[1])
+ self.dither(4)
+ self.remove(words[1])
+ self.add(words[2])
+ self.dither(3)
+ for order in range(2, self.max_order):
+ self.play(Transform(
+ curve,
+ StraightKoch(order = order)
+ ))
+ if order == 2:
+ self.dither(2)
+ elif order == 3:
+ self.dither()
+ self.clear()
+
+
+
+ def show_angles(self):
+ words = TextMobject("""
+ Let's see what happens as we change
+ the angle in this seed
+ """)
+ words.to_edge(UP)
+ koch, sharper_koch, duller_koch = curves = [
+ CurveClass(order = 1)
+ for CurveClass in StraightKoch, SharperKoch, DullerKoch
+ ]
+ arcs = [
+ Arc(
+ 2*(np.pi/2 - curve.angle),
+ radius = r,
+ start_angle = np.pi+curve.angle
+ ).shift(curve.points[curve.get_num_points()/2])
+ for curve, r in zip(curves, [0.6, 0.7, 0.4])
+ ]
+ theta = TexMobject("\\theta")
+ theta.shift(arcs[0].get_center()+2.5*DOWN)
+ arrow = Arrow(theta, arcs[0])
+
+ self.add(words, koch)
+ self.play(ShowCreation(arcs[0]))
+ self.play(
+ ShowCreation(arrow),
+ ShimmerIn(theta)
+ )
+ self.dither(2)
+ self.remove(theta, arrow)
+ self.play(
+ Transform(koch, duller_koch),
+ Transform(arcs[0], arcs[2]),
+ )
+ self.play(
+ Transform(koch, sharper_koch),
+ Transform(arcs[0], arcs[1]),
+ )
+ self.clear()
+
+ def show_change_side_by_side(self):
+
+ seed = TextMobject("Seed")
+ seed.shift(3*LEFT+2*DOWN)
+ fractal = TextMobject("Fractal")
+ fractal.shift(3*RIGHT+2*DOWN)
+ words = map(TextMobject, [
+ "A sharper angle results in a richer curve",
+ "A more obtuse angle gives a sparser curve",
+ "And as the angle approaches 0\\dots",
+ "We have a new space-filling curve."
+ ])
+ for text in words:
+ text.to_edge(UP)
+ sharper, duller, space_filling = [
+ CurveClass(order = 1).shift(3*LEFT)
+ for CurveClass in SharperKoch, DullerKoch, SpaceFillingKoch
+ ]
+ shaper_f, duller_f, space_filling_f = [
+ CurveClass(order = self.max_order).shift(3*RIGHT)
+ for CurveClass in SharperKoch, DullerKoch, SpaceFillingKoch
+ ]
+
+ self.add(words[0])
+ left_curve = SharperKoch(order = 1)
+ right_curve = SharperKoch(order = 1)
+ self.play(
+ Transform(left_curve, sharper),
+ ApplyMethod(right_curve.shift, 3*RIGHT),
+ )
+ self.play(
+ Transform(
+ right_curve,
+ SharperKoch(order = 2).shift(3*RIGHT)
+ ),
+ ShimmerIn(seed),
+ ShimmerIn(fractal)
+ )
+ for order in range(3, self.max_order):
+ self.play(Transform(
+ right_curve,
+ SharperKoch(order = order).shift(3*RIGHT)
+ ))
+ self.remove(words[0])
+ self.add(words[1])
+ kwargs = {
+ "run_time" : 4,
+ }
+ self.play(
+ Transform(left_curve, duller, **kwargs),
+ Transform(right_curve, duller_f, **kwargs)
+ )
+ self.dither()
+ kwargs["run_time"] = 7
+ kwargs["rate_func"] = None
+ self.remove(words[1])
+ self.add(words[2])
+ self.play(
+ Transform(left_curve, space_filling, **kwargs),
+ Transform(right_curve, space_filling_f, **kwargs)
+ )
+ self.remove(words[2])
+ self.add(words[3])
+ self.dither()
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/hilbert/section1.py b/hilbert/section1.py
index 54dd27f25..26b22bcbf 100644
--- a/hilbert/section1.py
+++ b/hilbert/section1.py
@@ -18,7 +18,7 @@ from animation.playground import Vibrate
from topics.geometry import \
Line, Dot, Arrow, Grid, Square, Point
from topics.characters import \
- ThoughtBubble, SpeechBubble, Mathematician
+ ThoughtBubble, SpeechBubble, Mathematician, Randolph
from topics.number_line import UnitInterval
from topics.three_dimensions import Stars
@@ -151,6 +151,23 @@ class PostponePhilosophizing(Scene):
self.dither()
+class GrowHilbertWithName(Scene):
+ def construct(self):
+ curve = HilbertCurve(order = 1)
+ words = TextMobject("``Hilbert Curve''")
+ words.to_edge(UP, buff = 0.2)
+ self.play(
+ ShimmerIn(words),
+ Transform(curve, HilbertCurve(order = 2)),
+ run_time = 2
+ )
+ for n in range(3, 8):
+ self.play(
+ Transform(curve, HilbertCurve(order = n)),
+ run_time = 5. /n
+ )
+
+
class SectionOne(Scene):
def construct(self):
self.add(TextMobject("Section 1: Seeing with your ears"))
@@ -189,6 +206,16 @@ class ImageToSound(Scene):
rate_func = rush_from
))
+class LinksInDescription(Scene):
+ def construct(self):
+ text = TextMobject("""
+ See links in the description for more on
+ sight via sound.
+ """)
+ self.play(ShimmerIn(text))
+ self.play(ShowCreation(Arrow(text, 3*DOWN)))
+ self.dither(2)
+
class ImageDataIsTwoDimensional(Scene):
def construct(self):
@@ -923,6 +950,22 @@ class ImagineItWorks(Scene):
self.add(TextMobject("Image your project succeeds..."))
self.dither()
+
+class RandyWithHeadphones(Scene):
+ def construct(self):
+ headphones = ImageMobject("Headphones.png")
+ headphones.scale(0.1)
+ headphones.stretch(2, 0)
+ headphones.shift(1.2*UP+0.05*LEFT)
+ headphones.highlight(GREY)
+ randy = Randolph()
+
+ self.add(randy, headphones)
+ self.dither(2)
+ self.play(ApplyMethod(randy.blink))
+ self.dither(4)
+
+
class IncreaseResolution(Scene):
def construct(self):
grids = [
@@ -1027,7 +1070,7 @@ class TrackSpecificSnakeCurvePoint(TrackSpecificCurvePoint):
class NeedToRelearn(Scene):
def construct(self):
top_words = TextMobject("Different pixel-frequency association")
- bottom_words = TextMobject("Need to relearn synesthesia")
+ bottom_words = TextMobject("Need to relearn sight-via-sound")
top_words.shift(UP)
bottom_words.shift(DOWN)
arrow = Arrow(top_words, bottom_words)
diff --git a/hilbert/section2.py b/hilbert/section2.py
index fa8f1f71b..5057be715 100644
--- a/hilbert/section2.py
+++ b/hilbert/section2.py
@@ -699,7 +699,12 @@ class FormalDefinitionOfContinuity(Scene):
point_description = TextMobject(
"Point where the function jumps"
)
- point_description.shift(3*RIGHT)
+ point_description.shift(3*RIGHT)
+ discontinuous_at_A = TextMobject(
+ "``Discontinuous at A''",
+ size = "\\Large"
+ )
+ discontinuous_at_A.shift(2*UP).to_edge(LEFT)
text = TextMobject("""
Circle around ouput \\\\
points can never \\\\
@@ -724,6 +729,7 @@ class FormalDefinitionOfContinuity(Scene):
Homotopy(self.input_homotopy, self.input_dot, **kwargs),
Homotopy(self.output_homotopy, self.output_dot, **kwargs)
)
+ discontinuous_arrow = Arrow(discontinuous_at_A, self.input_dot)
arrow = Arrow(
point_description, self.output_dot,
buff = 0.05,
@@ -754,7 +760,12 @@ class FormalDefinitionOfContinuity(Scene):
self.remove(input_circle, input_points, output_circle, input_points_copy)
self.play(vary_circles)
self.dither()
- self.remove(vary_circles.mobject)
+ self.play(
+ ShimmerIn(discontinuous_at_A),
+ ShowCreation(discontinuous_arrow)
+ )
+ self.dither(3)
+ self.remove(vary_circles.mobject, discontinuous_at_A, discontinuous_arrow)
def continuous_point(self):
pass
diff --git a/topics/characters.py b/topics/characters.py
index 6ef69245f..e8f7432a1 100644
--- a/topics/characters.py
+++ b/topics/characters.py
@@ -164,7 +164,7 @@ class Randolph(PiCreature):
class Mortimer(PiCreature):
DEFAULT_CONFIG = {
- "color" : DARK_BROWN
+ "color" : MAROON_E
}
def __init__(self, **kwargs):
PiCreature.__init__(self, **kwargs)
diff --git a/topics/number_line.py b/topics/number_line.py
index 0fcbf1216..558bba49d 100644
--- a/topics/number_line.py
+++ b/topics/number_line.py
@@ -228,9 +228,8 @@ class XYZAxes(Mobject1D):
)
self.y_axis = self.x_axis.copy().rotate(np.pi/2, OUT)
self.z_axis = self.x_axis.copy().rotate(np.pi/2, DOWN)
-
self.digest_mobject_attrs()
- self.pose_at_angle()
+
class SpaceGrid(Mobject1D):