Created
October 13, 2012 02:32
-
-
Save hvnsweeting/3882975 to your computer and use it in GitHub Desktop.
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
def make_bezier(xys): | |
# xys should be a sequence of 2-tuples (Bezier control points) | |
n=len(xys) | |
combinations=pascal_row(n-1) | |
def bezier(ts): | |
# This uses the generalized formula for bezier curves | |
# http://en.wikipedia.org/wiki/B%C3%A9zier_curve#Generalization | |
result=[] | |
for t in ts: | |
tpowers=(t**i for i in range(n)) | |
upowers=reversed([(1-t)**i for i in range(n)]) | |
coefs=[c*a*b for c,a,b in zip(combinations,tpowers,upowers)] | |
result.append( | |
tuple(sum([coef*p for coef,p in zip(coefs,ps)]) for ps in zip(*xys))) | |
return result | |
return bezier | |
def pascal_row(n): | |
# This returns the nth row of Pascal's Triangle | |
result=[1] | |
x,numerator=1,n | |
for denominator in range(1,n//2+1): | |
# print(numerator,denominator,x) | |
x*=numerator | |
x/=denominator | |
result.append(x) | |
numerator-=1 | |
if n&1==0: | |
# n is even | |
result.extend(reversed(result[:-1])) | |
else: | |
result.extend(reversed(result)) | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment