Improvements to ParametricFunction

This commit is contained in:
Grant Sanderson
2016-01-04 10:15:39 -08:00
parent 3230c186f4
commit 31b7732163

View File

@ -1,9 +1,8 @@
from helpers import * from scipy import integrate
from helpers import *
from mobject import Mobject, Mobject1D, Mobject from mobject import Mobject, Mobject1D, Mobject
from helpers import *
class FunctionGraph(Mobject1D): class FunctionGraph(Mobject1D):
DEFAULT_CONFIG = { DEFAULT_CONFIG = {
@ -27,35 +26,24 @@ class FunctionGraph(Mobject1D):
]) ])
class ParametricFunction(Mobject): class ParametricFunction(Mobject1D):
DEFAULT_CONFIG = { DEFAULT_CONFIG = {
"color" : WHITE, "start" : 0,
"dim" : 1, "end" : 1,
"expected_measure" : 10.0,
"density" : None
} }
def __init__(self, function, **kwargs): def __init__(self, function, **kwargs):
self.function = function self.function = function
if self.density: Mobject1D.__init__(self, **kwargs)
self.epsilon = 1.0 / self.density
elif self.dim == 1:
self.epsilon = 1.0 / self.expected_measure / DEFAULT_POINT_DENSITY_1D
else:
self.epsilon = 1.0 / np.sqrt(self.expected_measure) / DEFAULT_POINT_DENSITY_2D
Mobject.__init__(self, **kwargs)
def generate_points(self): def generate_points(self):
if self.dim == 1: integral = integrate.quad(
self.add_points([ lambda t : np.linalg.norm(self.function(t)),
self.function(t) self.start, self.end
for t in np.arange(-1, 1, self.epsilon) )
]) length = np.linalg.norm(integral)
if self.dim == 2: epsilon = self.epsilon / length
self.add_points([ t_range = np.arange(self.start, self.end, epsilon)
self.function(s, t) self.add_points([self.function(t) for t in t_range])
for t in np.arange(-1, 1, self.epsilon)
for s in np.arange(-1, 1, self.epsilon)
])
class Axes(Mobject): class Axes(Mobject):