3b1b-manim/topics/functions.py

66 lines
1.5 KiB
Python
Raw Normal View History

2016-01-04 10:15:39 -08:00
from scipy import integrate
2016-04-19 00:20:19 -07:00
from mobject.vectorized_mobject import VMobject
2016-01-04 10:15:39 -08:00
from helpers import *
2016-04-19 00:20:19 -07:00
class FunctionGraph(VMobject):
2016-02-27 16:32:53 -08:00
CONFIG = {
2017-03-14 15:50:16 -07:00
"color" : YELLOW,
2016-04-19 00:20:19 -07:00
"x_min" : -SPACE_WIDTH,
"x_max" : SPACE_WIDTH,
"num_steps" : 20,
}
def __init__(self, function, **kwargs):
self.function = function
2016-04-19 00:20:19 -07:00
VMobject.__init__(self, **kwargs)
def generate_points(self):
2017-03-14 15:50:16 -07:00
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]
2016-04-19 00:20:19 -07:00
self.set_anchor_points([
2017-03-14 15:50:16 -07:00
x*RIGHT + y*UP
for x, y in zip(x_values, y_values)
2016-04-19 00:20:19 -07:00
], mode = "smooth")
2016-09-16 14:06:44 -07:00
def get_function(self):
return self.function
2016-04-19 00:20:19 -07:00
class ParametricFunction(VMobject):
2016-02-27 16:32:53 -08:00
CONFIG = {
2016-04-19 00:20:19 -07:00
"t_min" : 0,
"t_max" : 1,
2017-01-05 11:56:52 -08:00
"num_anchor_points" : 10,
}
def __init__(self, function, **kwargs):
self.function = function
2016-04-19 00:20:19 -07:00
VMobject.__init__(self, **kwargs)
def generate_points(self):
2017-03-14 15:50:16 -07:00
t_values = np.linspace(
self.t_min, self.t_max, self.num_anchor_points
)
2017-03-21 15:53:41 -07:00
points = np.array(map(self.function, t_values))
2017-03-14 15:50:16 -07:00
okay_indices = np.apply_along_axis(np.all, 1, np.isfinite(points))
2017-03-21 15:53:41 -07:00
points = points[okay_indices]
2017-03-14 15:50:16 -07:00
self.set_anchor_points(points, mode = "smooth")
2016-04-19 00:20:19 -07:00