我已经成功地实施二次和三次贝塞尔curves.They是非常简单的,因为我们有一个公式。 现在我想用泛化到代表n阶贝塞尔曲线:
哪里
和
我使用的是位图库渲染输出,所以这里是我的代码:
// binomialCoef(n, k) = (factorial(n) / (factorial(k) * factorial(n- k)))
unsigned int binomialCoef(unsigned int n, const unsigned int k)
{
unsigned int r = 1;
if(k > n)
return 0;
for(unsigned int d = 1; d <= k; d++)
{
r *= n--;
r /= d;
}
return r;
}
void nBezierCurve(Bitmap* obj, const Point* p, const unsigned int nbPoint, float steps, const unsigned char red, const unsigned char green, const unsigned char blue)
{
int bx1 = p[0].x;
int by1 = p[0].y;
int bx2;
int by2;
steps = 1 / steps;
for(float i = 0; i < 1; i += steps)
{
bx2 = by2 = 0;
for(int j = 0; (unsigned int)j < nbPoint; j++)
{
bx2 += (int)(binomialCoef(nbPoint, j) * pow(1 - i, (float)nbPoint - j) * pow(i, j) * p[j].x);
by2 += (int)(binomialCoef(nbPoint, j) * pow(1 - i, (float)nbPoint - j) * pow(i, j) * p[j].y);
}
bresenhamLine(obj, bx1, by1, bx2, by2, red, green, blue);
bx1 = bx2;
by1 = by2;
}
// curve must end on the last anchor point
bresenhamLine(obj, bx1, by1, p[nbPoint - 1].x, p[nbPoint - 1].y, red, green, blue);
}
这里的设定点来呈现的:
Point ncurv[] = {
20, 200,
70, 300,
200, 400,
250, 200
};
和这里的输出:
红色曲线是三次贝塞尔。 蓝色的应该是4阶贝塞尔,这是一样的三次贝塞尔,但在这种情况下,他们是不一样的?!
编辑:我忘了要注意的是左下角点为(0,0)