home *** CD-ROM | disk | FTP | other *** search
/ Assembly 1994 - The 3rd Phase / ASMROM94.mdf / sources / cube216c.txt < prev    next >
Text File  |  1994-11-12  |  4KB  |  128 lines

  1. /* (C) Mikko Tommila 1989 */
  2. /* Written in Turbo C 2.0 (C) Borland, Inc.*/
  3. /* Link this with a VGA256.OBJ file if you haven't included it in your Graphics.Lib */
  4. /*VGA256.BGI (C) Borland, Inc.*/
  5.  
  6. #include <graphics.h>
  7. #include <math.h>
  8. #include <stdio.h>
  9.  
  10. void move(float m, float *b, float *c);
  11. void cdecl VGA256_driver(void);
  12. void swap(int *m1,int *m2);
  13.  
  14. main()
  15. {
  16.         int grdrv;  /*graphichs driver*/
  17.         int grmode;  /*& -mode*/
  18.         int xr, yr;
  19.         int nn, ap, bp, r[216];
  20.         float xp, yp;
  21.         int n1, n2, n3; /*something*/
  22.         float xf;
  23.         float xm, ym, zm;  /*momentum on x, y & z circles*/
  24.         typedef struct {
  25.                 float z;
  26.                 float y;
  27.                 float x;
  28.                 } coordinate;
  29.         coordinate cor[216];
  30.         int co[216];
  31.  
  32.         /*some info*/
  33.         puts("To acquire rotation on image:\n");
  34.         puts("E D  around x-axis");
  35.         puts("A S  around y-axis");
  36.         puts("Q W  around z-axis\n\n");
  37.  
  38.         puts("ESC exits !\n\n");
  39.         puts("Press any key");
  40.         do {delay(1);} while(!kbhit());
  41.  
  42.         installuserdriver("VGA256",0);
  43.         registerbgidriver(VGA256_driver);
  44.         grdrv=11;
  45.         grmode=0;
  46.  
  47.         /*set graphics mode*/
  48.         initgraph(&grdrv, &grmode, "");
  49.         getaspectratio(&xr,&yr);
  50.         xf=xr;
  51.         xf=xf/10000;
  52.  
  53.         for(n1=0;n1<6;n1++) {
  54.                 for (n2=0;n2<6;n2++) {
  55.                         for (n3=0;n3<6;n3++) {
  56.                                 nn=n3+6*n2+36*n1;
  57.                                 cor[nn].x=10*n1-25;
  58.                                 cor[nn].y=10*n2-25;
  59.                                 cor[nn].z=10*n3-25;
  60.                                 co[nn]=nn+16;
  61.                                 r[nn]=nn;
  62.                                 setrgbpalette(nn+16,10*n1+4,10*n2+4,10*n3+4);
  63.                                 }
  64.                         }
  65.                 }
  66.         nn=216;
  67.  
  68.         /* main turning routine */
  69.         xm=0; ym=0; zm=0;
  70.                 do {
  71.                 if (kbhit()) {
  72.                         switch (toupper(getch())){
  73.                                 case 'D':xm-=1;break;
  74.                                 case 'E':xm+=1;break;
  75.                                 case 'A':ym-=1;break;
  76.                                 case 'S':ym+=1;break;
  77.                                 case 'W':zm-=1;break;
  78.                                 case 'Q':zm+=1;break;
  79.                                 case 27:closegraph();exit(0);
  80.                                 }
  81.                         }
  82.                 for(ap=0; ap<nn; ap++)
  83.                         {
  84.                         move(xm,&cor[ap].y,&cor[ap].z);
  85.                         move(ym,&cor[ap].z,&cor[ap].x);
  86.                         move(zm,&cor[ap].y,&cor[ap].x);
  87.                         }
  88.                 for(ap=0; ap<nn; ap++)
  89.                         {
  90.                         for(bp=ap+1; bp<nn; bp++) { if(cor[r[bp]].z < cor[r[ap]].z) swap(&r[ap],&r[bp]); }
  91.                         }
  92.                 cleardevice();
  93.                 for(ap=0; ap<nn; ap++)
  94.                         {
  95.                         bp=r[ap];
  96.                         setcolor(co[bp]);
  97.                         setfillstyle(1,co[bp]);
  98.                         xp=cor[bp].x;
  99.                         yp=cor[bp].y;
  100.                         xp=xp+(xp*cor[bp].z/200);
  101.                         yp=yp+(yp*cor[bp].z/200);
  102.                         xp=xp+160;
  103.                         yp=yp*xf+100;
  104.                         fillellipse(xp,yp,3,3);
  105.                         }
  106.                 } while (1==1);
  107. }
  108.  
  109. /*routine that changes the x, y & z coordinates*/
  110. void move(float m, float *b, float *c)
  111. {
  112.         float d=hypot(*b,*c);
  113.         float l=0;
  114.         if (*b !=0) l=atan(*c/ *b);
  115.         if (*b<0) l+=3.14159265;
  116.         l+=6.283185+m/100;
  117.         if(l>6.283185) l-=6.283185;
  118.         *c=sin(l)* d;
  119.         *b=cos(l)* d;
  120. }
  121.  
  122. void swap(int *m1,int *m2)
  123. {
  124.         int tmp;
  125.         tmp=*m1;
  126.         *m1=*m2;
  127.         *m2=tmp;
  128. }