home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / FAQSYS18.ZIP / FAQS.DAT / QUATERN.CPP < prev    next >
C/C++ Source or Header  |  1996-08-18  |  6KB  |  243 lines

  1. #include <math.h>
  2. #include <graphics.h>
  3. #include <conio.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <iostream.h>
  7.  
  8. #include <fstream.h>
  9.  
  10.  
  11.     int zant=450;   //z-resolution. bigger zant -> better resolution
  12.     int zant1=25;   //z-resolution. bigger zant -> better resolution
  13.     int pixsize=2, vissize=1;
  14.  
  15.     double xmin=-1.66, xmax=1;
  16.     double ymin=-1, ymax=1;
  17.     double zmin=-1.7, zmax=1.7;
  18.     int iter=6;
  19.  
  20.     double lightx=-1, lighty=1, lightz=-3;
  21.  
  22.     double vx=0, vy=0, vz=0;
  23.  
  24.     double cr=0.50;  //constant real value
  25.     double ci=0.40;  //constant imaginary(1) value
  26.     double cj=1;  //constant imaginary(2) value
  27.     double ck=0.05;   //constant imaginary(3) value
  28.     double wk=-0.55;   //4th dimension
  29.  
  30.   int background = 0;
  31.  
  32.     int maxcolor = 16;
  33.  
  34.     int sx,sy;
  35.     double dx,dy,dz;
  36.     double origx, origy, origz;
  37.     double rminx, rminy, rminz;
  38.     double dxx, dxy, dxz;
  39.     double dyx, dyy, dyz;
  40.     double dzx, dzy, dzz;
  41.     double dzx1, dzy1, dzz1;
  42.     double tempx, tempy, tempz;
  43.     double cosx,cosy,cosz,sinx,siny,sinz;
  44.     double z_buffer[640][10];
  45.     int buffer_y;
  46.  
  47. void rotate3D(double &x,double &y,double &z)
  48. {
  49.     x-=origx;y-=origy;z-=origz;
  50.     double xy=y*cosx-z*sinx;
  51.     double xz=y*sinx+z*cosx;
  52.     double xx=x;
  53.     x=xx;
  54.     y=xy;
  55.     z=xz;
  56.     double yx=x*cosy+z*siny;
  57.     double yz=-x*siny+z*cosy;
  58.     x=yx;
  59.     z=yz;
  60.     double zx=x*cosz-y*sinz;
  61.     double zy=x*sinz+y*cosz;
  62.     x=zx;
  63.     y=zy;
  64.     x+=origx;y+=origy;z+=origz;
  65. }
  66.  
  67. void rotatevalues()
  68. {
  69.     rminx=xmin;rminy=ymin;rminz=zmin;
  70.     rotate3D(rminx, rminy, rminz);
  71.     tempx=xmax;tempy=ymin;tempz=zmin;
  72.     rotate3D(tempx, tempy, tempz);
  73.     dxx=(tempx-rminx)/sx;dxy=(tempy-rminy)/sx;dxz=(tempz-rminz)/sx;
  74.     tempx=xmin;tempy=ymax;tempz=zmin;
  75.     rotate3D(tempx, tempy, tempz);
  76.     dyx=(tempx-rminx)/sy;dyy=(tempy-rminy)/sy;dyz=(tempz-rminz)/sy;
  77.     tempx=xmin;tempy=ymin;tempz=zmax;
  78.     rotate3D(tempx, tempy, tempz);
  79.     dzx=(tempx-rminx)/zant;dzy=(tempy-rminy)/zant;dzz=(tempz-rminz)/zant;
  80.     dzx1=dzx/zant1;dzy1=dzy/zant1;dzz1=dzz/zant1;
  81. }
  82.  
  83. double calc_l(double x, double y, double z)
  84. {     //  (x,y,z,w)^2 =
  85.         //                    ( x*x - y*y - z*z - w*w ,
  86.         //                      x*y + y*x + z*w - w*z ,
  87.         //                      x*z + z*x - y*w + w*y ,
  88.         //                      x*w + w*x + y*z - z*y ) }
  89.     double lengde;
  90.     double temp;
  91.     double w=wk;
  92.     int m=0;
  93.     do {
  94.     temp=x+x;
  95.     x=x*x-y*y-z*z-w*w+cr;
  96.     y=temp*y+ci;
  97.     z=temp*z+cj;
  98.     w=temp*w+ck;
  99.  
  100.     m++;
  101.     lengde=x*x+y*y+z*z+w*w;
  102.     } while ((m<iter) && (lengde<2));
  103.  return lengde;
  104. }
  105.  
  106. double calc_angle(double w,double e,double n,double s,double cx,double cy,double cz)
  107. {
  108.     double lightdx=cx-lightx;
  109.     double lightdy=cy-lighty;
  110.     double lightdz=cz-lightz;
  111.  
  112.     double lightlength=sqrt(lightdx*lightdx+lightdy*lightdy+lightdz*lightdz);
  113.  
  114.     double fx=/*(0)*(s-n)*/-(e-w)*(dy+dy);
  115.     double fy=/*(e-w)*(0)*/-(dx+dx)*(s-n);
  116.     double fz=(dx+dx)*(dy+dy)/*-(0)*(0)*/;
  117.  
  118.     double flength=sqrt(fx*fx+fy*fy+fz*fz);
  119.     double c=(fx*lightdx+fy*lightdy+fz*lightdz)/(flength*lightlength);
  120.     return c;
  121. }
  122.  
  123. void show_buffer(int y)
  124. {
  125.     double a;
  126.  
  127.     for (int t=1; t<sx; t++) {
  128.      for (int i=1; i<=8; i++) {
  129.         if ((y+i)<sy) {
  130.             a=calc_angle(z_buffer[t-1][i],z_buffer[t+1][i],z_buffer[t][i-1],z_buffer[t][i+1]
  131.                          ,t*dx+xmin,(y+i)*dy+ymin,z_buffer[t][i]);
  132.             if ((z_buffer[t][i]>zmax) && (background==0)) {
  133.              setfillstyle(1,0);
  134.             } else if (a<0) {
  135.              setfillstyle(1,1);
  136.             } else {
  137.              setfillstyle(1,1+(maxcolor-1)*a);
  138.             }
  139.             bar(t*vissize,(y+i)*vissize,t*vissize+vissize-1,(y+i)*vissize+vissize-1);
  140.         }
  141.      }
  142.     }
  143.  
  144.  
  145.     for (t=0; t<640; t++) {
  146.      z_buffer[t][0]=z_buffer[t][8];
  147.      z_buffer[t][1]=z_buffer[t][9];
  148.     }
  149.     buffer_y=2;
  150. }
  151.  
  152. void main()
  153. {
  154.     int pz, pz1;
  155.     double l;
  156.  
  157.     int gdriver = VGA, gmode = VGAHI, errorcode;
  158.     errorcode = registerbgidriver(EGAVGA_driver);
  159.     if (errorcode < 0) {
  160.         printf("Graphics error: %s\n", grapherrormsg(errorcode));
  161.         exit(1);
  162.     }
  163.  
  164.     initgraph(&gdriver, &gmode, "");
  165.  
  166.     errorcode = graphresult();
  167.     if (errorcode != grOk) {
  168.         printf("Graphics error: %s\n", grapherrormsg(errorcode));
  169.         exit(1);
  170.     }
  171.  
  172.     for (int i=1; i<16; i++) {
  173.      setrgbpalette(i, 0, i*4, 0);
  174.      setpalette(i, i);
  175.     }
  176.     setrgbpalette(0,0,0,63);
  177.     setpalette(0, 0);
  178.  
  179.  
  180.     sx=getmaxx()/pixsize;sy=getmaxy()/pixsize;
  181.     dx=(xmax-xmin)/sx;
  182.     dy=(ymax-ymin)/sy;
  183.     dz=(zmax-zmin)/zant;
  184.     double dz1=dz/zant1;
  185.  
  186.     origx=(xmin+xmax)/2;
  187.     origy=(ymin+ymax)/2;
  188.     origz=(zmin+zmax)/2;
  189.  
  190.  
  191.     int ve=0;
  192. //  for (ve=0; ve<50; ve++) {  //only used when making animations
  193. //     vx=0;vy=0;vz=0;
  194.     vx=vx/180*3.14159265;
  195.     vy=vy/180*3.14159265;
  196.     vz=vz/180*3.14159265;
  197.  
  198.     cosx=cos(vx);cosy=cos(vy);cosz=cos(vz);
  199.     sinx=sin(vx);siny=sin(vy);sinz=sin(vz);
  200.  
  201.     rotatevalues();
  202.     buffer_y=0;
  203.     for (int py=0; py<=sy; py++) {
  204.      for (int px=0; px<=sx; px++) {
  205.         tempx=rminx+px*dxx+py*dyx/*+pz*dzx*/;
  206.         tempy=rminy+px*dxy+py*dyy/*+pz*dzy*/;
  207.         tempz=rminz+px*dxz+py*dyz/*+pz*dzz*/;
  208.         pz=0;
  209.         do {
  210.             tempx+=dzx;
  211.             tempy+=dzy;
  212.             tempz+=dzz;
  213.             l=calc_l(tempx,tempy,tempz);
  214.             pz++;
  215.         } while ((l>2) && (pz<zant));
  216.         pz1=0;
  217.         do {
  218.             pz1++;
  219.             tempx-=dzx1;
  220.             tempy-=dzy1;
  221.             tempz-=dzz1;
  222.             l=calc_l(tempx,tempy,tempz);
  223.         } while (l<2);
  224.         if (pz < zant)
  225.             z_buffer[px][buffer_y]=zmin+pz*dz-pz1*dz1;
  226.         else
  227.             z_buffer[px][buffer_y]=zmax+dz;
  228.         setfillstyle(1,15-pz/10);
  229.         bar(px*vissize,py*vissize,px*vissize+vissize-1,py*vissize+vissize-1);
  230.         if (kbhit()) break;
  231.      }
  232.      buffer_y++;
  233.      if (buffer_y==10) show_buffer(py-9);
  234.      if (kbhit()) break;
  235.     }
  236.     if (!kbhit()) {
  237.      show_buffer(py-buffer_y);
  238.      cout << '\7';
  239.     }
  240.     char answer = getch();
  241. //  }  //end of FOR-loop. Only used when making animations
  242.     closegraph();
  243. }