資源描述:
《用C 和openGL實現(xiàn)貝塞爾曲線的生成》由會員上傳分享,免費在線閱讀,更多相關內容在應用文檔-天天文庫。
1、C和openGL環(huán)境下編程生成貝塞爾曲線:#include#include#include#include#include#includevoidsetWindow(GLdoubleleft,GLdoubleright,GLdoublebuttom,GLdoubletop){glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(left,right,buttom,top);}void
2、setViewport(GLintleft,GLintright,GLintbottom,GLinttop){glViewport(left,bottom,right-left,top-bottom);}intzuhe(intn,intk){inti,s1,s2;s1=1;s2=1;if(k==0)return1;for(i=n;i>=n-k+1;i--)s1=s1*i;for(i=k;i>=2;i--)s2=s2*i;returns1/s2;}floatfang(floatn,intk){if(k==0)return1;returnpow(n,k);}
3、floatbenkn(intn,intk,floatt){returnzuhe(n,k)*fang(t,k)*fang(1-t,n-k);}voidmyInit(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);}voidmyDisplay(void){glClear(GL_COLOR_BUFFE
4、R_BIT);floatt[11]={0},x[4]={1,4,6,10},y[4]={1,5,7,2},x1[11],y1[11],s=0.0;inti;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
5、,2,t[i])+y[3]*benkn(3,3,t[i]);}printf("%f,%f,%f,%f",x[0],x[1],x[2],x[3]);printf("%f,%f,%f,%f",y[0],y[1],y[2],y[3]);for(i=0;i<11;i++){printf("%5.2f",t[i]);}printf("");for(i=0;i<11;i++){printf("%5.2f",x1[i]);}printf("");for(i=0;i<11;i++){printf("%5.2f",y1[i]);}glBegin(GL_PO
6、INTS);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();}voidmain(intargc,char**argv){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE
7、GLUT_RGB);glu
8、tInitWindowSize(640,480);glutInitWindowPosition(100,150);glutCreateWindow("myfirstattempt");glutDisplayFunc(myDisplay);myInit();setWindow(0.0,11.0,0.0,8.0);setViewport(0,640,0,480);glutMainLoop();}