关于Bezier曲线,相信所有接触过的都知道Q(t) = Pi *Cnk * (1-t)^(n-k) * t^k;(i = 0,1,2,3......n)
这里 我们把Cnk * (1-t)^(n-k) * t^k当做调和函数(blendFcn) 其实所谓 blendFcn有点像数学中的权值, 对于控制点,每个点都有一个权值比重, 并由此比重与点的乘积和获得目标点的坐标, 这个就是Bezier曲线的关键所在————————在此感谢学工老师的指导
#include "GL/glut.h"
#include "stdio.h"
/************************************************************************/
/* POINT_DENSITY PER 0-1 */
/************************************************************************/
#define POINT_DENSITY 100
/************************************************************************/
/* here C = new GLfloat[n+1]; */
/************************************************************************/
struct P{
GLfloat x, y, z;
};
void countCnk(GLint *C, GLint n)
{
int i = 0;
C[0] = 1;
for(int k = 1; k <= n; k++)
{
C[k] = C[k-1] * (n - k + 1)/k;
}
// for checking
for(i = 0; i <= n; i++)
{
printf("%3d", i);
}
printf("\n");
for(i = 0; i <= n; i++)
{
printf("%3d", C[i]);
}
printf("\n");
}
/************************************************************************/
/* HERE WE MAKE THE Z == pList.z WITHOUT CARING ABOUT THE BLENDARGS */
/************************************************************************/
void countQt(GLint n, P* pList, GLint*C, GLfloat t, P *result)
{
GLfloat blendArgs;
result->x = result->y = result->z = 0;
for(int k = 0; k <= n; k++)
{
blendArgs = C[k] * pow(t, k) * pow(1-t, n-k);
result->x += pList[k].x * blendArgs;
result->y += pList[k].y * blendArgs;
//result->z += pList[k].z * blendArgs;
result->z = pList[k].z;
}
}
void drawBezier(P *pList, int n)
{
GLint *C = new GLint[n+1];
P tempResult;
GLfloat t;
countCnk(C, n);
glBegin(GL_LINE_STRIP);
for(int i = 0; i < POINT_DENSITY; i++)
{
t = GLfloat(i)/POINT_DENSITY;
printf("drawing the t%1.2f\n", t);
countQt(n, pList, C, t, &tempResult);
glVertex3f(tempResult.x, tempResult.y, tempResult.z);
}
glEnd();
}