home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c129 / 1.ddi / PLOT3D.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-11-17  |  6.6 KB  |  390 lines

  1. #include <stdlib.h>
  2. #include <malloc.h>
  3. #include "worlddr.h"
  4. #include <graph.h>
  5. #include <math.h>
  6. #include <stdio.h>
  7. #include <io.h>
  8. float tCurnt[9];
  9. float t3Curnt[16];
  10.  
  11. struct point3D{
  12.     float x,y,z;
  13. };
  14. struct point3D Poly3DType[100];
  15.  
  16.  
  17. void IdArg(float a[])
  18. {
  19.    int i;
  20.    int j;
  21.  
  22.    for ( i = 0; i <= 2; ++i ) {
  23.       for ( j = 0; j <= 2; ++j ) {
  24.          a[i*3+j] = 0.0;
  25.        }
  26.       a[i*3+i] = 1.0;
  27.    }
  28. }
  29.  
  30. void Concat(float a[],float b[],float c[])
  31. {
  32.    int i;
  33.    int j;
  34.    float temp[9];
  35.  
  36.    for ( i = 0; i <= 2; ++i ) {
  37.       for ( j = 0; j <= 2; ++j ) {
  38.          temp[i*3+j] = a[i*3+0] * b[j] +
  39.                     a[i*3+1] * b[1*3+j] +
  40.                     a[i*3+2] * b[2*3+j];
  41.       }
  42.    }
  43.   for (i=0; i <=8; ++i){
  44.     c[i] = temp[i];
  45.   }
  46. }
  47.  
  48.  
  49. void Ident(float imatrix[])
  50. {
  51.    IdArg(imatrix);
  52. }
  53.  
  54. void tInit()
  55. {
  56.    Ident(tCurnt);
  57. }
  58.  
  59.  
  60. void Xfrm2P(float x,float y,float *xt,float *yt)
  61. {
  62.  
  63.    (*xt) = x * tCurnt[0] + y * tCurnt[3] +
  64.             tCurnt[6];
  65.    (*yt) = x * tCurnt[1] + y * tCurnt[4] +
  66.             tCurnt[7];
  67.     *xt = *xt/tCurnt[8];
  68.     *yt = *yt/tCurnt[8];
  69. }
  70.  
  71. void WorldTran2(float x,float y)
  72. {
  73.    float tmpMat[9];
  74.    IdArg(tmpMat);
  75.    tmpMat[6] = x;
  76.    tmpMat[7] = y;
  77.    Concat(tCurnt,tmpMat,tCurnt);
  78. }
  79.  
  80. void WorldRotate2(float degree)
  81. {
  82.    float tmpMat[9];
  83.    double radian;
  84.  
  85.    IdArg(tmpMat);
  86.    radian =  degree * 3.1415 / 180.0;
  87.    tmpMat[0] = cos(radian);
  88.    tmpMat[1] = sin(radian);
  89.    tmpMat[3] = -sin(radian);
  90.    tmpMat[4] = cos(radian);
  91.    Concat(tCurnt,tmpMat,tCurnt);
  92. }
  93.  
  94. void WorldScale2(float x,float y)
  95. {
  96.    float tmpMat[9];
  97.  
  98.    IdArg(tmpMat);
  99.    tmpMat[0] = x;
  100.    tmpMat[4] = y;
  101.    Concat(tCurnt,tmpMat,tCurnt);
  102. }
  103.  
  104. void Line2Abs(float x,float y)
  105. {
  106.    float xt;
  107.    float yt;
  108.    Xfrm2P(x,y,&xt,&yt);
  109.    LineWorldAbs(xt,yt);
  110. }
  111.  
  112. void Move2Abs(float x,float y)
  113. {
  114.    float xt;
  115.    float yt;
  116.  
  117.    Xfrm2P(x,y,&xt,&yt);
  118.    MoveWorldAbs(xt,yt);
  119. }
  120.  
  121. void Line2Rel(float x,float y)
  122. {
  123.    float xt;
  124.    float yt;
  125.  
  126.    Xfrm2P(x,y,&xt,&yt);
  127.    LineWorldRel(xt,yt);
  128. }
  129.  
  130. void Move2Rel(float x,float y)
  131. {
  132.    float xt;
  133.    float yt;
  134.  
  135.    Xfrm2P(x,y,&xt,&yt);
  136.    MoveWorldRel(xt,yt);
  137. }
  138.  
  139.  
  140. void IdArg3(float a[])
  141. {  int i;
  142.    int j;
  143.  
  144.    for  (i = 0; i <= 3; ++i)
  145.    {
  146.       for  (j = 0; j <= 3; ++j )
  147.          a[i*4+j] = 0.0;
  148.       a[i*4+i] = 1.0;
  149.    }
  150. }
  151.  
  152. void Concat3( float a[],
  153.               float b[],
  154.               float c[] )
  155. {  int i;
  156.    int j;
  157.    float temp[16];
  158.  
  159.   for  (i = 0; i <= 3; ++i)
  160.   {
  161.     for (j = 0; j <= 3; ++j )
  162.       temp[i*4+j] = a[i*4+0] * b[j] +
  163.                   a[i*4+1] * b[1*4+j] +
  164.                   a[i*4+2] * b[2*4+j] +
  165.                   a[i*4+3] * b[3*4+j];
  166.   }
  167.   for  (i = 0; i <= 15; ++i)
  168.     c[i] = temp[i];
  169.  
  170. }
  171.  
  172. void Ident3(float imatrix[])
  173. {
  174.    IdArg3(imatrix);
  175. }
  176.  
  177.  
  178. void tInit3()
  179. {
  180.    Ident3(t3Curnt);
  181. }
  182.  
  183.  
  184. void Xfrm3P(float x,
  185.             float y,
  186.             float z,
  187.             float w,
  188.             float *xt,
  189.             float *yt,
  190.             float *zt,
  191.             float *wt)
  192. {
  193.    *xt = x * t3Curnt[0] + y * t3Curnt[4] +
  194.          z * t3Curnt[8] + w * t3Curnt[12];
  195.  
  196.    *yt = x * t3Curnt[1] + y * t3Curnt[5] +
  197.          z * t3Curnt[9] + w * t3Curnt[13];
  198.  
  199.    *zt = x * t3Curnt[2] + y * t3Curnt[6] +
  200.          z * t3Curnt[10] + w * t3Curnt[14];
  201.  
  202.    *wt = x * t3Curnt[3] + y * t3Curnt[7] +
  203.          z * t3Curnt[11] + w * t3Curnt[15];
  204. }
  205.  
  206. void WorldTran3( float x,
  207.                  float y,
  208.                  float z)
  209. {
  210.    float tmp3Mat[16];
  211.  
  212.    IdArg3(tmp3Mat);
  213.    tmp3Mat[12] = x;
  214.    tmp3Mat[13] = y;
  215.    tmp3Mat[14] = z;
  216.    Concat3(t3Curnt,tmp3Mat,t3Curnt);
  217. }
  218.  
  219.  
  220. void WorldRotate3( float degree,
  221.                    int iAxis)
  222. {
  223.     float tmp3Mat[16];
  224.     float sign;
  225.     float radian;
  226.     int i1;
  227.     int i2;
  228.     int  i;
  229.  
  230.      switch (iAxis){
  231.      case 0:  {  i1 = 1; i2 = 2; sign =  1.0; }
  232.                break;
  233.      case 1:  {  i1 = 0; i2 = 2; sign = -1.0; }
  234.                break;
  235.      case 2:  {  i1 = 0; i2 = 1; sign =  1.0; }
  236.                break;
  237.      default:
  238.          {  i1 = 1; i2= 2;  };
  239.                break;
  240.    }
  241.    IdArg3(tmp3Mat);
  242.    radian = degree * 3.1415 / 180.0;
  243.  
  244.    tmp3Mat[i1*4+i1] = cos(radian);
  245.    tmp3Mat[i2*4+i2] = tmp3Mat[i1*4+i1];
  246.    tmp3Mat[i1*4+i2] = sign*sin(radian);
  247.    tmp3Mat[i2*4+i1] = -tmp3Mat[i1*4+i2];
  248.    Concat3(t3Curnt,tmp3Mat,t3Curnt);
  249.  
  250. }
  251.  
  252.  
  253. void WorldScale3( float x,
  254.                   float y,
  255.                   float z)
  256. {
  257.    float tmp3Mat[16];
  258.  
  259.    IdArg3(tmp3Mat);
  260.    tmp3Mat[0] = x;
  261.    tmp3Mat[5] = y;
  262.    tmp3Mat[10] = z;
  263.    Concat3(t3Curnt,tmp3Mat,t3Curnt);
  264. }
  265.  
  266. void Persp( float dist  )
  267.  
  268. {
  269.   float tmp3Mat[16];
  270.  
  271.   IdArg3( tmp3Mat );
  272.   tmp3Mat[0] = -dist;
  273.   tmp3Mat[5] = -dist;
  274.   tmp3Mat[11] = 1.0;
  275.   tmp3Mat[15] = -dist;
  276.   Concat3(t3Curnt,tmp3Mat, t3Curnt );
  277. }
  278.  
  279. void Line3Abs( float x,
  280.                float y,
  281.                float z)
  282.  
  283. {
  284.    float xt;
  285.    float yt;
  286.    float zt;
  287.    float wt;
  288.    Xfrm3P(x,y,z, 1.0, &xt,&yt,&zt,&wt);
  289.    xt = xt/wt;
  290.    yt= yt/wt;
  291.    LineWorldAbs(xt,yt);
  292.  
  293. }
  294.  
  295. void Move3Abs( float x,
  296.                float y,
  297.                float z)
  298.  
  299. {
  300.    float xt;
  301.    float yt;
  302.    float zt;
  303.    float wt;
  304.  
  305.    Xfrm3P(x,y,z, 1.0, &xt,&yt,&zt,&wt);
  306.    xt = xt/wt;
  307.    yt= yt/wt;
  308.    MoveWorldAbs(xt,yt);
  309.  
  310. }
  311.  
  312. void Line3Rel( float x,
  313.                float y,
  314.                float z)
  315.  
  316. {
  317.    float xt;
  318.    float yt;
  319.    float zt;
  320.    float wt;
  321.    Xfrm3P(x,y,z, 1.0, &xt,&yt,&zt,&wt);
  322.    xt = xt/wt;
  323.    yt= yt/wt;
  324.    LineWorldRel(xt,yt);
  325. }
  326.  
  327. void Move3Rel(float x,
  328.                float y,
  329.                float z)
  330.  
  331. {
  332.    float xt;
  333.    float yt;
  334.    float zt;
  335.    float wt;
  336.    Xfrm3P(x,y,z, 1.0, &xt,&yt,&zt,&wt);
  337.    xt = xt/wt;
  338.    yt = yt/wt;
  339.    MoveWorldRel(xt,yt);
  340. }
  341.  
  342. void Draw3DAxes( float x,
  343.                  float y,
  344.                  float z)
  345.  
  346.  
  347. {
  348.   Move3Abs( 0.0,0.0,0.0 );
  349.   Line3Abs( x,0.0,0.0);
  350.   Move3Abs(0.0,0.0,0.0 );
  351.   Line3Abs( 0.0 ,y,0.0 );
  352.   Move3Abs( 0.0,0.0,0.0);
  353.   Line3Abs( 0.0,0.0,z);
  354.  
  355. }
  356.  
  357. void PolyFill3D( struct point3D *p, int fillstyle, int fillcolor,int numdat){
  358.  int i;
  359.  float xt,yt,zt,wt;
  360.  float *x,*y;
  361.  
  362.   x = (float *) calloc(numdat,4);
  363.   y = (float *) calloc(numdat,4);
  364.   for (i=0; i <= numdat-1; i++){
  365.  
  366.         Xfrm3P(p[i].x,p[i].y,p[i].z,1.0,&xt,&yt,&zt,&wt);
  367.         x[i] = xt/wt;
  368.         y[i] = yt/wt;
  369.   }
  370.   PolyFillWorldAbs(x,y,fillstyle, fillcolor,numdat);
  371.   free(x); free(y);
  372. }
  373.  
  374. void Label3D( char *outstring ){
  375.   outtextXX(outstring);
  376. }
  377.  
  378. void Init3D(int crtmod)
  379. {
  380.   tInit();
  381.   tInit3();
  382.   OneTimeInit(crtmod);
  383. }
  384.  
  385. void Close3DGraphics(){
  386.   closegraphics();
  387. }
  388.  
  389.  
  390.