資源描述:
《貝塞爾曲線的生成(可調(diào)整控制點(diǎn))》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫(kù)。
1、//homework3_1.cpp:Definestheentrypointfortheconsoleapplication.//#include"stdafx.h"#include#include#include#include#include#include#includeintwide,height;intlefthit=0;floatx[4]={25,100,150,250},y[4]={25,125,175,50};voidr
2、eshape(intw,inth){wide=w;height=h;glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(-w/2,w/2,-h/2,h/2);glMatrixMode(GL_MODELVIEW);glLoadIdentity();}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;retu
3、rns1/s2;}floatfang(floatn,intk){if(k==0)return1;returnpow(n,k);}floatbenkn(intn,intk,floatt){returnzuhe(n,k)*fang(t,k)*fang(1-t,n-k);}voidInit(void){glClearColor(1.0,1.0,1.0,0.0);}voiddisplay(void){glClear(GL_COLOR_BUFFER_BIT);glPointSize(8.0);glColor3f(1.0f,0.0f,0.0f);floatx1[11],y1[11]
4、,t[11]={0},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,2,t[i])+y[3]*benkn(3,3,t[i]);}glBegin(GL_POINTS);glVertex2f(x[0
5、],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();}voidmouse(intkey,intstate,intm,intn){if(lefthit==0)glClear(GL_COLOR_BUFFER_BIT);inti=0;glColor3f(1.0f,0.0f,0.0f);if(key==GL
6、UT_LEFT_BUTTON&&state==GLUT_DOWN){glBegin(GL_POINTS);for(i=lefthit;i<=3;i++){x[i]=(m-wide/2);y[i]=(height/2-n);glVertex2f(x[i],y[i]);printf("%f%f",x[i],y[i]);}glEnd();glFlush();lefthit++;if(lefthit>=4){display();lefthit=0;}}}voidmain(intargc,char**argv){glutInit(&argc,argv);glutInitDis
7、playMode(GLUT_SINGLE
8、GLUT_RGB);glutInitWindowSize(640,480);glutInitWindowPosition(100,150);glutCreateWindow("貝塞爾曲線");glutReshapeFunc(reshape);glutDisplayFunc(display);glutMouseFunc(mouse);Init();glutMainLoop();}