⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 3dgraphics7.c

📁 计算机图形学
💻 C
📖 第 1 页 / 共 5 页
字号:
#include"stdio.h";
#include"graphics.h"
#include"math.h"
#include"dos.h"
#include"time.h"

/********************/
/*                  */
/*     二维图形     */
/*                  */
/********************/
typedef struct Point      /*  点 */
{
   int x;
   int y;
}Point;

typedef struct Line       /*  线 */
{
    Point  A;/*起点*/
    Point  B;/*终点*/
    int color; /*颜色*/
}Line;



typedef struct Yuan       /*  圆 */
{
   Point  o; /*圆心*/
   int r;    /*半径*/
   int color;/*颜色*/
}Yuan;

typedef struct TuoYuan    /*椭 圆*/
{
   Point  o;/*中心*/
   int a;   /*长轴*/
   int b;   /*短轴*/
   int color;/*颜色*/
}TuoYuan;

typedef struct Qiu        /* 球 */
{
   Point  o;/*中心*/
   int r;
   int color; /*颜色*/
}Qiu;

typedef struct Rect       /* 矩形*/
{
  Point LeftTop;
  Point RightDown;
  int color;
}Rect;


/********************************/
/****     二维基本变换     ******/
/********************************/

/* 点的平移变换 */
void PinYi(int * x,int * y,int tx,int ty)
{
  *x = *x + tx;
  *y = *y + ty;

}

/* 点的旋转变换 */

void XuanZhuan(int * x,int * y,float q)
{
  float m ;
  float n;
  float Q;
  Q = (3.1415926/180)*q;
  m = (*x);
  n = (*y);
  *x = m * cos(Q) - n * sin(Q);
  *y = m * sin(Q) + n * cos(Q);
}

/* 绕任意点的旋转变换 */

void XuanZhuan_RY(int m,int n,int * x,int * y,float q)/* 绕(m,n)的旋转*/
{

  PinYi(x,y,-m,-n);
    XuanZhuan(x,y,q);
  PinYi(x,y,m,n);
}

/* 点的放缩变换 */
void FangSuo(int * x,int * y,float sx,float sy)
{
   *x =*x * sx;
   *y =*y * sy;
}

/* 以任意点的放缩变换 */
void FangSuo_RY(int m,int n,int * x,int * y,float sx,float sy) /*以(m,n)为参考点*/
{
   PinYi(x,y,-m,-n);
     FangSuo(x,y,sx,sy);
   PinYi(x,y,m,n);
}



/* 点的错切变换 */
void CuoQie(int * x,int * y,float q,int flag)
{
  float Q;
  Q = (3.1415926/180) * q;
  if(flag==0)/* y为依赖轴 */
  {
    *x = (float)*x + tan(Q)*((float)(*y));
    *y = *y;
  }
  else
  {
    *y = (float)*y + tan(Q)*((float)(*x));
    *x = *x;
  }

}

/* 以任意点的错切变换 */
void CuoQie_RY(int m,int n,int * x,int * y,float q,int flag) /* 以为(m,n)参考点*/
{

    PinYi(x,y,-m,-n);
    CuoQie(x,y,q,flag);
    PinYi(x,y,m,n);

}
/***********************/
/*                     */
/*      三维图形       */
/*                     */
/***********************/
typedef struct Point3D      /*  点 */
{
   int x;
   int y;
   int z;
}Point3D;

typedef struct Rect3D      /*  矩形面 */
{
   Point3D A;
   int a;
   int b;
   int color;
}Rect3D;


typedef struct CFT        /*长方体*/
{
  Point3D A;/*正方体的中心*/
  int a;    /*    长    */
  int b;    /*    宽    */
  int h;    /*    高    */
}CFT;

typedef struct SLZ        /*四棱锥*/
{
  Point3D A;/*四棱锥底面的中心*/
  int r;    /*    底面边长    */
  int h;    /*    高     */
}SLZ;

typedef struct SLZU        /*三棱柱*/
{
  Point3D A;/*三棱锥底面的中心*/
  int r;    /*    底面边长    */
  int h;    /*    高     */
}SLZU;

typedef struct depth       /*三维实体深度信息*/
{
  char s;  /*实体名称*/
  int  d;   /*实体深度*/
}Depth;


/********************************/
/****                      ******/
/****     三维基本变换     ******/
/****                      ******/
/********************************/


/* 点的平移变换 */
void PinYi3D(int * x,int * y,int * z,int tx,int ty,int tz)
{
  *x = *x + tx;
  *y = *y + ty;
  *z = *z + tz;

}



/* 点旋转变换 */

void XuanZhuan3D(int * x,int * y,int *z,float q,int flag)
{
  int m,n,t;
  float Q;
  m = (*x);
  n = (*y);
  t = (*z);
  Q = (3.1415926/180)*q;
  if(flag==1)   /* 绕x轴旋转*/
  {
    *x = *x;
    *y = (float)n * cos(Q) - (float)t * sin(Q);
    *z = (float)n * sin(Q) + (float)t * cos(Q);
   }
   if(flag==2)  /* 绕y轴旋转*/
   {
     *x = (float)m * cos(Q) + (float)t * sin(Q);
     *y = *y;
     *z = (float)(-m) * sin(Q) + (float)t * cos(Q);
   }
   if(flag==3)  /* 绕z轴旋转*/
   {
      *x = (float)m * cos(Q) - (float)n * sin(Q);
      *y = (float)m * sin(Q) + (float)n * cos(Q);
      *z = *z;
   }

}

/* 绕任意轴的点的旋转变换 */
void XuanZhuan3D_RY(int * x,int * y,int *z,float q,int i,int j,int k,int flag)
{

  if(flag==1)   /* 绕平行于X轴且过点(i,j,k)的直线的旋转*/
  {
    PinYi3D(x,y,z,-i,-j,-k);
       XuanZhuan3D(x,y,z,q,flag);
    PinYi3D(x,y,z,i,j,k);
   }
   if(flag==2)  /* 绕平行于Y轴且过点(i,j,k)的直线的旋转*/
   {
     PinYi3D(x,y,z,-i,-j,-k);
       XuanZhuan3D(x,y,z,q,flag);
     PinYi3D(x,y,z,i,j,k);
   }
   if(flag==3)  /* 绕平行于Z轴且过点(i,j,k)的直线的旋转*/
   {
     PinYi3D(x,y,z,-i,-j,-k);
       XuanZhuan3D(x,y,z,q,flag);
     PinYi3D(x,y,z,i,j,k);
   }

}

/* 点放缩变换 */
void FangSuo3D(int * x,int * y,int * z,float sx,float sy,float sz)
{
   *x =*x * sx;
   *y =*y * sy;
   *z =*z * sz;
}

/* 以任意点的放缩变换 */
void FangSuo3D_RY(int m,int n,int t,int * x,int * y,int *z,float sx,float sy,float sz) /*以(m,n,t)为参考点*/
{
   PinYi3D(x,y,z,-m,-n,-t);
     FangSuo3D(x,y,z,sx,sy,sz);
   PinYi3D(x,y,z,m,n,t);
}


/************************/
/*                      */
/* 三维坐标系的平移变换 */
/*                      */
/************************/

void PingYi3D_ZBX(int * x,int * y,int *z,int tx,int ty,int tz)
{
   *x = *x - tx;
   *y = *y - ty;
   *z = *z - tz;
}


/************************/
/*                      */
/* 三维坐标系的旋转变换 */
/*                      */
/************************/

void XuanZhuan3D_ZBX(int * x,int * y,int *z,float q,int flag)
{
  int m,n,t;
  float Q;
  m = (*x);
  n = (*y);
  t = (*z);
  Q = (3.1415926/180 )*q;
  if(flag==1)   /* x,y轴的旋转*/
  {
    *x = (float)m * cos(Q) + (float)n * sin(Q);
    *y = (float)(-m) * sin(Q) + (float)n * cos(Q);
    *z = *z;
   }
   if(flag==2)  /* y,z的轴旋转*/
   {
     *x = *x;
     *y = (float)n * cos(Q) + (float)t * sin(Q);
     *z = (float)(-n) * sin(Q) + (float)t * cos(Q);
   }
   if(flag==3)  /* z,x的轴旋转*/
   {
      *x = (float)m * cos(Q) - (float)t * sin(Q);
      *y = *y;
      *z = (float)m * sin(Q) + (float)t * cos(Q);
   }
}


/********************************/
/*                              */
/* 三维空间点到二维平面点的投影 */
/*                              */
/********************************/
void Point3D_2D(int * x,int * y,int * z,Point3D B,float qx,float qy,float qz)/*投影方向为(qx,qy,qz)*/
{
      int t,i,j,k;
      i = B.x; j = B.y; k = B.z;

/* */FangSuo3D_RY(i,j,k,x,y,z,0.6,0.6,0.6);

      PingYi3D_ZBX(x,y,z,i,j,k);
       XuanZhuan3D_ZBX(x,y,z,qx,1);
       XuanZhuan3D_ZBX(x,y,z,qz,3);
     PingYi3D_ZBX(x,y,z,-i,-j,-k);

   /*   *x = 5000.0*(*x)/(5000.0 - *z);
      *y = 5000.0*(*y)/(5000.0 - *z);  */

      t = *x; *x = *y; *y = t;


}







/***************************/
/**                       **/
/**     3D空间矩形面      **/
/**    平行于X-Y平面      **/
/***************************/
void Rect_3DXY(Point3D A,int a,int b,int color,Point3D B,float qx,float qy,float qz)
{
   int i,j,k,X,Y,Z;
   Point3D B1,B2,B3,B4;

   B1.x = A.x + b/2.0; B1.y = A.y - a/2.0; B1.z = A.z;
   B2.x = A.x + b/2.0; B2.y = A.y + a/2.0; B2.z = A.z;
   B3.x = A.x - b/2.0; B3.y = A.y + a/2.0; B3.z = A.z;
   B4.x = A.x - b/2.0; B4.y = A.y - a/2.0; B4.z = A.z;

   X = B1.x; Y = B1.y; Z = B1.z;
   i = X; j = Y;
   while(j <= B2.y)
   {
      while(i >= B4.x)
      {
         Point3D_2D(&X,&Y,&Z,B,qx,qy,qz);
          putpixel(X,Y,color);

         i--;
         X = i; Y = j; Z = B1.z;
      }
      j++;
      X = B1.x; Y = j; Z = B1.z; i = X;
   }

}

/***************************/
/**                       **/
/**     3D空间矩形面      **/
/**     平行于X-Z平面     **/
/***************************/
void Rect_3DXZ(Point3D A,int a,int b,int color,Point3D B,float qx,float qy,float qz)
{
   int i,j,k,X,Y,Z;
   Point3D B1,B2,B3,B4;

   B1.x = A.x + a/2.0; B1.y = A.y; B1.z = A.z - b/2.0;
   B2.x = A.x - a/2.0; B2.y = A.y; B2.z = A.z - b/2.0;
   B3.x = A.x - a/2.0; B3.y = A.y; B3.z = A.z + b/2.0;
   B4.x = A.x + a/2.0; B4.y = A.y; B4.z = A.z + b/2.0;

   X = B1.x; Y = B1.y; Z = B1.z;
   i = X; k = Z;
   while(i >= B2.x)
   {
      while(k <= B4.z)
      {
         Point3D_2D(&X,&Y,&Z,B,qx,qy,qz);
          putpixel(X,Y,color);

         k++;
         X = i; Y =A.y; Z = k;
      }
      i--;
      X = i; Y = A.y; Z = B1.z; k = Z;
   }

}

/***************************/
/**                       **/
/**     3D空间矩形面      **/
/**     平行于Y-Z平面     **/
/***************************/
void Rect_3DYZ(Point3D A,int a,int b,int color,Point3D B,float qx,float qy,float qz)
{
   int i,j,k,X,Y,Z;
   Point3D B1,B2,B3,B4;

   B1.x = A.x; B1.y = A.y - a/2.0; B1.z = A.z - b/2.0;
   B2.x = A.x; B2.y = A.y + a/2.0; B2.z = A.z - b/2.0;
   B3.x = A.x; B3.y = A.y + a/2.0; B3.z = A.z + b/2.0;
   B4.x = A.x; B4.y = A.y - a/2.0; B4.z = A.z + b/2.0;

   X = B1.x; Y = B1.y; Z = B1.z;
   j = Y; k = Z;
   while(j <= B2.y)
   {
      while(k <= B4.z)
      {
         Point3D_2D(&X,&Y,&Z,B,qx,qy,qz);
          putpixel(X,Y,color);

         k++;
         X = A.x; Y =j; Z = k;
      }
      j++;
      X = A.x; Y = j; Z = B1.z; k = Z;
   }

}

/***************************/
/**                       **/
/**  3D空间矩形面的旋转   **/
/**    平行于X-Y平面      **/
/***************************/
void Rect_3DXY_XuanZhuan(Point3D A,int a,int b,int color,int q,Point3D D,int flag,Point3D B,float qx,float qy,float qz)
{
   int i,j,k,X,Y,Z;
   int ii,jj,kk;
   Point3D B1,B2,B3,B4;

   ii = D.x; jj = D.y; kk = D.z;

   B1.x = A.x + b/2.0; B1.y = A.y - a/2.0; B1.z = A.z;
   B2.x = A.x + b/2.0; B2.y = A.y + a/2.0; B2.z = A.z;
   B3.x = A.x - b/2.0; B3.y = A.y + a/2.0; B3.z = A.z;
   B4.x = A.x - b/2.0; B4.y = A.y - a/2.0; B4.z = A.z;

   X = B1.x; Y = B1.y; Z = B1.z;
   i = X; j = Y;
   while(j <= B2.y)
   {
      while(i >= B4.x)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -