Am I calculating the Bezier blend wrong? Any help would be appreciated. Thank you very much.
double bezierBlend(int i, double u, int 开发者_如何学Cm) {
double blend = 1;
blend = factorial(m) * pow(u, i) * pow(1 - u, (m - i)) / (factorial(i) * factorial(m - i));
return blend;
}
Here's a sample to compute the Bezier blend function, following directly from the formulation:
double choose( long n, long k )
{
long j;
double a;
a = 1;
for (j = k + 1; j <= n; j++)
a *= j;
for (j = 1; j <= n - k; j++)
a /= j;
return a;
};
double bezierBlend( int i, double t, int n )
{
return choose( n, i ) * pow(1 - t, n - i) * pow( t, i );
}
For most applications though, computing the powers and the binomial coefficients each time is absurdly inefficient. In typical applications, the degree of the curve is constant (e.g., 2 for quadratic or 3 for cubic), and you can compute the function much more efficiently by pre-expanding the formula. Here's an example for 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
}
精彩评论