2018-07-31 19:39:41 -07:00
|
|
|
import itertools as it
|
2018-03-31 18:05:02 -07:00
|
|
|
|
2018-12-24 12:37:51 -08:00
|
|
|
from manimlib.animation.animation import Animation
|
|
|
|
from manimlib.animation.rotation import Rotating
|
|
|
|
from manimlib.constants import *
|
|
|
|
from manimlib.mobject.geometry import AnnularSector
|
|
|
|
from manimlib.mobject.geometry import Arc
|
|
|
|
from manimlib.mobject.geometry import Circle
|
|
|
|
from manimlib.mobject.geometry import Line
|
|
|
|
from manimlib.mobject.geometry import Polygon
|
|
|
|
from manimlib.mobject.geometry import Rectangle
|
|
|
|
from manimlib.mobject.geometry import Square
|
|
|
|
from manimlib.mobject.mobject import Mobject
|
|
|
|
from manimlib.mobject.svg.svg_mobject import SVGMobject
|
|
|
|
from manimlib.mobject.svg.tex_mobject import TexMobject
|
|
|
|
from manimlib.mobject.svg.tex_mobject import TextMobject
|
|
|
|
from manimlib.mobject.three_dimensions import Cube
|
|
|
|
from manimlib.mobject.types.vectorized_mobject import VGroup
|
|
|
|
from manimlib.mobject.types.vectorized_mobject import VMobject
|
|
|
|
from manimlib.mobject.types.vectorized_mobject import VectorizedPoint
|
|
|
|
from manimlib.utils.bezier import interpolate
|
|
|
|
from manimlib.utils.config_ops import digest_config
|
2019-02-05 15:39:58 -08:00
|
|
|
from manimlib.utils.rate_functions import linear
|
2018-12-24 12:37:51 -08:00
|
|
|
from manimlib.utils.space_ops import angle_of_vector
|
|
|
|
from manimlib.utils.space_ops import complex_to_R3
|
|
|
|
from manimlib.utils.space_ops import rotate_vector
|
2016-11-07 11:05:41 -08:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2017-06-30 15:41:45 -07:00
|
|
|
class Lightbulb(SVGMobject):
|
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"file_name": "lightbulb",
|
|
|
|
"height": 1,
|
|
|
|
"stroke_color": YELLOW,
|
|
|
|
"stroke_width": 3,
|
|
|
|
"fill_color": YELLOW,
|
|
|
|
"fill_opacity": 0,
|
2017-06-30 15:41:45 -07:00
|
|
|
}
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2017-06-20 14:05:48 -07:00
|
|
|
class BitcoinLogo(SVGMobject):
|
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"file_name": "Bitcoin_logo",
|
|
|
|
"height": 1,
|
|
|
|
"fill_color": "#f7931a",
|
|
|
|
"inner_color": WHITE,
|
|
|
|
"fill_opacity": 1,
|
|
|
|
"stroke_width": 0,
|
2017-06-20 14:05:48 -07:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2017-06-20 14:05:48 -07:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
SVGMobject.__init__(self, **kwargs)
|
|
|
|
self[0].set_fill(self.fill_color, self.fill_opacity)
|
|
|
|
self[1].set_fill(self.inner_color, 1)
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2017-06-08 21:56:53 -07:00
|
|
|
class Guitar(SVGMobject):
|
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"file_name": "guitar",
|
|
|
|
"height": 2.5,
|
|
|
|
"fill_color": DARK_GREY,
|
|
|
|
"fill_opacity": 1,
|
|
|
|
"stroke_color": WHITE,
|
|
|
|
"stroke_width": 0.5,
|
2017-06-08 21:56:53 -07:00
|
|
|
}
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2017-06-07 14:34:39 -07:00
|
|
|
class SunGlasses(SVGMobject):
|
2017-06-05 12:47:03 -07:00
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"file_name": "sunglasses",
|
|
|
|
"glasses_width_to_eyes_width": 1.1,
|
2017-06-05 12:47:03 -07:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2017-06-07 14:34:39 -07:00
|
|
|
def __init__(self, pi_creature, **kwargs):
|
|
|
|
SVGMobject.__init__(self, **kwargs)
|
2018-04-06 13:58:59 -07:00
|
|
|
self.set_stroke(WHITE, width=0)
|
2017-06-07 14:34:39 -07:00
|
|
|
self.set_fill(GREY, 1)
|
2018-08-08 10:30:52 -07:00
|
|
|
self.set_width(
|
2018-04-06 13:58:59 -07:00
|
|
|
self.glasses_width_to_eyes_width * pi_creature.eyes.get_width()
|
2017-06-05 12:47:03 -07:00
|
|
|
)
|
2017-06-07 14:34:39 -07:00
|
|
|
self.move_to(pi_creature.eyes, UP)
|
2017-06-05 12:47:03 -07:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2017-04-12 09:06:04 -07:00
|
|
|
class Speedometer(VMobject):
|
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"arc_angle": 4 * np.pi / 3,
|
|
|
|
"num_ticks": 8,
|
|
|
|
"tick_length": 0.2,
|
|
|
|
"needle_width": 0.1,
|
|
|
|
"needle_height": 0.8,
|
|
|
|
"needle_color": YELLOW,
|
2017-04-12 09:06:04 -07:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2017-04-12 09:06:04 -07:00
|
|
|
def generate_points(self):
|
2018-04-06 13:58:59 -07:00
|
|
|
start_angle = np.pi / 2 + self.arc_angle / 2
|
|
|
|
end_angle = np.pi / 2 - self.arc_angle / 2
|
2017-04-12 09:06:04 -07:00
|
|
|
self.add(Arc(
|
2018-04-06 13:58:59 -07:00
|
|
|
start_angle=start_angle,
|
|
|
|
angle=-self.arc_angle
|
2017-04-12 09:06:04 -07:00
|
|
|
))
|
|
|
|
tick_angle_range = np.linspace(start_angle, end_angle, self.num_ticks)
|
|
|
|
for index, angle in enumerate(tick_angle_range):
|
|
|
|
vect = rotate_vector(RIGHT, angle)
|
2018-04-06 13:58:59 -07:00
|
|
|
tick = Line((1 - self.tick_length) * vect, vect)
|
|
|
|
label = TexMobject(str(10 * index))
|
2018-08-08 10:30:52 -07:00
|
|
|
label.set_height(self.tick_length)
|
2018-04-06 13:58:59 -07:00
|
|
|
label.shift((1 + self.tick_length) * vect)
|
2017-04-12 09:06:04 -07:00
|
|
|
self.add(tick, label)
|
|
|
|
|
|
|
|
needle = Polygon(
|
|
|
|
LEFT, UP, RIGHT,
|
2018-04-06 13:58:59 -07:00
|
|
|
stroke_width=0,
|
|
|
|
fill_opacity=1,
|
|
|
|
fill_color=self.needle_color
|
2017-04-12 09:06:04 -07:00
|
|
|
)
|
|
|
|
needle.stretch_to_fit_width(self.needle_width)
|
|
|
|
needle.stretch_to_fit_height(self.needle_height)
|
2018-04-06 13:58:59 -07:00
|
|
|
needle.rotate(start_angle - np.pi / 2, about_point=ORIGIN)
|
2017-04-12 09:06:04 -07:00
|
|
|
self.add(needle)
|
|
|
|
self.needle = needle
|
|
|
|
|
|
|
|
self.center_offset = self.get_center()
|
|
|
|
|
|
|
|
def get_center(self):
|
|
|
|
result = VMobject.get_center(self)
|
|
|
|
if hasattr(self, "center_offset"):
|
|
|
|
result -= self.center_offset
|
|
|
|
return result
|
|
|
|
|
|
|
|
def get_needle_tip(self):
|
|
|
|
return self.needle.get_anchors()[1]
|
|
|
|
|
|
|
|
def get_needle_angle(self):
|
|
|
|
return angle_of_vector(
|
|
|
|
self.get_needle_tip() - self.get_center()
|
|
|
|
)
|
|
|
|
|
|
|
|
def rotate_needle(self, angle):
|
2018-04-06 13:58:59 -07:00
|
|
|
self.needle.rotate(angle, about_point=self.get_center())
|
2017-04-12 09:06:04 -07:00
|
|
|
return self
|
|
|
|
|
|
|
|
def move_needle_to_velocity(self, velocity):
|
2018-04-06 13:58:59 -07:00
|
|
|
max_velocity = 10 * (self.num_ticks - 1)
|
2017-04-12 09:06:04 -07:00
|
|
|
proportion = float(velocity) / max_velocity
|
2018-04-06 13:58:59 -07:00
|
|
|
start_angle = np.pi / 2 + self.arc_angle / 2
|
2017-04-12 09:06:04 -07:00
|
|
|
target_angle = start_angle - self.arc_angle * proportion
|
|
|
|
self.rotate_needle(target_angle - self.get_needle_angle())
|
|
|
|
return self
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2017-04-26 16:22:05 -07:00
|
|
|
class AoPSLogo(SVGMobject):
|
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"file_name": "aops_logo",
|
|
|
|
"height": 1.5,
|
2017-04-26 16:22:05 -07:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2017-04-26 16:22:05 -07:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
SVGMobject.__init__(self, **kwargs)
|
2018-04-06 13:58:59 -07:00
|
|
|
self.set_stroke(WHITE, width=0)
|
2017-04-26 16:22:05 -07:00
|
|
|
colors = [BLUE_E, "#008445", GREEN_B]
|
|
|
|
index_lists = [
|
|
|
|
(10, 11, 12, 13, 14, 21, 22, 23, 24, 27, 28, 29, 30),
|
|
|
|
(0, 1, 2, 3, 4, 15, 16, 17, 26),
|
|
|
|
(5, 6, 7, 8, 9, 18, 19, 20, 25)
|
|
|
|
]
|
|
|
|
for color, index_list in zip(colors, index_lists):
|
|
|
|
for i in index_list:
|
2018-04-06 13:58:59 -07:00
|
|
|
self.submobjects[i].set_fill(color, opacity=1)
|
2017-04-26 16:22:05 -07:00
|
|
|
|
2018-08-08 10:30:52 -07:00
|
|
|
self.set_height(self.height)
|
2017-04-26 16:22:05 -07:00
|
|
|
self.center()
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2017-02-23 14:13:30 -08:00
|
|
|
class PartyHat(SVGMobject):
|
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"file_name": "party_hat",
|
|
|
|
"height": 1.5,
|
|
|
|
"pi_creature": None,
|
|
|
|
"stroke_width": 0,
|
|
|
|
"fill_opacity": 1,
|
|
|
|
"frills_colors": [MAROON_B, PURPLE],
|
|
|
|
"cone_color": GREEN,
|
|
|
|
"dots_colors": [YELLOW],
|
2017-02-23 14:13:30 -08:00
|
|
|
}
|
|
|
|
NUM_FRILLS = 7
|
|
|
|
NUM_DOTS = 6
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2017-02-23 14:13:30 -08:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
SVGMobject.__init__(self, **kwargs)
|
2018-08-08 10:30:52 -07:00
|
|
|
self.set_height(self.height)
|
2017-02-23 14:13:30 -08:00
|
|
|
if self.pi_creature is not None:
|
2018-04-06 13:58:59 -07:00
|
|
|
self.next_to(self.pi_creature.eyes, UP, buff=0)
|
2017-02-23 14:13:30 -08:00
|
|
|
|
|
|
|
self.frills = VGroup(*self[:self.NUM_FRILLS])
|
|
|
|
self.cone = self[self.NUM_FRILLS]
|
2018-04-06 13:58:59 -07:00
|
|
|
self.dots = VGroup(*self[self.NUM_FRILLS + 1:])
|
2017-02-23 14:13:30 -08:00
|
|
|
|
2018-03-30 11:59:39 -07:00
|
|
|
self.frills.set_color_by_gradient(*self.frills_colors)
|
2018-03-30 11:51:31 -07:00
|
|
|
self.cone.set_color(self.cone_color)
|
2018-03-30 11:59:39 -07:00
|
|
|
self.dots.set_color_by_gradient(*self.dots_colors)
|
2017-02-23 14:13:30 -08:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2017-02-18 14:02:46 -08:00
|
|
|
class Laptop(VGroup):
|
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"width": 3,
|
|
|
|
"body_dimensions": [4, 3, 0.05],
|
|
|
|
"screen_thickness": 0.01,
|
|
|
|
"keyboard_width_to_body_width": 0.9,
|
|
|
|
"keyboard_height_to_body_height": 0.5,
|
|
|
|
"screen_width_to_screen_plate_width": 0.9,
|
|
|
|
"key_color_kwargs": {
|
|
|
|
"stroke_width": 0,
|
|
|
|
"fill_color": BLACK,
|
|
|
|
"fill_opacity": 1,
|
2017-02-18 14:02:46 -08:00
|
|
|
},
|
2018-04-06 13:58:59 -07:00
|
|
|
"body_color": LIGHT_GREY,
|
|
|
|
"shaded_body_color": GREY,
|
|
|
|
"open_angle": np.pi / 4,
|
2017-02-18 14:02:46 -08:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2017-02-18 14:02:46 -08:00
|
|
|
def generate_points(self):
|
2018-04-06 13:58:59 -07:00
|
|
|
body = Cube(side_length=1)
|
2017-02-18 14:02:46 -08:00
|
|
|
for dim, scale_factor in enumerate(self.body_dimensions):
|
2018-04-06 13:58:59 -07:00
|
|
|
body.stretch(scale_factor, dim=dim)
|
2018-08-08 10:30:52 -07:00
|
|
|
body.set_width(self.width)
|
2018-04-06 13:58:59 -07:00
|
|
|
body.set_fill(self.shaded_body_color, opacity=1)
|
2019-02-04 15:02:32 -08:00
|
|
|
body.sort(lambda p: p[2])
|
2017-02-18 14:02:46 -08:00
|
|
|
body[-1].set_fill(self.body_color)
|
2018-04-18 00:13:11 -07:00
|
|
|
screen_plate = body.copy()
|
2017-02-18 14:02:46 -08:00
|
|
|
keyboard = VGroup(*[
|
|
|
|
VGroup(*[
|
|
|
|
Square(**self.key_color_kwargs)
|
2018-04-06 13:58:59 -07:00
|
|
|
for x in range(12 - y % 2)
|
2019-02-04 14:54:25 -08:00
|
|
|
]).arrange(RIGHT, buff=SMALL_BUFF)
|
2017-02-18 14:02:46 -08:00
|
|
|
for y in range(4)
|
2019-02-04 14:54:25 -08:00
|
|
|
]).arrange(DOWN, buff=MED_SMALL_BUFF)
|
2017-02-18 14:02:46 -08:00
|
|
|
keyboard.stretch_to_fit_width(
|
2018-04-06 13:58:59 -07:00
|
|
|
self.keyboard_width_to_body_width * body.get_width(),
|
2017-02-18 14:02:46 -08:00
|
|
|
)
|
|
|
|
keyboard.stretch_to_fit_height(
|
2018-04-06 13:58:59 -07:00
|
|
|
self.keyboard_height_to_body_height * body.get_height(),
|
2017-02-18 14:02:46 -08:00
|
|
|
)
|
2018-04-06 13:58:59 -07:00
|
|
|
keyboard.next_to(body, OUT, buff=0.1 * SMALL_BUFF)
|
|
|
|
keyboard.shift(MED_SMALL_BUFF * UP)
|
2017-02-18 14:02:46 -08:00
|
|
|
body.add(keyboard)
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
screen_plate.stretch(self.screen_thickness /
|
|
|
|
self.body_dimensions[2], dim=2)
|
2017-02-18 14:02:46 -08:00
|
|
|
screen = Rectangle(
|
2018-04-06 13:58:59 -07:00
|
|
|
stroke_width=0,
|
|
|
|
fill_color=BLACK,
|
|
|
|
fill_opacity=1,
|
2017-02-18 14:02:46 -08:00
|
|
|
)
|
2018-04-06 13:58:59 -07:00
|
|
|
screen.replace(screen_plate, stretch=True)
|
2017-02-18 14:02:46 -08:00
|
|
|
screen.scale_in_place(self.screen_width_to_screen_plate_width)
|
2018-04-06 13:58:59 -07:00
|
|
|
screen.next_to(screen_plate, OUT, buff=0.1 * SMALL_BUFF)
|
2017-02-18 14:02:46 -08:00
|
|
|
screen_plate.add(screen)
|
2018-04-06 13:58:59 -07:00
|
|
|
screen_plate.next_to(body, UP, buff=0)
|
2017-02-18 14:02:46 -08:00
|
|
|
screen_plate.rotate(
|
2018-04-06 13:58:59 -07:00
|
|
|
self.open_angle, RIGHT,
|
|
|
|
about_point=screen_plate.get_bottom()
|
2017-02-18 14:02:46 -08:00
|
|
|
)
|
|
|
|
self.screen_plate = screen_plate
|
|
|
|
self.screen = screen
|
|
|
|
|
|
|
|
axis = Line(
|
2018-04-06 13:58:59 -07:00
|
|
|
body.get_corner(UP + LEFT + OUT),
|
|
|
|
body.get_corner(UP + RIGHT + OUT),
|
|
|
|
color=BLACK,
|
|
|
|
stroke_width=2
|
2017-02-18 14:02:46 -08:00
|
|
|
)
|
|
|
|
self.axis = axis
|
|
|
|
|
|
|
|
self.add(body, screen_plate, axis)
|
2018-04-06 13:58:59 -07:00
|
|
|
self.rotate(5 * np.pi / 12, LEFT, about_point=ORIGIN)
|
|
|
|
self.rotate(np.pi / 6, DOWN, about_point=ORIGIN)
|
|
|
|
|
2017-02-18 14:02:46 -08:00
|
|
|
|
2017-02-08 13:29:32 -08:00
|
|
|
class PatreonLogo(SVGMobject):
|
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"file_name": "patreon_logo",
|
|
|
|
"fill_color": "#F96854",
|
2017-06-14 21:54:21 -07:00
|
|
|
# "fill_color" : WHITE,
|
2018-04-06 13:58:59 -07:00
|
|
|
"fill_opacity": 1,
|
|
|
|
"stroke_width": 0,
|
|
|
|
"width": 4,
|
2017-02-08 13:29:32 -08:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2017-02-08 13:29:32 -08:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
SVGMobject.__init__(self, **kwargs)
|
2018-08-08 10:30:52 -07:00
|
|
|
self.set_width(self.width)
|
2017-02-08 13:29:32 -08:00
|
|
|
self.center()
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2016-11-07 11:05:41 -08:00
|
|
|
class VideoIcon(SVGMobject):
|
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"file_name": "video_icon",
|
|
|
|
"width": FRAME_WIDTH / 12.,
|
2016-11-07 11:05:41 -08:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2016-11-07 11:05:41 -08:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
SVGMobject.__init__(self, **kwargs)
|
|
|
|
self.center()
|
2018-08-08 10:30:52 -07:00
|
|
|
self.set_width(self.width)
|
2018-04-06 13:58:59 -07:00
|
|
|
self.set_stroke(color=WHITE, width=0)
|
|
|
|
self.set_fill(color=WHITE, opacity=1)
|
|
|
|
|
2016-11-07 11:05:41 -08:00
|
|
|
|
|
|
|
class VideoSeries(VGroup):
|
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"num_videos": 11,
|
|
|
|
"gradient_colors": [BLUE_B, BLUE_D],
|
2016-11-07 11:05:41 -08:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2016-11-07 11:05:41 -08:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
digest_config(self, kwargs)
|
|
|
|
videos = [VideoIcon() for x in range(self.num_videos)]
|
|
|
|
VGroup.__init__(self, *videos, **kwargs)
|
2019-02-04 14:54:25 -08:00
|
|
|
self.arrange()
|
2018-08-08 10:30:52 -07:00
|
|
|
self.set_width(FRAME_WIDTH - MED_LARGE_BUFF)
|
2018-03-30 11:59:39 -07:00
|
|
|
self.set_color_by_gradient(*self.gradient_colors)
|
2016-11-07 11:05:41 -08:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2016-11-07 11:05:41 -08:00
|
|
|
class Headphones(SVGMobject):
|
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"file_name": "headphones",
|
|
|
|
"height": 2,
|
|
|
|
"y_stretch_factor": 0.5,
|
|
|
|
"color": GREY,
|
2016-11-07 11:05:41 -08:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
|
|
|
def __init__(self, pi_creature=None, **kwargs):
|
2016-11-07 11:05:41 -08:00
|
|
|
digest_config(self, kwargs)
|
2018-04-06 13:58:59 -07:00
|
|
|
SVGMobject.__init__(self, file_name=self.file_name, **kwargs)
|
|
|
|
self.stretch(self.y_stretch_factor, 1)
|
2018-08-08 10:30:52 -07:00
|
|
|
self.set_height(self.height)
|
2018-04-06 13:58:59 -07:00
|
|
|
self.set_stroke(width=0)
|
|
|
|
self.set_fill(color=self.color)
|
2017-06-09 16:10:34 -07:00
|
|
|
if pi_creature is not None:
|
|
|
|
eyes = pi_creature.eyes
|
2018-08-08 10:30:52 -07:00
|
|
|
self.set_height(3 * eyes.get_height())
|
2017-06-09 16:10:34 -07:00
|
|
|
self.move_to(eyes, DOWN)
|
2018-04-06 13:58:59 -07:00
|
|
|
self.shift(DOWN * eyes.get_height() / 4)
|
|
|
|
|
2016-11-07 11:05:41 -08:00
|
|
|
|
2016-12-26 07:10:38 -08:00
|
|
|
class Clock(VGroup):
|
2019-02-06 16:25:03 -08:00
|
|
|
CONFIG = {}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2016-12-26 07:10:38 -08:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
circle = Circle()
|
|
|
|
ticks = []
|
|
|
|
for x in range(12):
|
2018-04-06 13:58:59 -07:00
|
|
|
alpha = x / 12.
|
2016-12-26 07:10:38 -08:00
|
|
|
point = complex_to_R3(
|
2018-04-06 13:58:59 -07:00
|
|
|
np.exp(2 * np.pi * alpha * complex(0, 1))
|
2016-12-26 07:10:38 -08:00
|
|
|
)
|
2018-04-06 13:58:59 -07:00
|
|
|
length = 0.2 if x % 3 == 0 else 0.1
|
2016-12-26 07:10:38 -08:00
|
|
|
ticks.append(
|
2018-04-06 13:58:59 -07:00
|
|
|
Line(point, (1 - length) * point)
|
2016-12-26 07:10:38 -08:00
|
|
|
)
|
2018-04-06 13:58:59 -07:00
|
|
|
self.hour_hand = Line(ORIGIN, 0.3 * UP)
|
|
|
|
self.minute_hand = Line(ORIGIN, 0.6 * UP)
|
2016-12-26 07:10:38 -08:00
|
|
|
# for hand in self.hour_hand, self.minute_hand:
|
|
|
|
# #Balance out where the center is
|
|
|
|
# hand.add(VectorizedPoint(-hand.get_end()))
|
|
|
|
|
|
|
|
VGroup.__init__(
|
2018-04-06 13:58:59 -07:00
|
|
|
self, circle,
|
2016-12-26 07:10:38 -08:00
|
|
|
self.hour_hand, self.minute_hand,
|
|
|
|
*ticks
|
|
|
|
)
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2016-12-26 07:10:38 -08:00
|
|
|
class ClockPassesTime(Animation):
|
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"run_time": 5,
|
|
|
|
"hours_passed": 12,
|
2019-02-05 15:39:58 -08:00
|
|
|
"rate_func": linear,
|
2016-12-26 07:10:38 -08:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2016-12-26 07:10:38 -08:00
|
|
|
def __init__(self, clock, **kwargs):
|
|
|
|
digest_config(self, kwargs)
|
|
|
|
assert(isinstance(clock, Clock))
|
|
|
|
rot_kwargs = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"axis": OUT,
|
|
|
|
"about_point": clock.get_center()
|
2016-12-26 07:10:38 -08:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
hour_radians = -self.hours_passed * 2 * np.pi / 12
|
2016-12-26 07:10:38 -08:00
|
|
|
self.hour_rotation = Rotating(
|
2018-04-06 13:58:59 -07:00
|
|
|
clock.hour_hand,
|
|
|
|
radians=hour_radians,
|
2016-12-26 07:10:38 -08:00
|
|
|
**rot_kwargs
|
|
|
|
)
|
|
|
|
self.minute_rotation = Rotating(
|
2018-04-06 13:58:59 -07:00
|
|
|
clock.minute_hand,
|
|
|
|
radians=12 * hour_radians,
|
2016-12-26 07:10:38 -08:00
|
|
|
**rot_kwargs
|
|
|
|
)
|
|
|
|
Animation.__init__(self, clock, **kwargs)
|
|
|
|
|
2019-02-08 11:57:27 -08:00
|
|
|
def interpolate_mobject(self, alpha):
|
2016-12-26 07:10:38 -08:00
|
|
|
for rotation in self.hour_rotation, self.minute_rotation:
|
2019-02-08 11:57:27 -08:00
|
|
|
rotation.interpolate_mobject(alpha)
|
2016-11-07 11:05:41 -08:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2016-11-07 11:05:41 -08:00
|
|
|
class Bubble(SVGMobject):
|
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"direction": LEFT,
|
|
|
|
"center_point": ORIGIN,
|
|
|
|
"content_scale_factor": 0.75,
|
|
|
|
"height": 5,
|
|
|
|
"width": 8,
|
|
|
|
"bubble_center_adjustment_factor": 1. / 8,
|
|
|
|
"file_name": None,
|
|
|
|
"fill_color": BLACK,
|
|
|
|
"fill_opacity": 0.8,
|
|
|
|
"stroke_color": WHITE,
|
|
|
|
"stroke_width": 3,
|
2016-11-07 11:05:41 -08:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2016-11-07 11:05:41 -08:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
digest_config(self, kwargs, locals())
|
|
|
|
if self.file_name is None:
|
|
|
|
raise Exception("Must invoke Bubble subclass")
|
2018-01-12 13:38:25 -08:00
|
|
|
try:
|
|
|
|
SVGMobject.__init__(self, **kwargs)
|
|
|
|
except IOError as err:
|
|
|
|
self.file_name = os.path.join(FILE_DIR, self.file_name)
|
|
|
|
SVGMobject.__init__(self, **kwargs)
|
2016-11-07 11:05:41 -08:00
|
|
|
self.center()
|
|
|
|
self.stretch_to_fit_height(self.height)
|
|
|
|
self.stretch_to_fit_width(self.width)
|
|
|
|
if self.direction[0] > 0:
|
|
|
|
Mobject.flip(self)
|
|
|
|
self.direction_was_specified = ("direction" in kwargs)
|
|
|
|
self.content = Mobject()
|
|
|
|
|
|
|
|
def get_tip(self):
|
2018-04-06 13:58:59 -07:00
|
|
|
# TODO, find a better way
|
|
|
|
return self.get_corner(DOWN + self.direction) - 0.6 * self.direction
|
2016-11-07 11:05:41 -08:00
|
|
|
|
|
|
|
def get_bubble_center(self):
|
|
|
|
factor = self.bubble_center_adjustment_factor
|
2018-04-06 13:58:59 -07:00
|
|
|
return self.get_center() + factor * self.get_height() * UP
|
2016-11-07 11:05:41 -08:00
|
|
|
|
|
|
|
def move_tip_to(self, point):
|
2016-11-18 21:23:52 -08:00
|
|
|
VGroup(self, self.content).shift(point - self.get_tip())
|
2016-11-07 11:05:41 -08:00
|
|
|
return self
|
|
|
|
|
|
|
|
def flip(self):
|
2018-04-06 13:58:59 -07:00
|
|
|
Mobject.flip(self)
|
2016-11-07 11:05:41 -08:00
|
|
|
self.direction = -np.array(self.direction)
|
|
|
|
return self
|
|
|
|
|
|
|
|
def pin_to(self, mobject):
|
|
|
|
mob_center = mobject.get_center()
|
2018-05-10 17:10:01 +02:00
|
|
|
want_to_flip = np.sign(mob_center[0]) != np.sign(self.direction[0])
|
2016-11-07 11:05:41 -08:00
|
|
|
can_flip = not self.direction_was_specified
|
2018-05-10 17:10:01 +02:00
|
|
|
if want_to_flip and can_flip:
|
2016-11-07 11:05:41 -08:00
|
|
|
self.flip()
|
2018-04-06 13:58:59 -07:00
|
|
|
boundary_point = mobject.get_critical_point(UP - self.direction)
|
|
|
|
vector_from_center = 1.0 * (boundary_point - mob_center)
|
|
|
|
self.move_tip_to(mob_center + vector_from_center)
|
2016-11-07 11:05:41 -08:00
|
|
|
return self
|
|
|
|
|
|
|
|
def position_mobject_inside(self, mobject):
|
2018-04-06 13:58:59 -07:00
|
|
|
scaled_width = self.content_scale_factor * self.get_width()
|
2016-11-07 11:05:41 -08:00
|
|
|
if mobject.get_width() > scaled_width:
|
2018-08-08 10:30:52 -07:00
|
|
|
mobject.set_width(scaled_width)
|
2016-11-07 11:05:41 -08:00
|
|
|
mobject.shift(
|
|
|
|
self.get_bubble_center() - mobject.get_center()
|
|
|
|
)
|
|
|
|
return mobject
|
|
|
|
|
|
|
|
def add_content(self, mobject):
|
|
|
|
self.position_mobject_inside(mobject)
|
|
|
|
self.content = mobject
|
|
|
|
return self.content
|
|
|
|
|
|
|
|
def write(self, *text):
|
|
|
|
self.add_content(TextMobject(*text))
|
|
|
|
return self
|
|
|
|
|
|
|
|
def resize_to_content(self):
|
|
|
|
target_width = self.content.get_width()
|
2017-01-25 16:40:59 -08:00
|
|
|
target_width += max(MED_LARGE_BUFF, 2)
|
2016-11-07 11:05:41 -08:00
|
|
|
target_height = self.content.get_height()
|
2018-04-06 13:58:59 -07:00
|
|
|
target_height += 2.5 * LARGE_BUFF
|
2016-11-07 11:05:41 -08:00
|
|
|
tip_point = self.get_tip()
|
|
|
|
self.stretch_to_fit_width(target_width)
|
|
|
|
self.stretch_to_fit_height(target_height)
|
|
|
|
self.move_tip_to(tip_point)
|
|
|
|
self.position_mobject_inside(self.content)
|
|
|
|
|
|
|
|
def clear(self):
|
|
|
|
self.add_content(VMobject())
|
|
|
|
return self
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2016-11-07 11:05:41 -08:00
|
|
|
class SpeechBubble(Bubble):
|
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"file_name": "Bubbles_speech.svg",
|
|
|
|
"height": 4
|
2016-11-07 11:05:41 -08:00
|
|
|
}
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2016-11-07 11:05:41 -08:00
|
|
|
class DoubleSpeechBubble(Bubble):
|
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"file_name": "Bubbles_double_speech.svg",
|
|
|
|
"height": 4
|
2016-11-07 11:05:41 -08:00
|
|
|
}
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2016-11-07 11:05:41 -08:00
|
|
|
class ThoughtBubble(Bubble):
|
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"file_name": "Bubbles_thought.svg",
|
2016-11-07 11:05:41 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
Bubble.__init__(self, **kwargs)
|
|
|
|
self.submobjects.sort(
|
2018-08-11 17:16:03 -07:00
|
|
|
key=lambda m: m.get_bottom()[1]
|
2016-11-07 11:05:41 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
def make_green_screen(self):
|
2018-04-06 13:58:59 -07:00
|
|
|
self.submobjects[-1].set_fill(GREEN_SCREEN, opacity=1)
|
2016-11-07 11:05:41 -08:00
|
|
|
return self
|
2018-01-11 18:13:35 -08:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-02-13 21:38:10 -08:00
|
|
|
class Car(SVGMobject):
|
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"file_name": "Car",
|
|
|
|
"height": 1,
|
|
|
|
"color": LIGHT_GREY,
|
|
|
|
"light_colors": [BLACK, BLACK],
|
2018-02-13 21:38:10 -08:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-02-13 21:38:10 -08:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
SVGMobject.__init__(self, **kwargs)
|
2019-02-10 10:26:08 -08:00
|
|
|
|
|
|
|
path = self.submobjects[0]
|
|
|
|
subpaths = path.get_subpaths()
|
|
|
|
path.clear_points()
|
|
|
|
for indices in [(0, 1), (2, 3), (4, 6, 7), (5,), (8,)]:
|
|
|
|
part = VMobject()
|
|
|
|
for index in indices:
|
|
|
|
part.append_points(subpaths[index])
|
|
|
|
path.add(part)
|
|
|
|
|
2018-08-08 10:30:52 -07:00
|
|
|
self.set_height(self.height)
|
2018-04-06 13:58:59 -07:00
|
|
|
self.set_stroke(color=WHITE, width=0)
|
|
|
|
self.set_fill(self.color, opacity=1)
|
2018-02-13 21:38:10 -08:00
|
|
|
|
2018-12-27 09:41:41 -08:00
|
|
|
from manimlib.for_3b1b_videos.pi_creature import Randolph
|
2018-04-06 13:58:59 -07:00
|
|
|
randy = Randolph(mode="happy")
|
2018-08-08 10:30:52 -07:00
|
|
|
randy.set_height(0.6 * self.get_height())
|
2018-02-13 21:38:10 -08:00
|
|
|
randy.stretch(0.8, 0)
|
|
|
|
randy.look(RIGHT)
|
|
|
|
randy.move_to(self)
|
2018-04-06 13:58:59 -07:00
|
|
|
randy.shift(0.07 * self.height * (RIGHT + UP))
|
2018-02-13 21:38:10 -08:00
|
|
|
self.randy = self.pi_creature = randy
|
|
|
|
self.add_to_back(randy)
|
|
|
|
|
|
|
|
orientation_line = Line(self.get_left(), self.get_right())
|
2018-04-06 13:58:59 -07:00
|
|
|
orientation_line.set_stroke(width=0)
|
2018-02-13 21:38:10 -08:00
|
|
|
self.add(orientation_line)
|
|
|
|
self.orientation_line = orientation_line
|
|
|
|
|
|
|
|
for light, color in zip(self.get_lights(), self.light_colors):
|
|
|
|
light.set_fill(color, 1)
|
|
|
|
light.is_subpath = False
|
|
|
|
|
|
|
|
self.add_treds_to_tires()
|
|
|
|
|
|
|
|
def move_to(self, point_or_mobject):
|
|
|
|
vect = rotate_vector(
|
2018-04-06 13:58:59 -07:00
|
|
|
UP + LEFT, self.orientation_line.get_angle()
|
2018-02-13 21:38:10 -08:00
|
|
|
)
|
2018-04-06 13:58:59 -07:00
|
|
|
self.next_to(point_or_mobject, vect, buff=0)
|
2018-02-13 21:38:10 -08:00
|
|
|
return self
|
|
|
|
|
|
|
|
def get_front_line(self):
|
|
|
|
return DashedLine(
|
2018-04-06 13:58:59 -07:00
|
|
|
self.get_corner(UP + RIGHT),
|
|
|
|
self.get_corner(DOWN + RIGHT),
|
|
|
|
color=DISTANCE_COLOR,
|
2019-02-07 15:40:26 -08:00
|
|
|
dash_length=0.05,
|
2018-02-13 21:38:10 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
def add_treds_to_tires(self):
|
|
|
|
for tire in self.get_tires():
|
2018-04-06 13:58:59 -07:00
|
|
|
radius = tire.get_width() / 2
|
2018-02-13 21:38:10 -08:00
|
|
|
center = tire.get_center()
|
|
|
|
tred = Line(
|
2019-02-10 10:26:08 -08:00
|
|
|
0.7 * radius * RIGHT, 1.1 * radius * RIGHT,
|
2018-04-06 13:58:59 -07:00
|
|
|
stroke_width=2,
|
|
|
|
color=BLACK
|
2018-02-13 21:38:10 -08:00
|
|
|
)
|
2019-02-10 10:26:08 -08:00
|
|
|
tred.rotate(PI / 5, about_point=tred.get_end())
|
2018-04-06 13:58:59 -07:00
|
|
|
for theta in np.arange(0, 2 * np.pi, np.pi / 4):
|
2018-02-13 21:38:10 -08:00
|
|
|
new_tred = tred.copy()
|
2018-04-06 13:58:59 -07:00
|
|
|
new_tred.rotate(theta, about_point=ORIGIN)
|
2018-02-13 21:38:10 -08:00
|
|
|
new_tred.shift(center)
|
|
|
|
tire.add(new_tred)
|
|
|
|
return self
|
|
|
|
|
|
|
|
def get_tires(self):
|
2019-02-10 10:26:08 -08:00
|
|
|
return VGroup(self[1][0], self[1][1])
|
2018-02-13 21:38:10 -08:00
|
|
|
|
|
|
|
def get_lights(self):
|
|
|
|
return VGroup(self.get_front_light(), self.get_rear_light())
|
|
|
|
|
|
|
|
def get_front_light(self):
|
2019-02-10 10:26:08 -08:00
|
|
|
return self[1][3]
|
2018-02-13 21:38:10 -08:00
|
|
|
|
|
|
|
def get_rear_light(self):
|
2019-02-10 10:26:08 -08:00
|
|
|
return self[1][4]
|
2018-04-01 10:45:41 -07:00
|
|
|
|
2018-06-08 17:15:56 -07:00
|
|
|
|
|
|
|
class VectorizedEarth(SVGMobject):
|
|
|
|
CONFIG = {
|
|
|
|
"file_name": "earth",
|
|
|
|
"height": 1.5,
|
|
|
|
"fill_color": BLACK,
|
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
SVGMobject.__init__(self, **kwargs)
|
|
|
|
circle = Circle(
|
|
|
|
stroke_width=3,
|
|
|
|
stroke_color=GREEN,
|
|
|
|
fill_opacity=1,
|
|
|
|
fill_color=BLUE_C,
|
|
|
|
)
|
|
|
|
circle.replace(self)
|
|
|
|
self.add_to_back(circle)
|
|
|
|
|
|
|
|
|
2018-07-31 19:39:41 -07:00
|
|
|
class Logo(VMobject):
|
|
|
|
CONFIG = {
|
|
|
|
"pupil_radius": 1.0,
|
|
|
|
"outer_radius": 2.0,
|
|
|
|
"iris_background_blue": "#74C0E3",
|
|
|
|
"iris_background_brown": "#8C6239",
|
|
|
|
"blue_spike_colors": [
|
|
|
|
"#528EA3",
|
|
|
|
"#3E6576",
|
|
|
|
"#224C5B",
|
|
|
|
BLACK,
|
|
|
|
],
|
|
|
|
"brown_spike_colors": [
|
|
|
|
"#754C24",
|
|
|
|
"#603813",
|
|
|
|
"#42210b",
|
|
|
|
BLACK,
|
|
|
|
],
|
|
|
|
"n_spike_layers": 4,
|
|
|
|
"n_spikes": 28,
|
|
|
|
"spike_angle": TAU / 28,
|
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
VMobject.__init__(self, **kwargs)
|
|
|
|
self.add_iris_back()
|
|
|
|
self.add_spikes()
|
|
|
|
self.add_pupil()
|
|
|
|
|
|
|
|
def add_iris_back(self):
|
|
|
|
blue_iris_back = AnnularSector(
|
|
|
|
inner_radius=self.pupil_radius,
|
|
|
|
outer_radius=self.outer_radius,
|
|
|
|
angle=270 * DEGREES,
|
|
|
|
start_angle=180 * DEGREES,
|
|
|
|
fill_color=self.iris_background_blue,
|
|
|
|
fill_opacity=1,
|
|
|
|
stroke_width=0,
|
|
|
|
)
|
|
|
|
brown_iris_back = AnnularSector(
|
|
|
|
inner_radius=self.pupil_radius,
|
|
|
|
outer_radius=self.outer_radius,
|
|
|
|
angle=90 * DEGREES,
|
|
|
|
start_angle=90 * DEGREES,
|
|
|
|
fill_color=self.iris_background_brown,
|
|
|
|
fill_opacity=1,
|
|
|
|
stroke_width=0,
|
|
|
|
)
|
|
|
|
self.iris_background = VGroup(
|
|
|
|
blue_iris_back,
|
|
|
|
brown_iris_back,
|
|
|
|
)
|
|
|
|
self.add(self.iris_background)
|
|
|
|
|
|
|
|
def add_spikes(self):
|
|
|
|
layers = VGroup()
|
|
|
|
radii = np.linspace(
|
|
|
|
self.outer_radius,
|
|
|
|
self.pupil_radius,
|
|
|
|
self.n_spike_layers,
|
|
|
|
endpoint=False,
|
|
|
|
)
|
|
|
|
radii[:2] = radii[1::-1] # Swap first two
|
|
|
|
radii[-1] = interpolate(
|
|
|
|
radii[-1], self.pupil_radius, 0.25
|
|
|
|
)
|
|
|
|
|
|
|
|
for radius in radii:
|
|
|
|
tip_angle = self.spike_angle
|
|
|
|
half_base = radius * np.tan(tip_angle)
|
|
|
|
triangle, right_half_triangle = [
|
|
|
|
Polygon(
|
|
|
|
radius * UP,
|
|
|
|
half_base * RIGHT,
|
|
|
|
vertex3,
|
|
|
|
fill_opacity=1,
|
|
|
|
stroke_width=0,
|
|
|
|
)
|
2018-08-09 17:56:05 -07:00
|
|
|
for vertex3 in (half_base * LEFT, ORIGIN,)
|
2018-07-31 19:39:41 -07:00
|
|
|
]
|
|
|
|
left_half_triangle = right_half_triangle.copy()
|
|
|
|
left_half_triangle.flip(UP, about_point=ORIGIN)
|
|
|
|
|
|
|
|
n_spikes = self.n_spikes
|
|
|
|
full_spikes = [
|
|
|
|
triangle.copy().rotate(
|
|
|
|
-angle,
|
|
|
|
about_point=ORIGIN
|
|
|
|
)
|
|
|
|
for angle in np.linspace(
|
|
|
|
0, TAU, n_spikes, endpoint=False
|
|
|
|
)
|
|
|
|
]
|
2018-08-11 16:53:37 -07:00
|
|
|
index = (3 * n_spikes) // 4
|
2018-07-31 19:39:41 -07:00
|
|
|
if radius == radii[0]:
|
|
|
|
layer = VGroup(*full_spikes)
|
|
|
|
layer.rotate(
|
|
|
|
-TAU / n_spikes / 2,
|
|
|
|
about_point=ORIGIN
|
|
|
|
)
|
|
|
|
layer.brown_index = index
|
|
|
|
else:
|
|
|
|
half_spikes = [
|
|
|
|
right_half_triangle.copy(),
|
|
|
|
left_half_triangle.copy().rotate(
|
|
|
|
90 * DEGREES, about_point=ORIGIN,
|
|
|
|
),
|
|
|
|
right_half_triangle.copy().rotate(
|
|
|
|
90 * DEGREES, about_point=ORIGIN,
|
|
|
|
),
|
|
|
|
left_half_triangle.copy()
|
|
|
|
]
|
|
|
|
layer = VGroup(*it.chain(
|
|
|
|
half_spikes[:1],
|
|
|
|
full_spikes[1:index],
|
|
|
|
half_spikes[1:3],
|
|
|
|
full_spikes[index + 1:],
|
|
|
|
half_spikes[3:],
|
|
|
|
))
|
|
|
|
layer.brown_index = index + 1
|
|
|
|
|
|
|
|
layers.add(layer)
|
|
|
|
|
|
|
|
# Color spikes
|
|
|
|
blues = self.blue_spike_colors
|
|
|
|
browns = self.brown_spike_colors
|
|
|
|
for layer, blue, brown in zip(layers, blues, browns):
|
|
|
|
index = layer.brown_index
|
|
|
|
layer[:index].set_color(blue)
|
|
|
|
layer[index:].set_color(brown)
|
|
|
|
|
|
|
|
self.spike_layers = layers
|
|
|
|
self.add(layers)
|
|
|
|
|
|
|
|
def add_pupil(self):
|
|
|
|
self.pupil = Circle(
|
|
|
|
radius=self.pupil_radius,
|
|
|
|
fill_color=BLACK,
|
|
|
|
fill_opacity=1,
|
|
|
|
stroke_width=0,
|
2019-01-11 14:24:30 -08:00
|
|
|
sheen=0.0,
|
|
|
|
start_angle=90 * DEGREES,
|
2018-07-31 19:39:41 -07:00
|
|
|
)
|
|
|
|
self.add(self.pupil)
|
|
|
|
|
2019-01-11 14:24:30 -08:00
|
|
|
def cut_pupil(self):
|
|
|
|
pupil = self.pupil
|
|
|
|
center = pupil.get_center()
|
|
|
|
new_pupil = VGroup(*[
|
|
|
|
pupil.copy().pointwise_become_partial(pupil, a, b)
|
|
|
|
for (a, b) in [(0.25, 1), (0, 0.25)]
|
|
|
|
])
|
|
|
|
for sector in new_pupil:
|
2019-02-05 15:25:15 -08:00
|
|
|
sector.add_cubic_bezier_curve_to([
|
2019-01-11 14:24:30 -08:00
|
|
|
sector.points[-1],
|
|
|
|
*[center] * 3,
|
|
|
|
*[sector.points[0]] * 2
|
|
|
|
])
|
|
|
|
self.remove(pupil)
|
|
|
|
self.add(new_pupil)
|
|
|
|
self.pupil = new_pupil
|
|
|
|
|
|
|
|
def get_blue_part_and_brown_part(self):
|
|
|
|
if len(self.pupil) == 1:
|
|
|
|
self.cut_pupil()
|
|
|
|
# circle = Circle()
|
|
|
|
# circle.set_stroke(width=0)
|
|
|
|
# circle.set_fill(BLACK, opacity=1)
|
|
|
|
# circle.match_width(self)
|
|
|
|
# circle.move_to(self)
|
|
|
|
blue_part = VGroup(
|
|
|
|
self.iris_background[0],
|
|
|
|
*[
|
|
|
|
layer[:layer.brown_index]
|
|
|
|
for layer in self.spike_layers
|
|
|
|
],
|
|
|
|
self.pupil[0],
|
|
|
|
)
|
|
|
|
brown_part = VGroup(
|
|
|
|
self.iris_background[1],
|
|
|
|
*[
|
|
|
|
layer[layer.brown_index:]
|
|
|
|
for layer in self.spike_layers
|
|
|
|
],
|
|
|
|
self.pupil[1],
|
|
|
|
)
|
|
|
|
return blue_part, brown_part
|
|
|
|
|
|
|
|
|
2018-07-31 19:39:41 -07:00
|
|
|
|
|
|
|
# Cards
|
2018-04-01 10:45:41 -07:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-04-01 10:45:41 -07:00
|
|
|
class DeckOfCards(VGroup):
|
|
|
|
def __init__(self, **kwargs):
|
2018-08-09 17:56:05 -07:00
|
|
|
possible_values = list(map(str, list(range(1, 11)))) + ["J", "Q", "K"]
|
2018-04-01 10:45:41 -07:00
|
|
|
possible_suits = ["hearts", "diamonds", "spades", "clubs"]
|
|
|
|
VGroup.__init__(self, *[
|
2018-04-06 13:58:59 -07:00
|
|
|
PlayingCard(value=value, suit=suit, **kwargs)
|
2018-04-01 10:45:41 -07:00
|
|
|
for value in possible_values
|
|
|
|
for suit in possible_suits
|
|
|
|
])
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-04-01 10:45:41 -07:00
|
|
|
class PlayingCard(VGroup):
|
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"value": None,
|
|
|
|
"suit": None,
|
|
|
|
"key": None, # String like "8H" or "KS"
|
|
|
|
"height": 2,
|
|
|
|
"height_to_width": 3.5 / 2.5,
|
|
|
|
"card_height_to_symbol_height": 7,
|
|
|
|
"card_width_to_corner_num_width": 10,
|
|
|
|
"card_height_to_corner_num_height": 10,
|
|
|
|
"color": LIGHT_GREY,
|
|
|
|
"turned_over": False,
|
|
|
|
"possible_suits": ["hearts", "diamonds", "spades", "clubs"],
|
2018-08-09 17:56:05 -07:00
|
|
|
"possible_values": list(map(str, list(range(2, 11)))) + ["J", "Q", "K", "A"],
|
2018-04-01 10:45:41 -07:00
|
|
|
}
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
def __init__(self, key=None, **kwargs):
|
|
|
|
VGroup.__init__(self, key=key, **kwargs)
|
2018-04-01 10:45:41 -07:00
|
|
|
|
|
|
|
def generate_points(self):
|
|
|
|
self.add(Rectangle(
|
2018-04-06 13:58:59 -07:00
|
|
|
height=self.height,
|
|
|
|
width=self.height / self.height_to_width,
|
|
|
|
stroke_color=WHITE,
|
|
|
|
stroke_width=2,
|
|
|
|
fill_color=self.color,
|
|
|
|
fill_opacity=1,
|
2018-04-01 10:45:41 -07:00
|
|
|
))
|
|
|
|
if self.turned_over:
|
|
|
|
self.set_fill(DARK_GREY)
|
|
|
|
self.set_stroke(LIGHT_GREY)
|
|
|
|
contents = VectorizedPoint(self.get_center())
|
|
|
|
else:
|
|
|
|
value = self.get_value()
|
|
|
|
symbol = self.get_symbol()
|
|
|
|
design = self.get_design(value, symbol)
|
|
|
|
corner_numbers = self.get_corner_numbers(value, symbol)
|
|
|
|
contents = VGroup(design, corner_numbers)
|
|
|
|
self.design = design
|
|
|
|
self.corner_numbers = corner_numbers
|
|
|
|
self.add(contents)
|
|
|
|
|
|
|
|
def get_value(self):
|
|
|
|
value = self.value
|
|
|
|
if value is None:
|
|
|
|
if self.key is not None:
|
|
|
|
value = self.key[:-1]
|
|
|
|
else:
|
|
|
|
value = random.choice(self.possible_values)
|
|
|
|
value = string.upper(str(value))
|
|
|
|
if value == "1":
|
|
|
|
value = "A"
|
|
|
|
if value not in self.possible_values:
|
|
|
|
raise Exception("Invalid card value")
|
|
|
|
|
|
|
|
face_card_to_value = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"J": 11,
|
|
|
|
"Q": 12,
|
|
|
|
"K": 13,
|
|
|
|
"A": 14,
|
2018-04-01 10:45:41 -07:00
|
|
|
}
|
|
|
|
try:
|
|
|
|
self.numerical_value = int(value)
|
|
|
|
except:
|
|
|
|
self.numerical_value = face_card_to_value[value]
|
|
|
|
return value
|
|
|
|
|
|
|
|
def get_symbol(self):
|
|
|
|
suit = self.suit
|
|
|
|
if suit is None:
|
|
|
|
if self.key is not None:
|
|
|
|
suit = dict([
|
|
|
|
(string.upper(s[0]), s)
|
|
|
|
for s in self.possible_suits
|
|
|
|
])[string.upper(self.key[-1])]
|
|
|
|
else:
|
|
|
|
suit = random.choice(self.possible_suits)
|
|
|
|
if suit not in self.possible_suits:
|
|
|
|
raise Exception("Invalud suit value")
|
|
|
|
self.suit = suit
|
|
|
|
symbol_height = float(self.height) / self.card_height_to_symbol_height
|
2018-04-06 13:58:59 -07:00
|
|
|
symbol = SuitSymbol(suit, height=symbol_height)
|
2018-04-01 10:45:41 -07:00
|
|
|
return symbol
|
|
|
|
|
|
|
|
def get_design(self, value, symbol):
|
|
|
|
if value == "A":
|
|
|
|
return self.get_ace_design(symbol)
|
2018-08-09 17:56:05 -07:00
|
|
|
if value in list(map(str, list(range(2, 11)))):
|
2018-04-01 10:45:41 -07:00
|
|
|
return self.get_number_design(value, symbol)
|
|
|
|
else:
|
|
|
|
return self.get_face_card_design(value, symbol)
|
|
|
|
|
|
|
|
def get_ace_design(self, symbol):
|
|
|
|
design = symbol.copy().scale(1.5)
|
|
|
|
design.move_to(self)
|
|
|
|
return design
|
|
|
|
|
|
|
|
def get_number_design(self, value, symbol):
|
|
|
|
num = int(value)
|
|
|
|
n_rows = {
|
2018-04-06 13:58:59 -07:00
|
|
|
2: 2,
|
|
|
|
3: 3,
|
|
|
|
4: 2,
|
|
|
|
5: 2,
|
|
|
|
6: 3,
|
|
|
|
7: 3,
|
|
|
|
8: 3,
|
|
|
|
9: 4,
|
|
|
|
10: 4,
|
2018-04-01 10:45:41 -07:00
|
|
|
}[num]
|
|
|
|
n_cols = 1 if num in [2, 3] else 2
|
|
|
|
insertion_indices = {
|
2018-04-06 13:58:59 -07:00
|
|
|
5: [0],
|
|
|
|
7: [0],
|
|
|
|
8: [0, 1],
|
|
|
|
9: [1],
|
|
|
|
10: [0, 2],
|
2018-04-01 10:45:41 -07:00
|
|
|
}.get(num, [])
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
top = self.get_top() + symbol.get_height() * DOWN
|
|
|
|
bottom = self.get_bottom() + symbol.get_height() * UP
|
2018-04-01 10:45:41 -07:00
|
|
|
column_points = [
|
|
|
|
interpolate(top, bottom, alpha)
|
|
|
|
for alpha in np.linspace(0, 1, n_rows)
|
|
|
|
]
|
|
|
|
|
|
|
|
design = VGroup(*[
|
|
|
|
symbol.copy().move_to(point)
|
|
|
|
for point in column_points
|
|
|
|
])
|
|
|
|
if n_cols == 2:
|
2018-04-06 13:58:59 -07:00
|
|
|
space = 0.2 * self.get_width()
|
|
|
|
column_copy = design.copy().shift(space * RIGHT)
|
|
|
|
design.shift(space * LEFT)
|
2018-04-01 10:45:41 -07:00
|
|
|
design.add(*column_copy)
|
|
|
|
design.add(*[
|
|
|
|
symbol.copy().move_to(
|
2018-04-06 13:58:59 -07:00
|
|
|
center_of_mass(column_points[i:i + 2])
|
2018-04-01 10:45:41 -07:00
|
|
|
)
|
|
|
|
for i in insertion_indices
|
|
|
|
])
|
|
|
|
for symbol in design:
|
|
|
|
if symbol.get_center()[1] < self.get_center()[1]:
|
|
|
|
symbol.rotate_in_place(np.pi)
|
|
|
|
return design
|
|
|
|
|
|
|
|
def get_face_card_design(self, value, symbol):
|
|
|
|
from for_3b1b_videos.pi_creature import PiCreature
|
|
|
|
sub_rect = Rectangle(
|
2018-04-06 13:58:59 -07:00
|
|
|
stroke_color=BLACK,
|
|
|
|
fill_opacity=0,
|
|
|
|
height=0.9 * self.get_height(),
|
|
|
|
width=0.6 * self.get_width(),
|
2018-04-01 10:45:41 -07:00
|
|
|
)
|
|
|
|
sub_rect.move_to(self)
|
|
|
|
|
|
|
|
# pi_color = average_color(symbol.get_color(), GREY)
|
|
|
|
pi_color = symbol.get_color()
|
|
|
|
pi_mode = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"J": "plain",
|
|
|
|
"Q": "thinking",
|
|
|
|
"K": "hooray"
|
2018-04-01 10:45:41 -07:00
|
|
|
}[value]
|
|
|
|
pi_creature = PiCreature(
|
2018-04-06 13:58:59 -07:00
|
|
|
mode=pi_mode,
|
|
|
|
color=pi_color,
|
2018-04-01 10:45:41 -07:00
|
|
|
)
|
2018-08-08 10:30:52 -07:00
|
|
|
pi_creature.set_width(0.8 * sub_rect.get_width())
|
2018-04-01 10:45:41 -07:00
|
|
|
if value in ["Q", "K"]:
|
|
|
|
prefix = "king" if value == "K" else "queen"
|
2018-04-06 13:58:59 -07:00
|
|
|
crown = SVGMobject(file_name=prefix + "_crown")
|
|
|
|
crown.set_stroke(width=0)
|
2018-04-01 10:45:41 -07:00
|
|
|
crown.set_fill(YELLOW, 1)
|
2018-04-06 13:58:59 -07:00
|
|
|
crown.stretch_to_fit_width(0.5 * sub_rect.get_width())
|
|
|
|
crown.stretch_to_fit_height(0.17 * sub_rect.get_height())
|
2018-04-01 10:45:41 -07:00
|
|
|
crown.move_to(pi_creature.eyes.get_center(), DOWN)
|
|
|
|
pi_creature.add_to_back(crown)
|
|
|
|
to_top_buff = 0
|
|
|
|
else:
|
2018-04-06 13:58:59 -07:00
|
|
|
to_top_buff = SMALL_BUFF * sub_rect.get_height()
|
2018-04-01 10:45:41 -07:00
|
|
|
pi_creature.next_to(sub_rect.get_top(), DOWN, to_top_buff)
|
|
|
|
# pi_creature.shift(0.05*sub_rect.get_width()*RIGHT)
|
|
|
|
|
|
|
|
pi_copy = pi_creature.copy()
|
2018-04-06 13:58:59 -07:00
|
|
|
pi_copy.rotate(np.pi, about_point=sub_rect.get_center())
|
2018-04-01 10:45:41 -07:00
|
|
|
|
|
|
|
return VGroup(sub_rect, pi_creature, pi_copy)
|
|
|
|
|
|
|
|
def get_corner_numbers(self, value, symbol):
|
|
|
|
value_mob = TextMobject(value)
|
2018-04-06 13:58:59 -07:00
|
|
|
width = self.get_width() / self.card_width_to_corner_num_width
|
|
|
|
height = self.get_height() / self.card_height_to_corner_num_height
|
2018-08-08 10:30:52 -07:00
|
|
|
value_mob.set_width(width)
|
2018-04-01 10:45:41 -07:00
|
|
|
value_mob.stretch_to_fit_height(height)
|
|
|
|
value_mob.next_to(
|
2018-04-06 13:58:59 -07:00
|
|
|
self.get_corner(UP + LEFT), DOWN + RIGHT,
|
|
|
|
buff=MED_LARGE_BUFF * width
|
2018-04-01 10:45:41 -07:00
|
|
|
)
|
|
|
|
value_mob.set_color(symbol.get_color())
|
|
|
|
corner_symbol = symbol.copy()
|
2018-08-08 10:30:52 -07:00
|
|
|
corner_symbol.set_width(width)
|
2018-04-01 10:45:41 -07:00
|
|
|
corner_symbol.next_to(
|
2018-04-06 13:58:59 -07:00
|
|
|
value_mob, DOWN,
|
|
|
|
buff=MED_SMALL_BUFF * width
|
2018-04-01 10:45:41 -07:00
|
|
|
)
|
|
|
|
corner_group = VGroup(value_mob, corner_symbol)
|
|
|
|
opposite_corner_group = corner_group.copy()
|
|
|
|
opposite_corner_group.rotate(
|
2018-04-06 13:58:59 -07:00
|
|
|
np.pi, about_point=self.get_center()
|
2018-04-01 10:45:41 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
return VGroup(corner_group, opposite_corner_group)
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-04-01 10:45:41 -07:00
|
|
|
class SuitSymbol(SVGMobject):
|
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"height": 0.5,
|
|
|
|
"fill_opacity": 1,
|
|
|
|
"stroke_width": 0,
|
|
|
|
"red": "#D02028",
|
|
|
|
"black": BLACK,
|
2018-04-01 10:45:41 -07:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-04-01 10:45:41 -07:00
|
|
|
def __init__(self, suit_name, **kwargs):
|
|
|
|
digest_config(self, kwargs)
|
|
|
|
suits_to_colors = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"hearts": self.red,
|
|
|
|
"diamonds": self.red,
|
|
|
|
"spades": self.black,
|
|
|
|
"clubs": self.black,
|
2018-04-01 10:45:41 -07:00
|
|
|
}
|
|
|
|
if suit_name not in suits_to_colors:
|
|
|
|
raise Exception("Invalid suit name")
|
2018-04-06 13:58:59 -07:00
|
|
|
SVGMobject.__init__(self, file_name=suit_name, **kwargs)
|
2018-04-01 10:45:41 -07:00
|
|
|
|
|
|
|
color = suits_to_colors[suit_name]
|
2018-04-06 13:58:59 -07:00
|
|
|
self.set_stroke(width=0)
|
2018-04-01 10:45:41 -07:00
|
|
|
self.set_fill(color, 1)
|
2018-08-08 10:30:52 -07:00
|
|
|
self.set_height(self.height)
|