3b1b-manim/hilbert/section1.py

223 lines
5.8 KiB
Python
Raw Normal View History

2015-12-13 15:42:20 -08:00
from mobject import Mobject, Point
2015-12-16 19:50:32 -08:00
from mobject.tex_mobject import TexMobject, TextMobject, Brace
2015-12-15 11:31:19 -08:00
from mobject.image_mobject import ImageMobject
2015-12-13 15:42:20 -08:00
from scene import Scene
2015-12-15 11:31:19 -08:00
from animation import Animation
2015-12-13 15:42:20 -08:00
from animation.transform import Transform, CounterclockwiseTransform, ApplyMethod
from animation.simple_animations import ShowCreation, ShimmerIn
2015-12-15 11:31:19 -08:00
from animation.meta_animations import DelayByOrder, TransformAnimations
from animation.playground import VibratingString
2015-12-13 15:42:20 -08:00
from topics.geometry import Line
from topics.characters import ThoughtBubble
from helpers import *
from hilbert.curves import TransformOverIncreasingOrders, FlowSnake
2015-12-16 19:50:32 -08:00
2015-12-13 15:42:20 -08:00
class AboutSpaceFillingCurves(TransformOverIncreasingOrders):
@staticmethod
def args_to_string():
return ""
@staticmethod
def string_to_args(arg_str):
return ()
def construct(self):
self.bubble = ThoughtBubble().ingest_sub_mobjects()
self.bubble.scale(1.5)
2015-12-15 11:31:19 -08:00
TransformOverIncreasingOrders.construct(self, FlowSnake, 7)
2015-12-13 15:42:20 -08:00
self.play(Transform(self.curve, self.bubble))
self.show_infinite_objects()
self.pose_question()
self.dither()
def show_infinite_objects(self):
sigma, summand, equals, result = TexMobject([
"\\sum_{n = 1}^{\\infty}",
"\\dfrac{1}{n^2}",
"=",
"\\dfrac{\pi^2}{6}"
]).split()
alt_summand = TexMobject("n").replace(summand)
alt_result = TexMobject("-\\dfrac{1}{12}").replace(result)
rationals, other_equals, naturals = TexMobject([
"|\\mathds{Q}|",
"=",
"|\\mathds{N}|"
]).scale(2).split()
infinity = TexMobject("\\infty").scale(2)
local_mobjects = filter(
lambda m : isinstance(m, Mobject),
locals().values(),
)
for mob in local_mobjects:
mob.sort_points(np.linalg.norm)
self.play(ShimmerIn(infinity))
self.dither()
self.play(
ShimmerIn(summand),
ShimmerIn(equals),
ShimmerIn(result),
DelayByOrder(Transform(infinity, sigma))
)
self.dither()
self.play(
Transform(summand, alt_summand),
Transform(result, alt_result),
)
self.dither()
self.remove(infinity)
self.play(*[
CounterclockwiseTransform(
Mobject(summand, equals, result, sigma),
Mobject(rationals, other_equals, naturals)
)
])
self.dither()
self.clear()
self.add(self.bubble)
def pose_question(self):
infinity, rightarrow, N = TexMobject([
"\\infty", "\\rightarrow", "N"
]).scale(2).split()
question_mark = TextMobject("?").scale(2)
self.add(question_mark)
self.dither()
self.play(*[
ShimmerIn(mob)
for mob in infinity, rightarrow, N
] + [
ApplyMethod(question_mark.next_to, rightarrow, UP),
])
self.dither()
class PostponePhilosophizing(Scene):
def construct(self):
2015-12-15 11:31:19 -08:00
abstract, arrow, concrete = TextMobject([
"Abstract", " $\\rightarrow$ ", "Concrete"
]).scale(2).split()
2015-12-13 15:42:20 -08:00
2015-12-15 11:31:19 -08:00
self.add(abstract, arrow, concrete)
self.dither()
self.play(*[
ApplyMethod(
word1.replace, word2,
interpolation_function = path_along_arc(np.pi/2)
)
for word1, word2 in it.permutations([abstract, concrete])
])
self.dither()
2015-12-13 15:42:20 -08:00
2015-12-15 11:31:19 -08:00
class WriteSomeSoftware(Scene):
pass #Done viea screen capture, written here for organization
2015-12-13 15:42:20 -08:00
2015-12-15 11:31:19 -08:00
class ImageToSound(Scene):
def construct(self):
string = VibratingString(color = BLUE_D, run_time = 5)
picture = ImageMobject("lion", invert = False)
2015-12-16 19:50:32 -08:00
picture.scale(0.8)
picture_copy = picture.copy()
2015-12-15 11:31:19 -08:00
picture.sort_points(np.linalg.norm)
string.mobject.sort_points(lambda p : -np.linalg.norm(p))
self.add(picture)
self.dither()
self.play(Transform(
picture, string.mobject,
run_time = 3,
alpha_func = rush_into
))
self.remove(picture)
self.play(string)
2015-12-16 19:50:32 -08:00
for mob in picture_copy, string.mobject:
mob.sort_points(lambda p : np.linalg.norm(p)%1)
self.play(Transform(
string.mobject, picture_copy,
run_time = 5,
alpha_func = rush_from
))
class ImageDataIsTwoDimensional(Scene):
def construct(self):
image = ImageMobject("lion", invert = False)
image.scale(0.5)
image.shift(2*LEFT)
self.add(image)
for vect, num in zip([DOWN, RIGHT], [1, 2]):
brace = Brace(image, vect)
words_mob = TextMobject("Dimension %d"%num)
words_mob.next_to(image, vect, buff = 1)
self.play(
Transform(Point(brace.get_center()), brace),
ShimmerIn(words_mob),
run_time = 2
)
self.dither()
class SoundDataIsOneDimensional(Scene):
def construct(self):
overtones = 5
main_string = VibratingString(color = BLUE_D)
component_strings = [
VibratingString(
num_periods = k+1,
color = color,
center = 2*DOWN + UP*k
)
for k, color in zip(
range(overtones),
Color(BLUE_E).range_to(WHITE, overtones)
)
]
self.play(main_string)
self.remove(main_string.mobject)
self.play(*[
TransformAnimations(
main_string.copy(),
string
)
for string in component_strings
])
2015-12-15 11:31:19 -08:00
2015-12-13 15:42:20 -08:00