它的公式:点击打开链接
http://www.cnblogs.com/zhouxin/p/3436740.html
代码如下:
#include "math.h"
#include "GL/Gl.h"
#include "GL/glut.h"
typedef struct PointTag {
GLfloat x;
GLfloat y;
} Point;
static const Point BezierPoint[4] = {
{-0.8f, -0.5f},
{-0.5f, 0.5f},
{-0.5f, -0.7f},
{ 1.0f, 0.0f},
};
void myInit()
{
glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
glutInitWindowPosition(100, 100);
glutInitWindowSize(700, 400);
glMatrixMode(GL_TEXTURE);
glLoadIdentity(); //Move to the Center
gluOrtho2D(-0.0f, 0.0f, -0.0f, 0.0f);
}
Point drawBezier(Point A,Point B,Point C,Point D,double t)
{
Point P;
P.x=pow((1 - t), 3) * A.x + 3 * t * pow((1 - t), 2) * B.x + 3 * (1 - t) * pow(t, 2) * C.x + pow(t, 3) * D.x;
P.y=pow((1 - t), 3) * A.y + 3 * t * pow((1 - t), 2) * B.y + 3 * (1 - t) * pow(t, 2) * C.y + pow(t, 3) * D.y;
return P;
}
void drawBerize()
{
glClear(GL_LINE_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_LINES);
Point tempPoint = BezierPoint[0];
for (double t = 0.0f;t < 1.0f; t += 0.1f) {
Point endPoint=drawBezier(BezierPoint[0], BezierPoint[1], BezierPoint[2], BezierPoint[3], t);
glVertex2f(tempPoint.x, tempPoint.y);
glVertex2f(endPoint.x, endPoint.y);
tempPoint = endPoint;
}
glEnd();
glFlush();
glutSwapBuffers();
}
void idleFunc() {
drawBerize();
}
int main(int argc, char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(640,480);
glutInitWindowPosition(100,150);
glutCreateWindow("三次Bezier曲线");
glutDisplayFunc(drawBerize);
glutIdleFunc(idleFunc);
myInit();
glutMainLoop();
return 0;
}
例子2:
switch(moveStep) {
case 0:
break;
case 1:
{
if(tParam < 1.0f) {
Point endPoint = drawBezier(BezierPoint[0], BezierPoint[1], BezierPoint[2], BezierPoint[3], tParam);
LineMotion_Init2(&berizeMotion, tempPoint.x, tempPoint.y, endPoint.x, endPoint.y, 1, 10);
moveStep = 2;
tempPoint = endPoint;
}
}
break;
case 2:
{
VINT32 idx = 0;
VINT32 idy = 0;
VINT32 isEnd = LineMotion_Generator2(&berizeMotion, &idx, &idy, time);
if(isEnd == 1) {
tParam += 0.01f;
DEBUG_CONSOLE<<"Over\n";
moveStep = 1;
} else {
DEBUG_CONSOLE<<"X: "<<berizeMotion.iObjCurX<<"\n";
DEBUG_CONSOLE<<"Y: "<<berizeMotion.iObjCurY<<"\n";
test_exit->setX(berizeMotion.iObjCurX);
test_exit->setY(berizeMotion.iObjCurY);
}
}
break;
case 3:
break;
default:
break;
}