Created
March 5, 2012 21:58
-
-
Save potch/1981439 to your computer and use it in GitHub Desktop.
bezier curve function generator returns a function(t) that accepts [0..1] and returns an [x,y] pair at that point.
This file contains hidden or 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
function bezzy() { | |
var pts = Array.prototype.slice.call(arguments), | |
funcs = []; | |
function b1(p1, p2) { | |
return function(t) { | |
var i = 1 - t; | |
return [p1[0]*i+p2[0]*t, p1[1]*i+p2[1]*t]; | |
} | |
} | |
function b2(p1, p2, p3) { | |
return function(t) { | |
return b1(b1(p1, p2)(t), b1(p2, p3)(t))(t); | |
} | |
} | |
function b3(p1, p2, p3, p4) { | |
return function(t) { | |
return b1(b2(p1, p2, p3)(t), b2(p2, p3, p4)(t))(t); | |
} | |
} | |
switch (pts.length) { | |
case 4: | |
return b3.apply(null, pts); | |
case 3: | |
return b2.apply(null, pts); | |
case 2: | |
return b1.apply(null, pts); | |
default: | |
throw "unrecognized number of points!"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment