第1页共4页实验四Bezier曲线的绘制1.实验目的练习Bezier曲线的绘制和deCasteljau算法。2.实验内容和要求按要求完成如下一个作业,提交纸质实验报告,同时提交实验报告和代码的电子版。实现Bezier曲线的deCasteljau递推算法,能够对任意介于0和1之间的参数t计算Bezier曲线上的点,然后依次连接这些点生成Bezier曲线。要求:对[0,1]参数区间进行100等分。控制点的数目至少为5个,即Bezier曲线的次数不低于4次。deCasteljau算法用一个函数单独实现。绘制Bezier曲线的同时还要绘制其控制多边形。至少绘制两条Bezier曲线,具有不同的次数,颜色和曲线宽度。3.算法描述BezierCurve(贝塞尔曲线)是应用于二维图形应用程序的数学曲线。曲线定义:起始点、终止点、控制点。通过调整控制点,贝塞尔曲线的形状会发生变化。1962年,法国数学家PierreBezier第一个研究了这种矢量绘制曲线的方法,并给出了详细的计算公式,因此按照这样的公式绘制出来的曲线就用他的姓氏来命名,称为贝塞尔曲线。以下公式中:B(t)为t时间下点的坐标;P0为起点,Pn为终点,Pi为控制点。一阶贝塞尔曲线如下,意义由P0至P1的连续点,描述的是一条线段:二阶贝塞尔曲线(抛物线:P1-P0为曲线在P0处的切线):第2页共4页原理:由P0至P1的连续点Q0,描述一条线段。由P1至P2的连续点Q1,描述一条线段。由Q0至Q1的连续点B(t),描述一条二次贝塞尔曲线。4.源程序代码#includeC:\Include\GL\glut.h#includestdlib.h#includemath.hGLsizeiwinWidth=600,winHeight=600;GLfloatxwcMin=-150.0,xwcMax=150.0;GLfloatywcMin=-300.0,ywcMax=300.0;classwcPt3D{public:GLfloatx,y,z;};voidinit(){glClearColor(1.0,1.0,1.0,1.0);}voidplotPoint(wcPt3DbezCurevePt){glBegin(GL_POINTS);glVertex2f(bezCurevePt.x,bezCurevePt.y);glEnd();}voidbinomiaCoeffs(GLintn,GLint*C){GLintk,j;for(k=0;k=n;k++)C[k]=1;for(j=n;j=k+1;j--)C[k]*=j;for(j=n-k;j=2;j--)C[k]/=j;}voidcomputeBezPt(GLfloatu,wcPt3D*bezPt,GLintnCtrlPts,wcPt3D*CtrlPts,GLint*C){GLintk,n=nCtrlPts-1;GLfloatbezBlendFcn;bezPt-x=bezPt-y=bezPt-z=0.0;for(k=0;knCtrlPts;k++){bezBlendFcn=C[k]*pow(u,k)*pow(1-u,n-k);bezPt-x+=CtrlPts[k].x*bezBlendFcn;bezPt-y+=CtrlPts[k].y*bezBlendFcn;bezPt-z+=CtrlPts[k].z*bezBlendFcn;}}voidbezier(wcPt3D*ctrlPts,GLintnCtrlPts,GLintnBezCurvePts){第3页共4页wcPt3DbezCurvePt;GLfloatu;GLint*C,k;C=newGLint[nCtrlPts];binomiaCoeffs(nCtrlPts-1,C);for(k=0;k=nBezCurvePts;k++){u=GLfloat(k)/GLfloat(nBezCurvePts);computeBezPt(u,&bezCurvePt,nCtrlPts,ctrlPts,C);plotPoint(bezCurvePt);}delete[]C;}voiddisplayFcn(void){GLintnCtrlPts=5,nCtrlPts2=6,nBezCurvePts=1000;wcPt3DctrlPts[5]={{-135.0,-59.0,0.0},{-59.0,95.0,0.0},{0.0,-40.0,0.0},{70.0,120.0,0.0},{78,-125.0,0.0}};wcPt3DctrlPts2[6]={{-118.0,20.0,0.0},{-85.0,45.0,0.0},{-26.0,-126.0,0.0},{38.0,88.0,0.0},{58.0,188.0,0.0},{108.0,98.0,0.0}};glClear(GL_COLOR_BUFFER_BIT);glPointSize(6);glColor3f(0.0,1.0,1.0);bezier(ctrlPts,nCtrlPts,nBezCurvePts);glPointSize(5);glColor3f(1.0,0.0,1.0);bezier(ctrlPts2,nCtrlPts2,nBezCurvePts);glColor3f(0.0,0.0,1.0);glBegin(GL_LINES);glVertex2f(-135.0,-59.0);glVertex2f(-59.0,95.0);glVertex2f(-59.0,95.0);glVertex2f(0.0,-40.0);glVertex2f(0.0,-40.0);glVertex2f(70.0,120.0);glVertex2f(70.0,120.0);glVertex2f(78.0,-125.0);glVertex2f(-118.0,20.0);glVertex2f(-85.0,45.0);glVertex2f(-85.0,45.0);glVertex2f(-26.0,-126.0);glVertex2f(-26.0,-126.0);glVertex2f(38.0,88.0);glVertex2f(38.0,88.0);glVertex2f(58.0,188.0);glVertex2f(58.0,188.0);glVertex2f(108.0,98.0);glEnd();glFlush();}第4页共4页voidwinReshapeFcn(GLintnewWidth,GLintnewHeight){glViewport(0,0,newWidth,newHeight);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(xwcMin,xwcMax,ywcMin,ywcMax);glClear(GL_COLOR_BUFFER_BIT);}voidmain(intargc,char*argv[]){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowPosition(50,50);glutInitWindowSize(winWidth,winHeight);glutCreateWindow(yxl实验四Bezier曲线);init();glutDisplayFunc(displayFcn);glutReshapeFunc(winReshapeFcn);glutMainLoop();}5.实验结果6.实验体会最后一次实验报告了,老师要求我们做Bezier曲线,需要我们对函数去理解的一次实验,对于数学比较差的我来说还是很有困难的,理解起来比较吃力。说真的,这学期因为考研,上的一些课程都没有认真自己课下多做做,还是挺愧疚与遗憾的,毕竟像计算机图形学这些课程,还有虚拟现实技术等,都很实用的,信息技术领域很是热门,之后有时间了一定要好好再学习这些课,感谢老师的讲解。