2018-12-24 12:37:51 -08:00
|
|
|
from manimlib.constants import *
|
|
|
|
from manimlib.mobject.types.vectorized_mobject import VMobject
|
|
|
|
from manimlib.utils.config_ops import digest_config
|
2015-10-27 21:00:50 -07:00
|
|
|
|
|
|
|
|
2018-01-18 16:46:38 -08:00
|
|
|
class ParametricFunction(VMobject):
|
2016-02-27 16:32:53 -08:00
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"t_min": 0,
|
|
|
|
"t_max": 1,
|
|
|
|
"num_anchor_points": 100,
|
2015-10-27 21:00:50 -07:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2015-10-27 21:00:50 -07:00
|
|
|
def __init__(self, function, **kwargs):
|
2015-10-28 16:03:33 -07:00
|
|
|
self.function = function
|
2016-04-19 00:20:19 -07:00
|
|
|
VMobject.__init__(self, **kwargs)
|
2015-10-27 21:00:50 -07:00
|
|
|
|
|
|
|
def generate_points(self):
|
2018-04-06 13:58:59 -07:00
|
|
|
n_points = 3 * self.num_anchor_points - 2
|
2018-01-18 16:46:38 -08:00
|
|
|
self.points = np.zeros((n_points, self.dim))
|
2018-04-06 13:58:59 -07:00
|
|
|
self.points[:, 0] = np.linspace(
|
2018-01-18 16:46:38 -08:00
|
|
|
self.t_min, self.t_max, n_points
|
|
|
|
)
|
2018-04-06 13:58:59 -07:00
|
|
|
# VMobject.apply_function takes care of preserving
|
|
|
|
# desirable tangent line properties at anchor points
|
|
|
|
self.apply_function(lambda p: self.function(p[0]))
|
|
|
|
|
2015-10-27 21:00:50 -07:00
|
|
|
|
2018-01-18 16:46:38 -08:00
|
|
|
class FunctionGraph(ParametricFunction):
|
2016-02-27 16:32:53 -08:00
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"color": YELLOW,
|
|
|
|
"x_min": -FRAME_X_RADIUS,
|
|
|
|
"x_max": FRAME_X_RADIUS,
|
2015-10-27 21:00:50 -07:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2015-10-27 21:00:50 -07:00
|
|
|
def __init__(self, function, **kwargs):
|
2018-01-18 16:46:38 -08:00
|
|
|
digest_config(self, kwargs)
|
2018-04-06 13:58:59 -07:00
|
|
|
|
|
|
|
def parametric_function(t):
|
|
|
|
return t * RIGHT + function(t) * UP
|
2018-01-18 16:46:38 -08:00
|
|
|
ParametricFunction.__init__(
|
2018-04-06 13:58:59 -07:00
|
|
|
self,
|
2018-01-18 16:46:38 -08:00
|
|
|
parametric_function,
|
2018-04-06 13:58:59 -07:00
|
|
|
t_min=self.x_min,
|
|
|
|
t_max=self.x_max,
|
2018-01-18 16:46:38 -08:00
|
|
|
**kwargs
|
|
|
|
)
|
2015-10-28 16:03:33 -07:00
|
|
|
self.function = function
|
2015-10-27 21:00:50 -07:00
|
|
|
|
2018-01-18 16:46:38 -08:00
|
|
|
def get_function(self):
|
|
|
|
return self.function
|