// Demo for ECE660, 9/21/98, F.S.Hill,Jr. // bare bones Canvas class.. /* rotating rosette of numSides sides. Increase/decrease numSides with 'n"/'N', and increase/decrease rotation speed with 'r'/'R' */ //#include #include #include #include #include #include #include typedef struct { float x, y;} tRealPoint; //<<<<<<<<<<<<<<<<<<<<<<<<<<< Canvas class definition >>>>>>>>>>> class Canvas { private: tRealPoint CP; // current position in world public: Canvas(){CP.x = CP.y = 0.0;};// constructor void lineTo(float x, float y); void moveTo(float x, float y){ CP.x = x; CP.y = y;} }; //######################## GLOBALS ################################# int screenWidth = 480, screenHeight = 480; Canvas cvs; // create a global instance int numSides = 11; float angle = 0.0, delAngle = 0; FILE *fp; float f1,f2,f3,f4; int T1=0,T11=0,T2=0,T22=0,T3=480,T33=480,T4=480,T44=480; float x1,y11,x2,y2; int done,disp; float m; int code1[4],code2[4]; //<<<<<<<<<<<<<<<<<<<<<<< lineTo >>>>>>>>>>>>>>>>>>>>>>> void Canvas:: lineTo(float x, float y) { glBegin(GL_LINES); glVertex2f((GLfloat)CP.x, (GLfloat)CP.y); CP.x = x; CP.y = y; glVertex2f((GLfloat)CP.x, (GLfloat)CP.y); glEnd(); glFlush(); } //<<<<<<<<<<<<<<<>>>>>>>>>>>>>> void filename() { char s[20]; cout<<"ENTER THE FILE NAME"<>>>>>>>>>>>>>>>>>>>>>>> //<<<<<<<<<<<<<>>>>>>>>> void encode(int x,int y,int code[]) { if(xT3) code[1]=1; else code[1]=0; if(yT4) code[3]=1; else code[3]=0; } //<<<<<<<<<<<<<< to varify the validity of the point >>>>>>>>> int accept(int c1[],int c2[]) { int i=1; for(int j=0;j<4;j++) { if(c1[j]==1 || c2[j]==1) i=0;} return i; } int reject(int c1[],int c2[]) { int i=0; for(int j=0;j<4;j++) { if(c1[j]==1 && c2[j]==1) i=1;} return i; } //<<<<<<<<<<<<<<>>>>>>>>>>>>>>>> //notion--(x1,y1) is the point outside the clip window. void swap(float &x1,float &y1,float &x2,float &y2,int c1[],int c2[]) { float x,y;int c[4]; if(c1[0]==0 || c1[1]==0 || c1[2]==0 || c1[3]==0) { x=x1;y=y1;c[0]=c1[0];c[1]=c1[1];c[2]=c1[2];c[3]=c1[3]; x1=x2;y1=y2;c1[0]=c2[0];c1[1]=c2[1];c1[2]=c2[2];c1[3]=c2[3]; x2=x;y2=y;c2[0]=c[0];c2[1]=c[1];c2[2]=c[2];c2[3]=c[3]; } } //<<<<<<<<<<<<<<<
>>>>>>>>>>> void clip(float *x1,float *y11,float *x2,float *y2) { done=0;disp=0; while(done==0) { encode((int)*x1,(int)*y11,code1); encode((int)*x2,(int)*y2,code2); if(accept(code1,code2)==1) { done=1;disp=1;} else if(reject(code1,code2)==1) done=1; else { swap(*x1,*y11,*x2,*y2,code1,code2); m=(*y2-*y11)/(*x2-*x1); if(code1[0]==1) { *y11=(int) (*y11+(T1-*x1)*m);*x1=(int)T1;} else if(code1[1]==1) { *y11=(int)(*y11+(T3-*x1)*m);*x1=(int)T3;} else if(code1[2]==1) { *x1=(int)(*x1+(T2-*y11)/m);*y11=(int)T2;} else if(code1[3]==1) { *x1=(int)(*x1+(T4-*y11)/m);*y11=(int)T4;} } } } //<<<<<<<<<<<<<<<<<<<<<<<<<<<<< display >>>>>>>>>>>>>>>>>>>>>> void display(void) { glClear(GL_COLOR_BUFFER_BIT); // clear screen glMatrixMode(GL_MODELVIEW); // set up rotation glLoadIdentity(); char ch; int n1,n; rewind(fp); while((ch=fgetc(fp))!='*'); while((ch=fgetc(fp))=='*'); cvs.moveTo(T11,T22); cvs.lineTo(T33,T22); cvs.lineTo(T33,T44); cvs.lineTo(T11,T44); cvs.lineTo(T11,T22); if(T11>T33){T1=T33;T3=T11;} if(T22>T44){T2=T44;T4=T22;} fscanf(fp,"%f%f%f%f",&f1,&f2,&f3,&f4); fscanf(fp,"%d",&n); for( int j=0;j>>>>>>>>>>>>>>>>> void spinner(void) // the idle function { display(); } //<<<<<<<<<<<<<<<<<<<<< myInit >>>>>>>>>>>>>>>>>> void myInit() { glClearColor(1.0f,1.0f,1.0f,1.0f); // background is white glColor3f(0.0f,0.0f,0.0f); // set color of stuff glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0,480,0,480); // set window glViewport(0,0,480,480); //the default } //<<<<<<<<<<<<<<<<<<<<< myInit1 >>>>>>>>>>>>>>>>>> void myInit1() { glClearColor(1.0f,1.0f,1.0f,1.0f); // background is white glColor3f(0.0f,0.0f,0.0f); // set color of stuff glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0,480,0,480); // set window glViewport(0,0,(int)(f3-f1)*480,(int)(f4-f2)*480); //the default } //<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>> void mouse(int button , int state ,int x ,int y) { cout<>>>>>>>>>>>>>>>>>>>>>>>> void motion(int x,int y) { T3=T33=x;T4=T44=480-y; } //<<<<<<<<<<<<<<<<<<<<<< main >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> void main(int argc, char **argv) { filename(); glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(screenWidth, screenHeight); glutInitWindowPosition(50, 100); glutCreateWindow("here we go"); glutDisplayFunc(display); glutMouseFunc(mouse); glutMotionFunc(motion); glutIdleFunc(spinner); myInit(); glutMainLoop(); } //<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.