math - Blending Function/Bezier -


am calculating bezier blend wrong? appreciated. thank much.

double bezierblend(int i, double u, int m) {           double blend = 1;    blend = factorial(m) * pow(u, i) * pow(1 - u, (m - i)) / (factorial(i) * factorial(m - i));    return blend; } 

here's sample compute bezier blend function, following directly formulation:

double choose( long n, long k ) {     long j;     double a;      = 1;     (j = k + 1; j <= n; j++)         *= j;     (j = 1; j <= n - k; j++)         /= j;     return a; };  double bezierblend( int i, double t, int n )  {     return choose( n, ) * pow(1 - t, n - i) * pow( t, ); } 

for applications though, computing powers , binomial coefficients each time absurdly inefficient. in typical applications, degree of curve constant (e.g., 2 quadratic or 3 cubic), , can compute function more efficiently pre-expanding formula. here's example cubic curves:

double bezcoef(int i, double t) {   double tmp = 1-t;   switch (i)   {   case 0: return tmp*tmp*tmp;    case 1: return 3*tmp*tmp*t;    case 2: return 3*tmp*t*t;    case 3: return t*t*t;   }   return 0; // not reached } 

Comments