3b1b-manim/mobject/functions.py

65 lines
1.4 KiB
Python
Raw Normal View History

from __future__ import absolute_import
from constants import *
from mobject.types.vectorized_mobject import VMobject
from utils.config_ops import digest_config
2018-01-18 16:46:38 -08:00
class ParametricFunction(VMobject):
2016-02-27 16:32:53 -08:00
CONFIG = {
2018-01-18 16:46:38 -08:00
"t_min" : 0,
"t_max" : 1,
"num_anchor_points" : 100,
}
def __init__(self, function, **kwargs):
self.function = function
2016-04-19 00:20:19 -07:00
VMobject.__init__(self, **kwargs)
def generate_points(self):
2018-01-18 16:46:38 -08:00
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
)
#VMobject.apply_function takes care of preserving
#desirable tangent line properties at anchor points
self.apply_function(lambda p : self.function(p[0]))
2018-01-18 16:46:38 -08:00
class FunctionGraph(ParametricFunction):
2016-02-27 16:32:53 -08:00
CONFIG = {
2018-01-18 16:46:38 -08:00
"color" : YELLOW,
"x_min" : -FRAME_X_RADIUS,
"x_max" : FRAME_X_RADIUS,
}
def __init__(self, function, **kwargs):
2018-01-18 16:46:38 -08:00
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
2018-01-18 16:46:38 -08:00
def get_function(self):
return self.function
2017-03-14 15:50:16 -07:00
2016-04-19 00:20:19 -07:00