A community-maintained Python framework for creating mathematical animations.
Photon through lens
Grant Sanderson044e0b5c723b
diff --git a/brachistochrone/__init__.py b/brachistochrone/__init__.pynew file mode 100644index 000000000..e69de29bbdiff --git a/brachistochrone/curves.py b/brachistochrone/curves.pyindex fe1ac8608..8573c90c0 100644--- a/brachistochrone/curves.py+++ b/brachistochrone/curves.py@@ -323,39 +323,6 @@ class RollingRandolph(PathSlidingScene):-class OceanScene(Scene):- def construct(self):- self.rolling_waves()-- def rolling_waves(self):- if not hasattr(self, "ocean"):- self.setup_ocean()- for state in self.ocean_states:- self.play(Transform(self.ocean, state))--- def setup_ocean(self):- def func(points):- result = np.zeros(points.shape)- result[:,1] = 0.25 * np.sin(points[:,0]) * np.sin(points[:,1])- return result-- self.ocean_states = []- for unit in -1, 1:- ocean = FilledRectangle(- color = BLUE_D,- density = 25- )- nudges = unit*func(ocean.points)- ocean.points += nudges- alphas = nudges[:,1]- alphas -= np.min(alphas)- whites = np.ones(ocean.rgbs.shape)- thick_alphas = alphas.repeat(3).reshape((len(alphas), 3))- ocean.rgbs = interpolate(ocean.rgbs, whites, thick_alphas)- self.ocean_states.append(ocean)- self.ocean = self.ocean_states[1].copy()-diff --git a/brachistochrone/light.py b/brachistochrone/light.pyindex 889bce71b..9a1a060ff 100644--- a/brachistochrone/light.py+++ b/brachistochrone/light.py@@ -27,6 +27,26 @@ from topics.number_line import NumberPlanefrom region import Region, region_from_polygon_verticesfrom scene import Scene+from brachistochrone.curves import Cycloid++class Lens(Arc):+ DEFAULT_CONFIG = {+ "radius" : 2,+ "angle" : np.pi/2,+ "color" : BLUE_B,+ }+ def __init__(self, **kwargs):+ digest_config(self, kwargs)+ Arc.__init__(self, self.angle, **kwargs)++ def generate_points(self):+ Arc.generate_points(self)+ self.rotate(-np.pi/4)+ self.shift(-self.get_left())+ self.add_points(self.copy().rotate(np.pi).points)+++class PhotonScene(Scene):def wavify(self, mobject):tangent_vectors = mobject.points[1:]-mobject.points[:-1]@@ -46,14 +66,11 @@ class PhotonScene(Scene):result.points[1:] += nudgesreturn result- def photon_along_path(self, path, color = YELLOW, run_time = 1):++ def photon_run_along_path(self, path, color = YELLOW, **kwargs):photon = self.wavify(path)photon.highlight(color)- self.play(ShowPassingFlash(- photon,- run_time = run_time,- rate_func = None- ))+ return ShowPassingFlash(photon, **kwargs)class SimplePhoton(PhotonScene):@@ -61,5 +78,60 @@ class SimplePhoton(PhotonScene):text = TextMobject("Light")text.to_edge(UP)self.play(ShimmerIn(text))- self.photon_along_path(Cycloid())- self.dither()\ No newline at end of file+ self.play(self.photon_run_along_path(+ Cycloid(), rate_func = None+ ))+ self.dither()+++class PhotonThroughLens(PhotonScene):+ def construct(self):+ lens = Lens()+ num_paths = 5+ interval_values = np.arange(num_paths)/(num_paths-1.)+ first_contact = [+ lens.point_from_interval(0.4*v+0.55)+ for v in reversed(interval_values)+ ]+ second_contact = [+ lens.point_from_interval(0.3*v + 0.1)+ for v in interval_values+ ]+ focal_point = 2*RIGHT+ paths = [+ Mobject(+ Line(SPACE_WIDTH*LEFT + fc[1]*UP, fc),+ Line(fc, sc),+ Line(sc, focal_point),+ Line(focal_point, 6*focal_point-5*sc)+ ).ingest_sub_mobjects()+ for fc, sc in zip(first_contact, second_contact)+ ]+ colors = Color(YELLOW).range_to(WHITE, len(paths))+ for path, color in zip(paths, colors):+ path.highlight(color)+ photon_runs = [+ self.photon_run_along_path(path)+ for path in paths+ ]+ self.add(lens)+ for photon_run, path in zip(photon_runs, paths):+ self.play(+ photon_run,+ ShowCreation(+ path,+ rate_func = lambda t : 0.9*smooth(t)+ )+ )+ self.dither()+++++++++++diff --git a/constants.py b/constants.pyindex 80d27a050..17bab634a 100644--- a/constants.py+++ b/constants.py@@ -19,8 +19,8 @@ MEDIUM_QUALITY_DISPLAY_CONFIG = {}LOW_QUALITY_DISPLAY_CONFIG = {- "height" : 480,- "width" : 840,+ "height" : 600,#480,+ "width" : 1000,#840,"frame_duration" : 0.04,}diff --git a/mobject/mobject.py b/mobject/mobject.pyindex 1e8369fcb..0b38511c3 100644--- a/mobject/mobject.py+++ b/mobject/mobject.py@@ -460,6 +460,9 @@ class Mobject(object):def get_height(self):return self.length_over_dim(1)+ def point_from_interval(self, alpha):+ index = alpha*(self.get_num_points()-1)+ return self.points[index]def get_color(self):color = Color()