Created
September 24, 2010 23:14
-
-
Save jflatow/596216 to your computer and use it in GitHub Desktop.
quadratic polynomial to quadratic bezier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from math import factorial | |
def nchoosek(n, k): | |
return factorial(n) / (factorial(k) * factorial(n - k)) | |
class point(tuple): | |
def __add__(self, other): | |
return point(ps + po for ps, po in zip(self, other)) | |
def __mul__(self, scalar): | |
return point(scalar * p for p in self) | |
def __rmul__(self, scalar): | |
return self * scalar | |
class origin(point): | |
def __add__(self, other): | |
return other | |
def __mul__(self, scalar): | |
return self | |
class bezier(tuple): | |
def __call__(self, t): | |
n = len(self) - 1 | |
return sum((nchoosek(n, i) * pow(1 - t, n - i) * pow(t, i) * p | |
for i, p in enumerate(self)), origin()) | |
class quadratic(tuple): | |
def __call__(self, x): | |
return sum(an * pow(x, n) for n, an in enumerate(self)) | |
def bezier(self, xi, dx): | |
c, b, a = self | |
return bezier((point((xi, self(xi))), | |
point((xi + dx / 2., a * xi**2 + b * (xi + dx / 2.) + c)), | |
point((xi + dx, self(xi + dx))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment