// PROGRAM3.C // // // PROGRAM BY LOUIS GUZIK FEBUARY 27, 1996 // // SQUARE WITH LETTER A INSIDE, DOING STUFF // // // GRADE:_____ // // // _______________________________________________________________ // // _______________________________________________________________ // // _______________________________________________________________ // // _______________________________________________________________ // // ///////////////////////////////////////////////////////////////////////////// #include #include #include #include #include #include #include #include #include "d:\compiler\clang\pitt\2078\turboc\mouse.h" #include "d:\compiler\clang\pitt\2078\turboc\matrix.h" #include "d:\compiler\clang\pitt\2078\turboc\3dtransf.h" #define ESCAPE 27 #define YES 1 #define NO 0 ///////////////////////////////////////////////////////////////////////////// // GOBAL VARS int zdegrees = 30; // DEGREES FOR Z AXIS int i, j, k, x; // VARS USED AS TEMP HOLDERS int gdriver, gmode, errorcode; // VIDEO GRAPHICS VARS int maxX, maxY, midX, midY; // SCREEN COORDS VARS int xr, yr; // SCREEN ASPECT RATIO STUFF double Aratio; int Mouse_loaded=0, Mx_pos=0, My_pos=0, Mbutton=0, Mcount=0; int page = 0; int flag = 0; char title[ 132 ]; #define npts 13 // 0 1 2 3 4 5 6 7 8 9 10 11 12 float points[npts][4]={ 0,0,0,1, 20,0,0,1, 20,20,0,1, 0,20,0,1, 0,0,20,1, 20,0,20,1, 20,20,20,1, 0,20,20,1, 5,5,0,1, 10,15,0,1, 15,5,0,1, 13,10,0,1, 8,10,0,1 }; float extrapoints[npts][4]={ 10,10,10,1, 30,10,10,1, 30,30,10,1, 10,30,10,1, 10,10,30,1, 30,10,30,1, 30,30,30,1, 10,30,30,1, 15,15,10,1, 20,25,10,1, 25,15,10,1, 22,20,10,1, 18,20,10,1 }; float newpoints[npts][4]; float x_scal=3.0,y_scal=3.0,z_scal=3.0,all_scal=1.0; float x_rot=45.0,y_rot=45,z_rot=45.0; ///////////////////////////////////////////////////////////////////////////// // FUNCTIONS int SetupGraphicsMode( void ); int SetLineWidth( int Pwide ); void DrawTitle( int Pwide, int Color ); void DrawGraph( int Pwide, int Color, int zdegrees ); void DrawObject( float p[13][4], int Color, int fill ); void NewObject( void ); ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// // MAIN int main( int argc, char *argv[] ) { int menu = 1; char tmp[ 32 ]; strcpy( title, "Orginial Figure" ); flushall(); // SETUP GRAPHICS SetupGraphicsMode(); cleardevice(); setbkcolor( BLUE ); // SETUP MOUSE Mouse_loaded = checkformouse(); if( Mouse_loaded) { Mx_pos = maxX-50; My_pos = 50; limitmouse( 0, 0, maxX, maxY ); positionmouse( Mx_pos, My_pos ); showmouse(); } rotate3d( points, newpoints, 0, 1, npts ); ///////////////////////////////////////////////////////////////////////////// // DO THE ASSIGNMENT do { setactivepage( page ); DrawTitle( 1, WHITE ); DrawObject( newpoints, RED, YES ); DrawGraph( 1, WHITE, zdegrees ); // DO THE I/O STUFF if( kbhit() ) { if( getch() == ESCAPE ) menu = 0; NewObject(); } if( Mouse_loaded) { buttonpress( &Mbutton, &Mcount, &Mx_pos, &My_pos ); if( Mcount != 0 ) NewObject(); } // CHANGE PAGES setvisualpage( page ); page++; if( page > 1 ) page = 0; } while( menu ); ///////////////////////////////////////////////////////////////////////////// /* clean up, then exit */ cleardevice(); closegraph(); printf( "%s", "\n End of Graphics. LGG 1996 \n\n" ); flushall(); return 0; } // END - MAIN() ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// int SetupGraphicsMode( void ) { /* report any registration errors */ errorcode = registerbgidriver( EGAVGA_driver ); if (errorcode < 0) { printf("Graphics reg error: %s\n", grapherrormsg(errorcode)); printf("Press any key to exit."); getch(); exit(1); /* terminate with an error code */ } /* request auto detection */ // gdriver = DETECT; /* select a driver and mode that supports multiple pages. */ gdriver = VGA; gmode = VGAMED; /* initialize graphics, local variables*/ initgraph( &gdriver, &gmode, "" ); /* read result of initialization */ errorcode = graphresult(); if (errorcode != grOk) /* an error occurred */ { printf( "Graphics init error: %s\n", grapherrormsg(errorcode)); printf( "Program must be run with VGA monitor ! ! ! \n" ); printf( "Press any key to exit:"); getch(); exit(errorcode); /* terminate with error code */ } maxX = getmaxx(); maxY = getmaxy(); midX = maxX / 2; midY = maxY / 2; return( gmode ); } // END - DetermineGraphicMode( void ) ///////////////////////////////////////////////////////////////////////////// void DrawTitle( int Pwide, int Color ) { int i; int old_color; int old_line_width; char tmp[ 256 ]; char buffer[ 256 ] = "\0"; int mx_pos=-1, my_pos=-1, mbutton=0, mcount=0; if( Mouse_loaded) mousestatus( &mbutton, &mx_pos, &my_pos ); // SET COLOR old_color = getcolor(); setcolor( Color ); old_line_width = SetLineWidth( Pwide ); // ERASE OLD MOUSE POS // setcolor( GREEN ); setcolor( getbkcolor() ); setfillstyle( SOLID_FILL, getbkcolor() ); bar3d( 548, 3, 636, 12, 0, 0); // DRAW THE HEADER setcolor( Color ); strcpy( buffer, "Lou Guzik Feb96 TRANSFORMATION [A] Drv=" ); itoa( gdriver, tmp, 10 ); strcat( buffer, tmp ); strcat( buffer, " Mode=" ); itoa( gmode, tmp, 10 ); strcat( buffer, tmp ); strcat( buffer, " maxX=" ); itoa( maxX, tmp, 10 ); strcat( buffer, tmp ); strcat( buffer, " maxY=" ); itoa( maxY, tmp, 10 ); strcat( buffer, tmp ); strcat( buffer, " x=" ); mx_pos = mx_pos - midX + 1; itoa( mx_pos, tmp, 10 ); strcat( buffer, tmp ); strcat( buffer, " y=" ); my_pos = midY - my_pos - 1; itoa( my_pos, tmp, 10 ); strcat( buffer, tmp ); outtextxy( 6, 4, buffer ); line( 3, 13, maxX-3, 13 ); // LINE UNDER TITLE outtextxy( 6, 15, "Press any Key or Mouse" ); outtextxy( 6, 24, "Press ESC to exit" ); // TITLE OF FIGURE setcolor( getbkcolor() ); bar3d( 5, 33, 300, 44, 0, 0 ); setcolor( Color ); outtextxy( 10, 35, title ); // CLEANUP setcolor( old_color ); SetLineWidth( old_line_width ); } // END - DRAWTITLE() ///////////////////////////////////////////////////////////////////////////// int SetLineWidth( int Pwide ) { // SETS PIXCEL DRAWING WIDTH. VALUES ARE 1 & 3. // RETURNS OLD PIXCEL WIDTH. struct linesettingstype lineinfo; int linestyle; unsigned upattern; int thickness; // SET LINE WIDTH getlinesettings( &lineinfo ); linestyle = lineinfo.linestyle; upattern = lineinfo.upattern; thickness = lineinfo.thickness; setlinestyle( linestyle, upattern, Pwide ); // setlinestyle( SOLID_LINE, upattern, Pwide ); return( thickness ); } ///////////////////////////////////////////////////////////////////////////// void DrawGraph( int Pwide, int Color, int zdegrees ) { int i; int old_color; int old_line_width; double rad = zdegrees / 57.2957797; int r, x, y; int radius = midY * 1.7; int dotline = getbkcolor(); double xpos = cos( rad ); double ypos = sin( rad ); // SET COLOR old_color = getcolor(); setcolor( Color ); old_line_width = SetLineWidth( Pwide ); // DRAW COORD LINES // DRAW X LINE setcolor( Color ); line( 2, midY, maxX-2, midY ); for( i=midX; i0; i-=10 ) line( i, midY-2, i, midY+2 ); // DRAW Y LINE line( midX, 13, midX, maxY-2 ); for( i=midY; i13; i-=10 ) line( midX-2, i, midX+2, i ); // DRAW Z LINE for( r=0; r<=radius; r++ ) { x = r * xpos; // GET X NORMAL COORDS x = x + midX; // CHANGE INTO SCREEN COORDS y = r * ypos; // GET Y NORMAL COORDS y = midY - y; // CHANGE INTO SCREEN COORDS putpixel( x, y, dotline ); // DRAW THE Z DOTTED LINE if( (r%5) == 0 ) if( dotline == getbkcolor() ) dotline = Color; else dotline = getbkcolor(); } for( r=0; r<=radius; r++ ) { x = r * xpos; // GET X IN NORMAL COORDS x = midX - x; // CHANGE INTO SCREEN COORDS y = r * ypos; // GET Y IN NORMAL COORDS y = midY + y; // CHANGE INTO SCREEN COORDS putpixel( x, y, Color ); // DRAW THE Z LINE } // DRAW SCREEN BORDER setcolor( RED ); SetLineWidth( 3 ); line( 0, 0, maxX, 0 ); line( maxX, 0, maxX, maxY ); line( maxX, maxY, 0, maxY ); line( 0, maxY, 0, 0 ); // CLEANUP setcolor( old_color ); SetLineWidth( old_line_width ); } // END - DRAWGRAPH() ///////////////////////////////////////////////////////////////////////////// void DrawObject( float p[13][4], int Color, int fill ) { // 0 1 2 3 //float points[8][4]={ 0,0,0,1, 20,0,0,1, 20,20,0,1, 0,20,0,1, // 4 5 6 7 // 0,0,20,1, 20,0,20,1, 20,20,20,1, 0,20,20,1 }; // mx_pos = mx_pos - midX + 1; // my_pos = midY - my_pos - 1; int x1, y1, x2, y2; double rad = zdegrees / 57.2957797; double xpos = cos( rad ); double ypos = sin( rad ); hidemouse(); // ERASE OLD FLOOD FILL FACE if( fill == NO ) { // DRAW BACK - POINTS 4 TO 6. MAKE BACK SURFACE BACKGROUND COLOR // SO THE FOLLOWING FLOODFILL COLORS IN THE WHOLE FRONT setcolor( getbkcolor() ); // CONNECT FACE TO BACK for( i=0; i<4; i++ ) { x1 = (int) p[i][0] + (p[i][2] * xpos); x1 = midX + x1; y1 = (int) p[i][1] + (p[i][2] * ypos); y1 = midY - y1; x2 = (int) p[i+4][0] + (p[i+4][2] * xpos); x2 = midX + x2; y2 = (int) p[i+4][1] + (p[i+4][2] * ypos); y2 = midY - y2; line( x1, y1, x2, y2 ); } for( i=4; i<7; i++ ) { x1 = (int) p[i][0] + (p[i][2] * xpos); x1 = midX + x1; y1 = (int) p[i][1] + (p[i][2] * ypos); y1 = midY - y1; x2 = (int) p[i+1][0] + (p[i+1][2] * xpos); x2 = midX + x2; y2 = (int) p[i+1][1] + (p[i+1][2] * ypos); y2 = midY - y2; line( x1, y1, x2, y2 ); } x1 = (int) p[4][0] + (p[4][2] * xpos); x1 = midX + x1; y1 = (int) p[4][1] + (p[4][2] * ypos); y1 = midY - y1; line( x1, y1, x2, y2 ); setcolor( RED ); for( i=0; i<3; i++ ) { x1 = (int) p[i][0] + (p[i][2] * xpos); x1 = midX + x1; y1 = (int) p[i][1] + (p[i][2] * ypos); y1 = midY - y1; x2 = (int) p[i+1][0] + (p[i+1][2] * xpos); x2 = midX + x2; y2 = (int) p[i+1][1] + (p[i+1][2] * ypos); y2 = midY - y2; line( x1, y1, x2, y2 ); } x1 = (int) p[0][0] + (p[0][2] * xpos); x1 = midX + x1; y1 = (int) p[0][1] + (p[0][2] * ypos); y1 = midY - y1; line( x1, y1, x2, y2 ); x1 = (int) p[8][0] + (p[8][2] * xpos); x1 = midX + x1; y1 = (int) p[8][1] + (p[8][2] * ypos); y1 = midY - y1; setfillstyle(SOLID_FILL, getbkcolor() ); floodfill( x1, y1, RED ); } setcolor( Color ); // DRAW NEW FACE - POINTS 0 TO 4 for( i=0; i<3; i++ ) { x1 = (int) p[i][0] + (p[i][2] * xpos); x1 = midX + x1; y1 = (int) p[i][1] + (p[i][2] * ypos); y1 = midY - y1; x2 = (int) p[i+1][0] + (p[i+1][2] * xpos); x2 = midX + x2; y2 = (int) p[i+1][1] + (p[i+1][2] * ypos); y2 = midY - y2; line( x1, y1, x2, y2 ); } x1 = (int) p[0][0] + (p[0][2] * xpos); x1 = midX + x1; y1 = (int) p[0][1] + (p[0][2] * ypos); y1 = midY - y1; line( x1, y1, x2, y2 ); // FLOOD FILL FACE if( fill == YES ) { x1 = (int) p[8][0] + (p[8][2] * xpos); x1 = midX + x1; y1 = (int) p[8][1] + (p[8][2] * ypos); y1 = midY - y1; setfillstyle(SOLID_FILL, GREEN ); floodfill( x1, y1, Color ); } // DRAW BACK - POINTS 4 TO 6 for( i=4; i<7; i++ ) { x1 = (int) p[i][0] + (p[i][2] * xpos); x1 = midX + x1; y1 = (int) p[i][1] + (p[i][2] * ypos); y1 = midY - y1; x2 = (int) p[i+1][0] + (p[i+1][2] * xpos); x2 = midX + x2; y2 = (int) p[i+1][1] + (p[i+1][2] * ypos); y2 = midY - y2; line( x1, y1, x2, y2 ); } x1 = (int) p[4][0] + (p[4][2] * xpos); x1 = midX + x1; y1 = (int) p[4][1] + (p[4][2] * ypos); y1 = midY - y1; line( x1, y1, x2, y2 ); // CONNECT FACE TO BACK for( i=0; i<4; i++ ) { x1 = (int) p[i][0] + (p[i][2] * xpos); x1 = midX + x1; y1 = (int) p[i][1] + (p[i][2] * ypos); y1 = midY - y1; x2 = (int) p[i+4][0] + (p[i+4][2] * xpos); x2 = midX + x2; y2 = (int) p[i+4][1] + (p[i+4][2] * ypos); y2 = midY - y2; line( x1, y1, x2, y2 ); } // DRAW THE A if( fill ) setcolor( MAGENTA ); else setcolor( getbkcolor() ); for( i=8; i<12; i++ ) { x1 = (int) p[i][0] + (p[i][2] * xpos); x1 = midX + x1; y1 = (int) p[i][1] + (p[i][2] * ypos); y1 = midY - y1; x2 = (int) p[i+1][0] + (p[i+1][2] * xpos); x2 = midX + x2; y2 = (int) p[i+1][1] + (p[i+1][2] * ypos); y2 = midY - y2; line( x1, y1, x2, y2 ); } showmouse(); } ///////////////////////////////////////////////////////////////////////////// void NewObject( void ) { // ERASE OLD OBJECT setactivepage( 0 ); DrawObject( newpoints, BLUE, NO ); setactivepage( 1 ); DrawObject( newpoints, BLUE, NO ); setactivepage( page ); flag++; /* rtype = type of rotation 1 = x axis 2 = y axis 3 = z axis */ switch( flag ) { // SCALE case 1: strcpy( title, "Scale=3" ); scale3d(points,newpoints,x_scal,y_scal,z_scal,all_scal,npts); break; // ROTATE case 2: strcpy( title, "Scale=3 Rotate on X 285 deg." ); scale3d(points,newpoints,x_scal,y_scal,z_scal,all_scal,npts); rotate3d( newpoints, newpoints, 285, 1, npts ); // looks bad rotate3d( newpoints, newpoints, 270, 1, npts ); break; case 3: strcpy( title, "Scale=3 Rotate on Y 208 deg." ); scale3d(points,newpoints,x_scal,y_scal,z_scal,all_scal,npts); rotate3d( newpoints, newpoints, 208, 2, npts ); // looks bad rotate3d( newpoints, newpoints, 270, 2, npts ); break; case 4: strcpy( title, "Scale=3 Rotate on Z 200 deg." ); scale3d(points,newpoints,x_scal,y_scal,z_scal,all_scal,npts); rotate3d( newpoints, newpoints, 200, 3, npts ); // rotate3d( newpoints, newpoints, 270, 3, npts ); break; /* translate the figure */ /* input: a = amount of x translation b = amount of y translation c = amount of z translation output: t = transformation matrix return: */ case 5: strcpy( title, "Scale=3 Trans on X 40 units" ); scale3d(points,newpoints,x_scal,y_scal,z_scal,all_scal,npts); translate3d(newpoints,newpoints,40,0,0,npts); break; case 6: strcpy( title, "Scale=3 Trans on Y 40 units" ); scale3d(points,newpoints,x_scal,y_scal,z_scal,all_scal,npts); translate3d(newpoints,newpoints,0,40,0,npts); break; case 7: strcpy( title, "Scale=3 Trans on Z 40 units" ); scale3d(points,newpoints,x_scal,y_scal,z_scal,all_scal,npts); translate3d(newpoints,newpoints,0,0,40,npts); break; // REFLECTION case 8: strcpy( title, "Scale=3 Reflect on X" ); scale3d(points,newpoints,x_scal,y_scal,z_scal,all_scal,npts); reflect3d(newpoints,newpoints,1,npts); break; case 9: strcpy( title, "Scale=3 Reflect on Y" ); scale3d(points,newpoints,x_scal,y_scal,z_scal,all_scal,npts); reflect3d(newpoints,newpoints,2,npts); break; case 10: strcpy( title, "Scale=3 Reflect on Z" ); scale3d(points,newpoints,x_scal,y_scal,z_scal,all_scal,npts); reflect3d(newpoints,newpoints,3,npts); break; default: strcpy( title, "Orginial Figure" ); rotate3d( points, newpoints, 0, 1, npts ); flag = 0; break; } } ///////////////////////////////////////////////////////////////////////////// // PROGRAM3.C 2-27-96 by Lou Guzik