// homework3_1.cpp : Defines the entry point for the console application.
//
#include \"stdafx.h\"
#include #include #include #include #include #include #include int wide,height; int lefthit=0; float x[4]={25,100,150,250},y[4]={25,125,175,50}; void reshape(int w, int h) { 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(); } 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 Init(void) { glClearColor(1.0,1.0,1.0,0.0); } void display(void) { glClear(GL_COLOR_BUFFER_BIT); glPointSize(8.0); glColor3f(1.0f,0.0f,0.0f); float x1[11],y1[11],t[11]={0},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]); } 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 mouse(int key,int state,int m,int n) { if (lefthit==0) glClear(GL_COLOR_BUFFER_BIT); int i=0; glColor3f(1.0f,0.0f,0.0f); if (key==GLUT_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\\n\ } glEnd(); glFlush(); lefthit++; if (lefthit>=4) { display(); lefthit=0; } } } void main(int argc,char** argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(640,480); glutInitWindowPosition(100,150); glutCreateWindow(\"贝塞尔曲线\"); glutReshapeFunc(reshape); glutDisplayFunc(display); glutMouseFunc(mouse); Init(); glutMainLoop(); } 因篇幅问题不能全部显示,请点此查看更多更全内容