def snells_law_at_every_point(self, cycloid, chopped_cycloid): square = Square(side_length = 0.2, color = WHITE) words = TextMobject(["Snell's law ", "everywhere"]) snells, rest = words.split() colon = TextMobject(":") words.next_to(square) words.shift(0.3*UP) combo = Mobject(square, words) combo.get_center = lambda : square.get_center() new_snells = snells.copy().center().to_edge(UP, buff = 1.5) colon.next_to(new_snells) colon.shift(0.05*DOWN) self.play(MoveAlongPath( combo, cycloid, run_time = 5 )) self.play(MoveAlongPath( combo, chopped_cycloid, run_time = 4 )) dot = Dot(combo.get_center()) self.play(Transform(square, dot)) self.play( Transform(snells, new_snells), Transform(rest, colon) ) self.dither() return colon
def __init__(self, direction = LEFT, index_of_tip = -1, center = ORIGIN): self.direction = direction self.content = Mobject() self.index_of_tip = index_of_tip self.center_offset = center - Mobject.get_center(self) if direction[0] > 0: self.rotate(np.pi, UP)
def __init__(self, direction=LEFT, index_of_tip=-1, center=ORIGIN): self.direction = direction self.content = Mobject() self.index_of_tip = index_of_tip self.center_offset = center - Mobject.get_center(self) if direction[0] > 0: self.rotate(np.pi, UP)
def show_equation(self): equation = TexMobject([ "\\left(\\dfrac{1}{\\phantom{v_air}}\\right)", "\\sin(\\theta_1)", "=", "\\left(\\dfrac{1}{\\phantom{v_water}}\\right)", "\\sin(\\theta_2)" ]) equation.to_corner(UP + RIGHT) frac1, sin1, equals, frac2, sin2 = equation.split() v_air, v_water = [ TexMobject("v_{\\text{%s}}" % s, size="\\Large") for s in "air", "water" ] v_air.next_to(Point(frac1.get_center()), DOWN) v_water.next_to(Point(frac2.get_center()), DOWN) frac1.add(v_air) frac2.add(v_water) f1, f2 = [TexMobject("F_%d" % d, size="\\Large") for d in 1, 2] f1.next_to(sin1, LEFT) f2.next_to(equals, RIGHT) sin2_start = sin2.copy().next_to(f2, RIGHT) bar1 = TexMobject("\\dfrac{\\qquad}{\\qquad}") bar2 = bar1.copy() bar1.next_to(sin1, DOWN) bar2.next_to(sin2, DOWN) v_air_copy = v_air.copy().next_to(bar1, DOWN) v_water_copy = v_water.copy().next_to(bar2, DOWN) bars = Mobject(bar1, bar2) new_eq = equals.copy().center().shift(bars.get_center()) snells = TextMobject("Snell's Law") snells.highlight(YELLOW) snells.shift(new_eq.get_center()[0] * RIGHT) snells.shift(UP) anims = [] for mob in f1, sin1, equals, f2, sin2_start: anims.append(ShimmerIn(mob)) self.play(*anims) self.wait() for f, frac in (f1, frac1), (f2, frac2): target = frac.copy().ingest_submobjects() also = [] if f is f2: also.append(Transform(sin2_start, sin2)) sin2 = sin2_start self.play(Transform(f, target), *also) self.remove(f) self.add(frac) self.wait() self.play(FadeOut(frac1), FadeOut(frac2), Transform(v_air, v_air_copy), Transform(v_water, v_water_copy), ShowCreation(bars), Transform(equals, new_eq)) self.wait() frac1 = Mobject(sin1, bar1, v_air) frac2 = Mobject(sin2, bar2, v_water) for frac, vect in (frac1, LEFT), (frac2, RIGHT): self.play(ApplyMethod(frac.next_to, equals, vect)) self.wait() self.play(ShimmerIn(snells)) self.wait()
def snells_law_at_every_point(self, cycloid, chopped_cycloid): square = Square(side_length=0.2, color=WHITE) words = TextMobject(["Snell's law ", "everywhere"]) snells, rest = words.split() colon = TextMobject(":") words.next_to(square) words.shift(0.3 * UP) combo = Mobject(square, words) combo.get_center = lambda: square.get_center() new_snells = snells.copy().center().to_edge(UP, buff=1.5) colon.next_to(new_snells) colon.shift(0.05 * DOWN) self.play(MoveAlongPath(combo, cycloid, run_time=5)) self.play(MoveAlongPath(combo, chopped_cycloid, run_time=4)) dot = Dot(combo.get_center()) self.play(Transform(square, dot)) self.play(Transform(snells, new_snells), Transform(rest, colon)) self.dither() return colon
def __init__(self, **kwargs): Mobject.__init__(self, **kwargs) self.center_offset = self.center_point - Mobject.get_center(self) if self.direction[0] > 0: self.rotate(np.pi, UP) self.content = Mobject()
class PiCreature(Mobject): DEFAULT_CONFIG = { "color" : BLUE_E } PART_NAMES = [ 'arm', 'body', 'left_eye', 'right_eye', 'left_leg', 'right_leg', 'mouth', ] WHITE_PART_NAMES = ['left_eye', 'right_eye', 'mouth'] MOUTH_NAMES = ["smile", "frown", "straight_mouth"] def __init__(self, **kwargs): Mobject.__init__(self, **kwargs) for part_name in self.PART_NAMES: mob = ImageMobject( part_name_to_directory(part_name), should_center = False ) if part_name not in self.WHITE_PART_NAMES: mob.highlight(self.color) setattr(self, part_name, mob) self.eyes = Mobject(self.left_eye, self.right_eye) self.legs = Mobject(self.left_leg, self.right_leg) mouth_center = self.get_mouth_center() self.mouth.center() self.smile = self.mouth self.frown = self.mouth.copy().rotate(np.pi, RIGHT) self.straight_mouth = TexMobject("-").scale(0.7) for mouth in self.smile, self.frown, self.straight_mouth: mouth.sort_points(lambda p : p[0]) mouth.highlight(self.color) ##to blend into background mouth.shift(mouth_center) self.digest_mobject_attrs() self.give_smile() self.add(self.mouth) self.scale(PI_CREATURE_SCALE_VAL) def get_parts(self): return [getattr(self, pn) for pn in self.PART_NAMES] def get_white_parts(self): return [ getattr(self, pn) for pn in self.WHITE_PART_NAMES+self.MOUTH_NAMES ] def get_mouth_center(self): result = self.body.get_center() result[0] = self.eyes.get_center()[0] return result # left_center = self.left_eye.get_center() # right_center = self.right_eye.get_center() # l_to_r = right_center-left_center # eyes_to_mouth = rotate_vector(l_to_r, -np.pi/2, OUT) # eyes_to_mouth /= np.linalg.norm(eyes_to_mouth) # return left_center/2 + right_center/2 + \ # PI_CREATURE_MOUTH_TO_EYES_DISTANCE*eyes_to_mouth def highlight(self, color, condition = None): for part in set(self.get_parts()).difference(self.get_white_parts()): part.highlight(color, condition) return self def move_to(self, destination): self.shift(destination-self.get_bottom()) return self def change_mouth_to(self, mouth_name): #TODO, This is poorly implemented self.mouth = getattr(self, mouth_name) self.sub_mobjects = list_update( self.sub_mobjects, self.get_parts() ) self.mouth.highlight(WHITE) return self def give_smile(self): return self.change_mouth_to("smile") def give_frown(self): return self.change_mouth_to("frown") def give_straight_face(self): return self.change_mouth_to("straight_mouth") def get_eye_center(self): return self.eyes.get_center() def make_mean(self): eye_x, eye_y = self.get_eye_center()[:2] def should_delete((x, y, z)): return y - eye_y > 0.3*abs(x - eye_x) self.eyes.highlight("black", should_delete) self.give_straight_face() return self def make_sad(self): eye_x, eye_y = self.get_eye_center()[:2] eye_y += 0.15 def should_delete((x, y, z)): return y - eye_y > -0.3*abs(x - eye_x) self.eyey.highlight("black", should_delete) self.give_frown() return self def get_step_intermediate(self, pi_creature): vect = pi_creature.get_center() - self.get_center() result = self.copy().shift(vect / 2.0) left_forward = vect[0] > 0 if self.right_leg.get_center()[0] < self.left_leg.get_center()[0]: #For Mortimer's case left_forward = not left_forward if left_forward: result.left_leg.wag(vect/2.0, DOWN) result.right_leg.wag(-vect/2.0, DOWN) else: result.right_leg.wag(vect/2.0, DOWN) result.left_leg.wag(-vect/2.0, DOWN) return result def blink(self): bottom = self.eyes.get_bottom() self.eyes.apply_function( lambda (x, y, z) : (x, bottom[1], z) ) return self def shift_eyes(self): for eye in self.left_eye, self.right_eye: eye.rotate_in_place(np.pi, UP) return self def to_symbol(self): Mobject.__init__( self, *list(set(self.get_parts()).difference(self.get_white_parts())) )
def __init__(self, **kwargs): Mobject.__init__(self, **kwargs) self.center_offset = self.center_point - Mobject.get_center(self) if self.direction[0] > 0: self.rotate(np.pi, UP) self.content = Mobject()
class PiCreature(Mobject): DEFAULT_CONFIG = {"color": BLUE_E} PART_NAMES = [ 'arm', 'body', 'left_eye', 'right_eye', 'left_leg', 'right_leg', 'mouth', ] WHITE_PART_NAMES = ['left_eye', 'right_eye', 'mouth'] MOUTH_NAMES = ["smile", "frown", "straight_mouth"] def __init__(self, **kwargs): Mobject.__init__(self, **kwargs) for part_name in self.PART_NAMES: mob = ImageMobject(part_name_to_directory(part_name), should_center=False) if part_name not in self.WHITE_PART_NAMES: mob.highlight(self.color) setattr(self, part_name, mob) self.eyes = Mobject(self.left_eye, self.right_eye) self.legs = Mobject(self.left_leg, self.right_leg) mouth_center = self.get_mouth_center() self.mouth.center() self.smile = self.mouth self.frown = self.mouth.copy().rotate(np.pi, RIGHT) self.straight_mouth = TexMobject("-").scale(0.7) for mouth in self.smile, self.frown, self.straight_mouth: mouth.sort_points(lambda p: p[0]) mouth.highlight(self.color) ##to blend into background mouth.shift(mouth_center) self.digest_mobject_attrs() self.give_smile() self.add(self.mouth) self.scale(PI_CREATURE_SCALE_VAL) def get_parts(self): return [getattr(self, pn) for pn in self.PART_NAMES] def get_white_parts(self): return [ getattr(self, pn) for pn in self.WHITE_PART_NAMES + self.MOUTH_NAMES ] def get_mouth_center(self): result = self.body.get_center() result[0] = self.eyes.get_center()[0] return result # left_center = self.left_eye.get_center() # right_center = self.right_eye.get_center() # l_to_r = right_center-left_center # eyes_to_mouth = rotate_vector(l_to_r, -np.pi/2, OUT) # eyes_to_mouth /= np.linalg.norm(eyes_to_mouth) # return left_center/2 + right_center/2 + \ # PI_CREATURE_MOUTH_TO_EYES_DISTANCE*eyes_to_mouth def highlight(self, color, condition=None): for part in set(self.get_parts()).difference(self.get_white_parts()): part.highlight(color, condition) return self def move_to(self, destination): self.shift(destination - self.get_bottom()) return self def change_mouth_to(self, mouth_name): #TODO, This is poorly implemented self.mouth = getattr(self, mouth_name) self.sub_mobjects = list_update(self.sub_mobjects, self.get_parts()) self.mouth.highlight(WHITE) return self def give_smile(self): return self.change_mouth_to("smile") def give_frown(self): return self.change_mouth_to("frown") def give_straight_face(self): return self.change_mouth_to("straight_mouth") def get_eye_center(self): return self.eyes.get_center() def make_mean(self): eye_x, eye_y = self.get_eye_center()[:2] def should_delete((x, y, z)): return y - eye_y > 0.3 * abs(x - eye_x) self.eyes.highlight("black", should_delete) self.give_straight_face() return self def make_sad(self): eye_x, eye_y = self.get_eye_center()[:2] eye_y += 0.15 def should_delete((x, y, z)): return y - eye_y > -0.3 * abs(x - eye_x) self.eyey.highlight("black", should_delete) self.give_frown() return self def get_step_intermediate(self, pi_creature): vect = pi_creature.get_center() - self.get_center() result = self.copy().shift(vect / 2.0) left_forward = vect[0] > 0 if self.right_leg.get_center()[0] < self.left_leg.get_center()[0]: #For Mortimer's case left_forward = not left_forward if left_forward: result.left_leg.wag(vect / 2.0, DOWN) result.right_leg.wag(-vect / 2.0, DOWN) else: result.right_leg.wag(vect / 2.0, DOWN) result.left_leg.wag(-vect / 2.0, DOWN) return result def blink(self): bottom = self.eyes.get_bottom() self.eyes.apply_function(lambda (x, y, z): (x, bottom[1], z)) return self def shift_eyes(self): for eye in self.left_eye, self.right_eye: eye.rotate_in_place(np.pi, UP) return self def to_symbol(self): Mobject.__init__( self, *list(set(self.get_parts()).difference(self.get_white_parts())))
def show_equation(self): equation = TexMobject([ "\\left(\\dfrac{1}{\\phantom{v_air}}\\right)", "\\sin(\\theta_1)", "=", "\\left(\\dfrac{1}{\\phantom{v_water}}\\right)", "\\sin(\\theta_2)" ]) equation.to_corner(UP+RIGHT) frac1, sin1, equals, frac2, sin2 = equation.split() v_air, v_water = [ TexMobject("v_{\\text{%s}}"%s, size = "\\Large") for s in "air", "water" ] v_air.next_to(Point(frac1.get_center()), DOWN) v_water.next_to(Point(frac2.get_center()), DOWN) frac1.add(v_air) frac2.add(v_water) f1, f2 = [ TexMobject("F_%d"%d, size = "\\Large") for d in 1, 2 ] f1.next_to(sin1, LEFT) f2.next_to(equals, RIGHT) sin2_start = sin2.copy().next_to(f2, RIGHT) bar1 = TexMobject("\\dfrac{\\qquad}{\\qquad}") bar2 = bar1.copy() bar1.next_to(sin1, DOWN) bar2.next_to(sin2, DOWN) v_air_copy = v_air.copy().next_to(bar1, DOWN) v_water_copy = v_water.copy().next_to(bar2, DOWN) bars = Mobject(bar1, bar2) new_eq = equals.copy().center().shift(bars.get_center()) snells = TextMobject("Snell's Law") snells.highlight(YELLOW) snells.shift(new_eq.get_center()[0]*RIGHT) snells.shift(UP) anims = [] for mob in f1, sin1, equals, f2, sin2_start: anims.append(ShimmerIn(mob)) self.play(*anims) self.dither() for f, frac in (f1, frac1), (f2, frac2): target = frac.copy().ingest_submobjects() also = [] if f is f2: also.append(Transform(sin2_start, sin2)) sin2 = sin2_start self.play(Transform(f, target), *also) self.remove(f) self.add(frac) self.dither() self.play( FadeOut(frac1), FadeOut(frac2), Transform(v_air, v_air_copy), Transform(v_water, v_water_copy), ShowCreation(bars), Transform(equals, new_eq) ) self.dither() frac1 = Mobject(sin1, bar1, v_air) frac2 = Mobject(sin2, bar2, v_water) for frac, vect in (frac1, LEFT), (frac2, RIGHT): self.play(ApplyMethod( frac.next_to, equals, vect )) self.dither() self.play(ShimmerIn(snells)) self.dither()