From 31b77321636fb6f3d63aaa3130436173f8a2bf53 Mon Sep 17 00:00:00 2001 From: Grant Sanderson Date: Mon, 4 Jan 2016 10:15:39 -0800 Subject: [PATCH] Improvements to ParametricFunction --- topics/functions.py | 40 ++++++++++++++-------------------------- 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/topics/functions.py b/topics/functions.py index 3c1c8011..6e67c9f5 100644 --- a/topics/functions.py +++ b/topics/functions.py @@ -1,9 +1,8 @@ -from helpers import * - -from helpers import * +from scipy import integrate from mobject import Mobject, Mobject1D, Mobject +from helpers import * class FunctionGraph(Mobject1D): DEFAULT_CONFIG = { @@ -27,35 +26,24 @@ class FunctionGraph(Mobject1D): ]) -class ParametricFunction(Mobject): +class ParametricFunction(Mobject1D): DEFAULT_CONFIG = { - "color" : WHITE, - "dim" : 1, - "expected_measure" : 10.0, - "density" : None + "start" : 0, + "end" : 1, } def __init__(self, function, **kwargs): self.function = function - if self.density: - 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) + Mobject1D.__init__(self, **kwargs) def generate_points(self): - if self.dim == 1: - self.add_points([ - self.function(t) - for t in np.arange(-1, 1, self.epsilon) - ]) - if self.dim == 2: - self.add_points([ - self.function(s, t) - for t in np.arange(-1, 1, self.epsilon) - for s in np.arange(-1, 1, self.epsilon) - ]) + integral = integrate.quad( + lambda t : np.linalg.norm(self.function(t)), + self.start, self.end + ) + length = np.linalg.norm(integral) + epsilon = self.epsilon / length + t_range = np.arange(self.start, self.end, epsilon) + self.add_points([self.function(t) for t in t_range]) class Axes(Mobject):