diff --git a/topics/functions.py b/topics/functions.py index 7ae68c28..27bffb69 100644 --- a/topics/functions.py +++ b/topics/functions.py @@ -4,50 +4,47 @@ from mobject.vectorized_mobject import VMobject from helpers import * -class FunctionGraph(VMobject): - CONFIG = { - "color" : YELLOW, - "x_min" : -SPACE_WIDTH, - "x_max" : SPACE_WIDTH, - "num_steps" : 20, - } - def __init__(self, function, **kwargs): - self.function = function - VMobject.__init__(self, **kwargs) - - def generate_points(self): - x_values = np.linspace(self.x_min, self.x_max, self.num_steps) - y_values = self.function(x_values) - okay_indices = np.isfinite(y_values) - x_values = x_values[okay_indices] - y_values = y_values[okay_indices] - self.set_anchor_points([ - x*RIGHT + y*UP - for x, y in zip(x_values, y_values) - ], mode = "smooth") - - def get_function(self): - return self.function - - class ParametricFunction(VMobject): CONFIG = { "t_min" : 0, "t_max" : 1, - "num_anchor_points" : 10, + "num_anchor_points" : 100, } def __init__(self, function, **kwargs): self.function = function VMobject.__init__(self, **kwargs) def generate_points(self): - t_values = np.linspace( - self.t_min, self.t_max, self.num_anchor_points + n_points = 3*self.num_anchor_points - 2 + self.points = np.zeros((n_points, self.dim)) + self.points[:,0] = np.linspace( + self.t_min, self.t_max, n_points ) - points = np.array(map(self.function, t_values)) - okay_indices = np.apply_along_axis(np.all, 1, np.isfinite(points)) - points = points[okay_indices] - self.set_anchor_points(points, mode = "smooth") + #VMobject.apply_function takes care of preserving + #desirable tangent line properties at anchor points + self.apply_function(lambda p : self.function(p[0])) + +class FunctionGraph(ParametricFunction): + CONFIG = { + "color" : YELLOW, + "x_min" : -SPACE_WIDTH, + "x_max" : SPACE_WIDTH, + } + def __init__(self, function, **kwargs): + digest_config(self, kwargs) + parametric_function = lambda t : t*RIGHT + function(t)*UP + ParametricFunction.__init__( + self, + parametric_function, + t_min = self.x_min, + t_max = self.x_max, + **kwargs + ) + self.function = function + + def get_function(self): + return self.function + diff --git a/topics/number_line.py b/topics/number_line.py index 59f00c6b..83b6d2cd 100644 --- a/topics/number_line.py +++ b/topics/number_line.py @@ -4,6 +4,7 @@ from mobject import Mobject1D from mobject.vectorized_mobject import VMobject, VGroup from mobject.tex_mobject import TexMobject from topics.geometry import Line, Arrow +from topics.functions import ParametricFunction from scene import Scene class NumberLine(VMobject): @@ -162,6 +163,7 @@ class Axes(VGroup): "z_min" : -3.5, "z_max" : 3.5, "z_normal" : DOWN, + "default_num_graph_points" : 100, } def __init__(self, **kwargs): VGroup.__init__(self, **kwargs) @@ -192,13 +194,16 @@ class Axes(VGroup): self.y_axis.point_to_number(point), ) - def get_graph(self, function, num_graph_points = 40, **kwargs): + def get_graph(self, function, num_graph_points = None, **kwargs): kwargs["fill_opacity"] = kwargs.get("fill_opacity", 0) - graph = VMobject(**kwargs) - graph.set_points_smoothly([ - self.coords_to_point(x, function(x)) - for x in np.linspace(self.x_min, self.x_max, num_graph_points) - ]) + kwargs["num_anchor_points"] = \ + num_graph_points or self.default_num_graph_points + graph = ParametricFunction( + lambda t : self.coords_to_point(t, function(t)), + t_min = self.x_min, + t_max = self.x_max, + **kwargs + ) graph.underlying_function = function return graph