// ef // introducing structures and functions for vectors and matrices // // The origin of this screen is at the center. // This program demonstrates translation examples. // T. Kosaka CS TNCT 1Aug2002 #include #include #include #include "drawtools2D.h" void userdraw(void); void display(void) { glClear( GL_COLOR_BUFFER_BIT); userdraw(); glutSwapBuffers(); } ////////////////////////////////////////////////////////////////// void drawcharX(float x,float y) //文字 x の表示 { drawLine(x,y,x+10,y+12);drawLine(x,y+12,x+10,y); } void drawcharY(float x,float y) //文字 y の表示 { drawLine(x+5,y,x+5,y+7);drawLine(x,y+12,x+5,y+7);drawLine(x+10,y+12,x+5,y+7); } void drawAxes(void) // xy軸の表示 { drawLine(-310,0,310,0);drawLine(310,0,300,5);drawLine(310,0,300,-5); drawcharX(300,-20); drawLine(0,-230,0,230);drawLine(0,230,5,220);drawLine(0,230,-5,220); drawcharY(-20,220); } ////////////////////////////////////////////////////////////////// void userdraw(void) { static int tick=0; int disp=(tick/100)%5; point2D_t ef[10]={ {10,100},{10,10},{30,10},{30,40},{60,40}, {60,60},{30,60},{30,80},{70,80},{70,100} }; vector2D_t vec[10]; matrix2D_t trans,rot,scale,refxaxis,refyaxis,reforg; color_t white={1.,1.,1.}; color_t cyan={0.,1.,1.}; int i,loop; setColor(white); drawAxes(); setColor(cyan); drawPolygon(ef,10); switch(disp) { case 0: glutSetWindowTitle("matrixvector"); break; case 1: //translation glutSetWindowTitle("matrixvector - translation"); trans=translationMTX(62,22); for (i=0;i<10;i++) vec[i]=Point2Vector(ef[i]); for (loop=0;loop<4;loop++) { for (i=0;i<10;i++) vec[i]=multiply(trans,vec[i]); /* for (i=0;i<10;i++) vec[i]=trans*vec[i]; ....これでも同じ意味*/ drawPolygon(vec,10); } break; case 2: //rotation glutSetWindowTitle("matrixvector - rotation"); rot=rotationMTX(1.); for (i=0;i<10;i++) vec[i]=Point2Vector(ef[i]); for (loop=0;loop<4;loop++) { for (i=0;i<10;i++) vec[i]=multiply(rot,vec[i]); /* for (i=0;i<10;i++) vec[i]=rot*vec[i]; ....これでも同じ意味*/ drawPolygon(vec,10); } break; case 3: //scaling glutSetWindowTitle("matrixvector - scaling"); scale=scalingMTX(1.2,1.2); for (i=0;i<10;i++) vec[i]=Point2Vector(ef[i]); for (loop=0;loop<4;loop++) { for (i=0;i<10;i++) vec[i]=multiply(scale,vec[i]); /* for (i=0;i<10;i++) vec[i]=scale*vec[i]; ....これでも同じ意味*/ drawPolygon(vec,10); } break; case 4: //reflection a special case of scaling glutSetWindowTitle("matrixvector - reflection"); refxaxis=scalingMTX(1.,-1.); refyaxis=scalingMTX(-1.,1.); reforg=scalingMTX(-1.,-1.); for (i=0;i<10;i++) { vec[i]=Point2Vector(ef[i]); vec[i]=multiply(refxaxis,vec[i]); /* vec[i]=refxaxis*vec[i] ....これでも同じ意味*/ } drawPolygon(vec,10); for (i=0;i<10;i++) { vec[i]=Point2Vector(ef[i]); vec[i]=multiply(refyaxis,vec[i]); /* vec[i]=refyaxis*vec[i] ....これでも同じ意味*/ } drawPolygon(vec,10); for (i=0;i<10;i++) { vec[i]=Point2Vector(ef[i]); vec[i]=multiply(reforg,vec[i]); /* vec[i]=reforg*vec[i] ....これでも同じ意味*/ } drawPolygon(vec,10); break; default: break; } tick++; } int main(int argc, char **argv) { glutInit(&argc,argv); glutInitDisplayMode ( GLUT_DOUBLE | GLUT_RGB ); glutInitWindowPosition(100,100); glutInitWindowSize(640,480); glutCreateWindow ("matrixvector"); glClearColor(0.0, 0.0, 0.0, 0.0); gluOrtho2D(-320., 320., -240.0, 240.0); // Define the dimensions of the Orthographic Viewing Volume glutIdleFunc(display); // idle event call back glutDisplayFunc(display); glutMainLoop(); return 0; }