logo资料库

用C++和openGL实现贝塞尔曲线的生成.doc

第1页 / 共3页
第2页 / 共3页
第3页 / 共3页
资料共3页,全文预览结束
C 和 openGL 环境下编程生成贝塞尔曲线: #include #include #include #include #include #include void setWindow(GLdouble left,GLdouble right,GLdouble buttom,GLdouble top) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(left,right,buttom,top); } void setViewport(GLint left,GLint right,GLint bottom,GLint top) { glViewport(left,bottom,right-left,top-bottom); } int zuhe(int n,int k) { int i,s1,s2; s1=1; s2=1; if(k==0) return 1; for(i=n;i>=n-k+1;i--) s1=s1*i; for(i=k;i>=2;i--) s2=s2*i; return s1/s2; } float fang(float n,int k) { if(k==0) return 1; return pow(n,k); } float benkn(int n,int k,float t) { return zuhe(n,k)*fang(t,k)*fang(1-t,n-k); } void myInit(void)
{ glClearColor(1.0,1.0,1.0,0.0); glColor3f(1.0f,0.0f,0.0f); glPointSize(4.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0,640.0,0.0,480.0); } void myDisplay(void) { glClear(GL_COLOR_BUFFER_BIT); float t[11]={0},x[4]={1,4,6,10},y[4]={1,5,7,2},x1[11],y1[11],s=0.0; int i; for(i=1;i<11;i++) {s=s+0.1;t[i]=s;} for(i=0;i<11;i++) { x1[i]=x[0]*benkn(3,0,t[i])+x[1]*benkn(3,1,t[i])+x[2]*benkn(3,2,t[i])+x[3]*benkn(3,3,t[i]); y1[i]=y[0]*benkn(3,0,t[i])+y[1]*benkn(3,1,t[i])+y[2]*benkn(3,2,t[i])+y[3]*benkn(3,3,t[i]); } printf("%f,%f,%f,%f\n",x[0],x[1],x[2],x[3]); printf("%f,%f,%f,%f\n",y[0],y[1],y[2],y[3]); for(i=0;i<11;i++) { printf("%5.2f",t[i]); } printf("\n"); for(i=0;i<11;i++) { printf("%5.2f",x1[i]); } printf("\n"); for(i=0;i<11;i++) { printf("%5.2f",y1[i]); }
glBegin(GL_POINTS); glVertex2f(x[0],y[0]); glVertex2f(x[1],y[1]); glVertex2f(x[2],y[2]); glVertex2f(x[3],y[3]); glEnd(); glBegin(GL_LINE_STRIP); for(i=0;i<11;i++) glVertex2f(x1[i],y1[i]); glEnd(); glFlush(); } void main(int argc,char** argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(640,480); glutInitWindowPosition(100,150); glutCreateWindow("my first attempt"); glutDisplayFunc(myDisplay); myInit(); setWindow(0.0,11.0,0.0,8.0); setViewport(0,640,0,480); glutMainLoop(); }
分享到:
收藏