Created
March 2, 2015 14:08
-
-
Save yiwenl/b486991143ed9969a8f6 to your computer and use it in GitHub Desktop.
Get Bezier line from points
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
MathUtils = MathUtils || {}; | |
MathUtils.level = function(i) { | |
if(i==0) return 1; | |
else return i*MathUtils.level(i-1); | |
} | |
MathUtils.binCoefficient = function(n, i) { | |
return MathUtils.level(n) / ( MathUtils.level(i) * MathUtils.level(n-i) ); | |
} | |
MathUtils.getBezierPoints = function(points, numSeg) { | |
var bezierPoints = []; | |
numPoints = points.length; | |
var t, tx, ty, tz; | |
var bc, pow1, pow2; | |
for(var i=0; i<numSeg; i++) { | |
t = i/numSeg; | |
tx = ty = tz = 0; | |
for(var j=0; j<points.length; j++) { | |
bc = MathUtils.binCoefficient(numPoints-1, j); | |
pow1 = Math.pow((1-t), numPoints-j-1); | |
pow2 = Math.pow(t, j); | |
tx += bc * pow1 * pow2 * points[j][0]; | |
ty += bc * pow1 * pow2 * points[j][1]; | |
tz += bc * pow1 * pow2 * points[j][2]; | |
} | |
var p = [tx, ty, tz]; | |
var index = Math.floor(points.length * t); | |
if(index == points.length) index = points.length-1; | |
p.distance = points[index].distance; | |
bezierPoints.push(p); | |
} | |
return bezierPoints; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment