Board logo

标题: 一个c语言图形库和数学库 [打印本页]

作者: bmmm906     时间: 2006-1-24 11:53    标题: 一个c语言图形库和数学库

/*  by   边明明,西安邮电学院。*/
/*这个是俺自己写的一个c语言图形库和数学库,在tc2.0下测试通过。请大家放心使用O_o!

  所有函数是相对于调整的坐标原点进行画图的,不是以屏幕原点坐标,这一点请大家注意!!!

  特点是所有作图函数加入了颜色设置,
  特别加入了3D作图,3D作图时要初始化观察者的坐标和距离(InitPer())。
  还要初始化观察者相对与x轴旋转角,相对与xy平面的俯视角(InitPoltting())。
  因为所有的3D作图函数都是相对于这个初始值进行作图的。
  

  注意:在包含这个库时还要包含进stdio.h,dos.h,conio.h,math.h,graphics.h,math.inc这些库。
        这里也可以一个include "myhead.h"。这个文件在压缩包里。

  您可以在这个基础上继续建立自己的库。您也可以进行任意修改。O_o!

嘿嘿!俺做的很粗略,请大家批评指正!!!

  这里有几个例子:3Ddemo.c,3Ddemo2.c,3Ddemo3.c。




                                     bmmm906@sohu.com
                                     qq:53793208
                                     西安邮电学院南校区 电科0303班  75号信箱。
附件 1: 数学与图形库.rar (2006-1-24 11:53, 119.12 K, 下载附件所需积分 1点 ,下载次数: 726)

作者: cooray     时间: 2007-9-7 22:39
下载看看,谢谢^_^
作者: zian     时间: 2007-9-8 00:34
次次西.
作者: ysc     时间: 2007-9-8 09:10
顶,支持这种好东西~
作者: wangxy200696     时间: 2007-9-12 17:44
很好啊,不过好像有部分内容在我在一本讲3d书见过,不过它里面的是要svag的
作者: sohu129     时间: 2007-9-12 19:33
玩玩看。了解下C
作者: wmc6432107     时间: 2007-9-13 11:05
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
作者: zhuangjia     时间: 2008-1-28 22:40
终于找到了
作者: chnping     时间: 2008-1-29 17:43    标题: hjgh

jklhhhhhhhhuuiiiiiighuigig
作者: boyachang     时间: 2008-1-29 19:34
谢谢,正好需要。
作者: lostbear     时间: 2008-1-31 15:48
下看看,谢谢!`
作者: kittking     时间: 2008-4-21 20:48
谢谢楼主分享~~
作者: jefnik     时间: 2008-5-9 10:29
先收着,谢谢
作者: hengliangz     时间: 2008-9-4 16:19
好,楼主真强啊!
作者: opssss     时间: 2008-9-15 21:17

作者: vvtommy     时间: 2008-9-30 23:36
下载中~。。
作者: xwysj     时间: 2008-10-3 10:59
学习了!!!
作者: ywhm2004     时间: 2008-10-6 02:53
看起来不错喔
作者: bestmax     时间: 2008-10-12 11:07
不晓得咋用、、
告诉我咯
作者: 33360zhang     时间: 2008-10-12 15:28
123
作者: 594fqj     时间: 2008-10-13 19:41
好好孝敬额
作者: kc1235     时间: 2008-10-15 18:08

作者: mayanyxm     时间: 2008-11-21 16:58

作者: coinchen     时间: 2009-1-2 22:21
谢谢 作业用到正好
作者: cdb723g     时间: 2009-1-20 20:00
谢谢提供
作者: nbed     时间: 2009-2-10 17:49    标题: ding

ding xia
作者: andyond     时间: 2009-3-15 10:14
很崇拜搂住
作者: chali     时间: 2009-4-9 23:28
ding
作者: jiaking     时间: 2009-4-17 06:58
好久没回来看看了。看来高手们都还聚集啊
作者: Jamesman     时间: 2009-5-1 22:27
好东西
作者: violetlb     时间: 2009-6-5 05:54
回复就能下载?
作者: 169s     时间: 2009-6-23 23:09
这贴一定要顶
作者: hexianliang     时间: 2009-6-27 00:48
谢谢
作者: myfreedos     时间: 2009-8-13 08:56
xialai kankan
作者: zgtzyq     时间: 2009-8-28 01:25
谢谢,下载看看
作者: zgtzyq     时间: 2009-8-28 01:25
可以下载吗
作者: mlkflkf     时间: 2009-9-5 00:24
先看看,谢了
作者: zzybq     时间: 2009-9-14 00:26
看看先,
作者: tkdr2001     时间: 2009-10-7 09:46
不错
作者: ruanzhi     时间: 2009-10-21 21:59
下来看看再说。
作者: hny     时间: 2009-11-6 19:18
谢谢楼主分享~~  支持原创
作者: netreg     时间: 2009-11-16 17:41
好,楼主真强啊!
作者: marione     时间: 2009-11-17 21:10
支持下~~~~
作者: xidian218     时间: 2009-11-21 21:59
资料不错啊
作者: linuxdos110     时间: 2009-12-16 23:53
好东西。。
作者: lyqq1982     时间: 2009-12-25 16:18
学习一下啊
作者: arey     时间: 2010-1-22 10:34
正在做测试VGA的程序,下来看看
作者: hhsccw     时间: 2010-1-26 23:34
好东西~
作者: Jackholmes     时间: 2010-3-11 18:09
看一下。。应该不错哟
作者: madrat     时间: 2010-3-30 09:43
正需要,Windows下的代码太复杂了~~
作者: pois0n     时间: 2010-3-31 14:00
好,楼主真强啊!
作者: zhsun     时间: 2010-4-3 16:17
多谢 正需要这个做参考
作者: jjww2999     时间: 2010-6-16 10:10
作个记号,<图形库,数学库>
作者: shxch1987     时间: 2010-7-13 09:49
学习一下,谢谢!
作者: a56225669     时间: 2010-7-24 17:52
谢谢
作者: liuzuqing     时间: 2010-9-19 09:37

作者: apingzai     时间: 2010-11-20 21:26
谢谢^_^
作者: dragonye2012     时间: 2010-11-22 19:25

作者: tianguo     时间: 2010-11-26 21:51    标题: xiex

反反复反复反复
作者: pengqh     时间: 2010-12-12 23:12
c语言
作者: leafmaple     时间: 2010-12-21 08:18
谢谢分享......
作者: svols     时间: 2010-12-30 18:56
学习了,多谢lz啊
作者: angussx     时间: 2011-1-24 12:13
支持一下了
作者: qingwashaoye     时间: 2017-2-1 01:08
顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶
顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶          顶顶
顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶                顶顶
顶顶顶顶顶顶顶顶顶顶    顶顶          顶顶顶顶顶顶顶
顶顶顶顶顶顶        顶顶  顶顶顶    顶顶顶顶顶顶顶顶
顶             顶顶顶顶顶顶顶   顶顶顶顶顶顶顶顶顶
顶           顶顶顶顶顶顶顶顶         顶顶顶顶
顶顶         顶顶顶顶顶顶顶顶            顶顶
顶顶顶 顶顶顶    顶顶顶顶顶顶    顶顶顶顶顶     顶顶
顶顶顶顶顶顶顶顶   顶顶顶顶顶顶  顶顶顶 顶顶顶顶   顶顶顶
顶顶顶顶顶顶顶顶   顶顶顶顶顶   顶顶顶  顶顶顶   顶顶顶
顶顶顶顶顶顶顶顶   顶顶顶顶顶   顶顶顶  顶顶顶   顶顶顶
顶顶顶顶顶顶顶顶   顶顶顶顶顶   顶顶   顶顶顶   顶顶顶
顶顶顶顶顶顶顶顶   顶顶顶顶顶   顶顶   顶顶顶   顶顶顶
顶顶顶顶顶顶顶顶   顶顶顶顶顶   顶顶   顶顶顶   顶顶顶
顶顶顶顶顶顶顶顶   顶顶顶顶顶   顶顶   顶顶顶   顶顶顶
顶顶顶顶顶顶顶顶   顶顶顶顶顶  顶顶顶  顶顶顶顶   顶顶顶
顶顶顶顶顶顶顶顶   顶顶顶顶顶  顶顶顶  顶顶顶顶   顶顶顶
顶顶顶        顶顶顶顶顶顶 顶顶  顶顶顶顶    顶顶顶
顶顶顶顶       顶顶顶顶顶顶顶顶顶  顶顶 顶顶顶  顶顶顶
顶顶顶顶顶顶     顶顶顶顶顶顶顶顶   顶顶    顶顶顶顶顶
顶顶顶顶顶顶顶顶   顶顶顶顶顶顶顶    顶顶顶      顶顶
顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶     顶顶顶顶顶      顶
顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶    顶顶顶顶顶顶顶顶     顶
顶顶顶顶顶顶顶顶顶顶顶顶顶     顶顶顶顶顶顶顶顶顶顶    顶
顶顶顶顶顶顶顶顶顶顶顶顶  顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶  顶顶
顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶
作者: fsyzwuli     时间: 2017-12-6 08:53
资料这么齐全!强啊!!!
作者: zzz19760225     时间: 2017-12-6 15:42    标题: 楼主文件铺开

数学与图形库
|
|---MATH.INC
|---MYGRAPH.INC
|---MYHEAD.H
|---readme.txt
|
|---3ddemo.rar
        |----3DDEMO.C
        |----3DDEMO.exe
        |----3DDEMO2.C
        |----3DDEMO2.exe
        |----3DDEMO3.C
        |----3DDEMO3.exe
---------------------------------------------------------------------------------------------------
|---MATH.INC

/*Math.Inc   BY BIANMING 2004-11-24
/*  by   边明明,西安邮电学院。*/
/*这个是俺自己写的一个c语言数学库,在tc2.0下测试通过。请大家放心使用O_o!

  特别加入了三维向量的运算TDA,用于图形库中的画向量。


Radians   -  converts degrees to radians
Degrees   -  converts radians to degrees
CosD      -  cosine in degrees
SinD      -  sine   in degrees
Power     -  power a^n
Log       -  log base 10
Exp10     -  exp base 10
Sign      -  negative=-1   positive=1   null=0
IntSign   -  negative=-1   positive=1   null=0
IntSqrt   -  integer aquare root
IntPower  -  integer power a^n
*/

#define Ln10          2.30258509299405E+000
#define Pi            3.1415927
#define PiOver180     1.74532925199433E-002
#define PiUnder180    5.72957795130823E+001

typedef enum {false, true} Boolean;
typedef unsigned char      Byte;
typedef unsigned int       Word;

int Round(float x)
   {
    return((int)(x+0.5));
   }

int Trunc(float x)
   {
    return((int)(x));
   }

float SqrFP(float x)
   {
    return(x*x);
   }

int Sqr(int x)
   {
    return(x*x);
   }

float Radians(float Angle)
   {
    return(Angle*PiOver180);
   }

float Degrees(float Angle)
   {
    return(Angle*PiUnder180);
   }

float CosD(float Angle)
   {
    return(cos(Radians(Angle)));
   }

float SinD(float Angle)
   {
    return(sin(Radians(Angle)));
   }

float Power(float Base,int E)
   {
    float BPower;
    int   t;

    if(E==0) return(1);
    else
      {
       BPower=1.0;
       for(t=1;t<=E;t++)
          BPower*=Base;
       return(BPower);
      }
   }

float Log(float x)
   {
    return(log(x)/Ln10);
   }

float Exp10(float x)
   {
    return(exp(x*Ln10));
   }

float Sign(float x)
   {
    if(x<0) return(-1);
    if(x>0) return(1);
    else    return(0);
   }

int IntSign(int x)
   {
    if(x<0) return(-1);
    if(x>0) return(1);
    else    return(0);
   }

int IntSqrt(int x)
   {
    int OddInt,OldArg,FirstSqrt;

    OddInt=1;
    OldArg=x;
    while(x>=0)
       {
        x-=OddInt;
        OddInt+=2;
       }
    FirstSqrt=OddInt >> 1;
    if(Sqr(FirstSqrt)-FirstSqrt+1 > OldArg)
      return(FirstSqrt-1);
    else
      return(FirstSqrt);
   }

int IntPower(int Base,int E)
   {
    int BPower;
    int   t;

    if(E==0) return(1);
    else
      {
       BPower=1;
       for(t=1;t<=E;t++)
          BPower*=Base;
       return(BPower);
      }
   }

/*三维向量操作
vec           -   Make Vector三参数生成一实数向量
VecInt        -   Make Integer Vector三参数生成一整数向量
UnVec         -   Get Components of Vector实向量x,y,z轴传给三个数
UnVecInt      -   Get Components of Integer Vector整向量x,y,z轴传给三个数
VecDot        -   Vector Dot Product两向量的点乘积
VecCross      -   Vector Cross Product向量叉乘积
VecLen        -   Vector length向量长度
VecNormalize  -   Vector Normalize 规格化向量,0长度向量不能规格化
VecMatxMult   -   Vector Matrix Multiple 一个4×4矩阵于一向量乘积
VecSub        -   Vector Subtraction俩向量的差赋值给另一向量
VecSubInt     -   Vector Subtraction Integer整数向量差赋值
VecAdd        -   Vector Addition俩向量和赋值给另一向量
VecAdd3       -   Vector Addition仨向量和赋值给另一向量
VecCopy       -   Vector Copy向量拷贝
VecLinComb    -   Vector Linear Combination  C=R*A+S*B
VecScalMult   -   Vector Scalar Multiple实向量与实数乘积
VecScalMultI  -   Vector Scalar Multiple整向量与实数乘积
VecScalMultInt-   Vector Scalar Multiple and Rounding整向量与整数乘积
VecAddScalMult-   Vector Add scalar Multiple  C=R*A+B
VecNull       -   Vector Null  实向量置零
VecNullInt    -   Vector Null Integer整向量置零
VecElemMult   -   Vector Element Mulitiple C=R*A*B
*/

typedef float TDA[3];
typedef int   TDIA[3];
typedef float FDA[4];
typedef float Matx4x4[4][4];

void Vec(float r,float s,float t,TDA a)
    {
     a[0]=r;a[1]=s;a[2]=t;
    }

void VecInt(int r,int s,int t,TDIA a)
    {
     a[0]=r;a[1]=s;a[2]=t;
    }

void UnVec(TDA a,float *r,float *s,float *t)
    {
     *r=a[0];*s=a[1];*t=a[2];
    }

void UnVecInt(TDIA a,int *r,int *s,int *t)
    {
     *r=a[0];*s=a[1];*t=a[2];
    }

float VecDot(TDA a,TDA b)
    {
     return(a[0]*b[0]+a[1]*b[1]+a[2]*b[2]);
    }

void VecCross(TDA a,TDA b,TDA c)
    {
     c[0]=a[1]*b[2]-a[2]*b[1];
     c[1]=a[2]*b[0]-a[0]*b[2];
     c[2]=a[0]*b[1]-a[1]*b[0];
    }

float VecLen(TDA a)
    {
     return(sqrt(SqrFP(a[0])+SqrFP(a[1])+SqrFP(a[2])));
    }

void VecNormalize(TDA a)
    {
     float dist,invdist;

     dist=VecLen(a);
     if(!(dist==0.0))
       {
        invdist=1.0/dist;
        a[0]*=invdist;
        a[1]*=invdist;
        a[2]*=invdist;
       }
     else
       {
        puts("Zero-Length Vectors Cannot be Nomalized!!!");
        exit(1);
       }
    }

void VecMatxMult(FDA a,Matx4x4 Matrix,FDA b)
    {
     int mrow, mcol;

     for(mcol=0;mcol<4;mcol++)
        {
         b[mcol]=0;
         for(mrow=0;mrow<4;mrow++)
         b[mcol]+=a[mrow]*Matrix[mrow][mcol];
        }
    }

void VecSub(TDA a,TDA b,TDA c)
    {
     c[0]=a[0]-b[0];
     c[1]=a[1]-b[1];
     c[2]=a[2]-b[2];
    }

void VecSubInt(TDIA a,TDIA b,TDIA c)
    {
     c[0]=a[0]-b[0];
     c[1]=a[1]-b[1];
     c[2]=a[2]-b[2];
    }

void VecAdd(TDA a,TDA b,TDA c)
    {
     c[0]=a[0]+b[0];
     c[1]=a[1]+b[1];
     c[2]=a[2]+b[2];
    }

void VecAdd3(TDA a,TDA b,TDA c,TDA d)
    {
     d[0]=a[0]+b[0]+c[0];
     d[1]=a[1]+b[1]+c[1];
     d[2]=a[2]+b[2]+c[2];
    }

void VecCopy(TDA a,TDA b)
    {
     b[0]=a[0];
     b[1]=a[1];
     b[2]=a[2];
    }

void VecLinComb(float r,TDA a,float s,TDA b,TDA c)
    {
     c[0]=r*a[0]+s*b[0];
     c[1]=r*a[1]+s*b[1];
     c[2]=r*a[2]+s*b[2];
    }

void VecScalMult(float r,TDA a,TDA b)
    {
     b[0]=r*a[0];
     b[1]=r*a[1];
     b[2]=r*a[2];
    }

void VecScalMultI(float r,TDIA a,TDA b)
    {
     b[0]=r*a[0];
     b[1]=r*a[1];
     b[2]=r*a[2];
    }

void VecScalMultInt(float r,TDA a,TDIA b)
    {
     b[0]=Round(r*a[0]);
     b[1]=Round(r*a[1]);
     b[2]=Round(r*a[2]);
    }

void VecAddScalMult(float r,TDA a,TDA b,TDA c)
    {
     c[0]=r*a[0]+b[0];
     c[1]=r*a[1]+b[1];
     c[2]=r*a[2]+b[2];
    }

void VecNull(TDA a)
    {
     a[0]=0.0;
     a[1]=0.0;
     a[2]=0.0;
    }

void VecNullInt(TDIA a)
    {
     a[0]=0;
     a[1]=0;
     a[2]=0;
    }

void VecElemMult(float r, TDA a,TDA b,TDA c)
    {
     c[0]=r*a[0]*b[0];
     c[1]=r*a[1]*b[1];
     c[2]=r*a[2]*b[2];
    }

/*
ZeroMatrix            -   zeros the elements of a 4x4 matrix
Translate3D           -   make translation matrix把一向量线性转换道空间的新位置生成新矩阵
Scale3D               -   make Scaling matrix使一向量转换成矩阵
Rotate3D              -   make rotation matrix使一向量旋转生成一矩阵
ZeroAllMatricies      -   zeros all matricies used in tranformation
Multiply3DMatricies   -   multiply 2 4x4 matricies矩阵相乘
PrepareMatrix         -   prepare the transformation matrix (Tm=S*R*T)生成一个完整的仿射变换矩阵
PrepareInvMatrix      -   prepare the inverse transformation matrix
Transform             -   multiply a vertex by the transformation matrix 向量与矩阵相乘生成新向量
*/

void ZeroMatrix(Matx4x4 a)
    {
     int i,j;

     for(i=0;i<4;i++)
        for(j=0;j<4;j++)
           a[j]=0.0;
    }

void Translate3D(float tx, float ty, float tz,Matx4x4 a)
    {
     int i;

     ZeroMatrix(a);
     for(i=0;i<4;i++)
        a=1.0;
     a[0][3]=-tx;
     a[1][3]=-ty;
     a[2][3]=-tz;
    }

void Scale3D(float sx,float sy,float sz,Matx4x4 a)
    {
     ZeroMatrix(a);
     a[0][0]=sx;
     a[1][1]=sy;
     a[2][2]=sz;
     a[3][3]=1.0;
    }

void Rotate3D(int m,float theta, Matx4x4 a)
    {
     int m1,m2;
     float c,s;

     ZeroMatrix(a);
     a[m-1][m-1]=1.0;
     a[3][3]=1.0;
     m1=(m%3)+1;
     m2=(m1%3);
     m1=-1;
     c=CosD(theta);
     s=SinD(theta);
     a[m1][m1]=c;
     a[m1][m2]=s;
     a[m2][m2]=c;
     a[m2][m1]=-s;
    }

void Multiply3DMatricies(Matx4x4 a,Matx4x4 b,Matx4x4 c)
    {
     int i,j,k;
     float ab;

     for(i=0;i<4;i++)
        {
         for(j=0;j<4;j++)
            {
             ab=0;
             for(k=0;k<4;k++)
                ab+=a[k]*b[k][j];
             c[j]=ab;
            }
        }
    }

void MatCopy(Matx4x4 a,Matx4x4 b)
    {
     int i,j;

     for(i=0;i<4;i++)
        for(j=0;j<4;j++)
           b[j]=a[j];
    }

void PrepareMatrix(float tx,float ty,float tz,
                   float sx,float sy,float sz,
                   float rx,float ry,float rz,
                   Matx4x4 XForm)
    {
     Matx4x4 m1,m2,m3,m4,m5,m6,m7,m8,m9;

     Scale3D(sx,sy,sz,m1);
     Rotate3D(1,rx,m2);
     Rotate3D(2,ry,m3);
     Rotate3D(3,rz,m4);
     Translate3D(tx,ty,tz,m5);
     Multiply3DMatricies(m2,m1,m6);
     Multiply3DMatricies(m3,m6,m7);
     Multiply3DMatricies(m4,m7,m8);
     Multiply3DMatricies(m5,m8,m9);
     MatCopy(m9,XForm);
    }

void PrepareInvMatrix(float tx,float ty,float tz,
                      float sx,float sy,float sz,
                      float rx,float ry,float rz,
                      Matx4x4 XForm)
    {
     Matx4x4 m1,m2,m3,m4,m5,m6,m7,m8,m9;

     Scale3D(sx,sy,sz,m1);
     Rotate3D(1,rx,m2);
     Rotate3D(2,ry,m3);
     Rotate3D(3,rz,m4);
     Translate3D(tx,ty,tz,m5);
     Multiply3DMatricies(m4,m5,m6);
     Multiply3DMatricies(m3,m6,m7);
     Multiply3DMatricies(m2,m7,m8);
     Multiply3DMatricies(m1,m8,m9);
     MatCopy(m9,XForm);
    }

void Transform(TDA a,Matx4x4 m,TDA b)
    {
     b[0]=m[0][0]*a[0]+m[0][1]*a[1]+m[0][2]*a[2]+m[0][3];
     b[1]=m[1][0]*a[0]+m[1][1]*a[1]+m[1][2]*a[2]+m[1][3];
     b[0]=m[2][0]*a[0]+m[2][1]*a[1]+m[2][2]*a[2]+m[2][3];
    }

---------------------------------------------------------------------------------------------------
|---MYGRAPH.INC

/*MYGraph.Inc  BY BIANMING 2005-11-27
InitGraphics     -   initialize graphics
                 -   屏幕初始化,指明图形驱动路径 gpath
ExitGraphics     -   sound and wait for key press before exiting grapgics
                 -   生成图型后的调用
SetAxisZP        -   设置图形原点座标,默认下以(0,439)为原点
SetAxisMid       -   设置图形原点座标为屏幕中央
Plot             -   place pixel to screen
CylPlot          -   极坐标画点法
GetPixel         -   gets  pixel
                 -   从指定点返回颜色
Circle           -   circle draw routine
                 -   画给定圆心,半径,颜色的圆
Line             -   line draw routine
                 -   从给定点到另一点给定颜色的线
ShowAxis         -   显示2D坐标轴 ,set X-Axis color,set Y-Axis color.
Title            -   set up text screen colors
                 -   为以文本方式输出准备屏幕
*/


int CentreX ,CentreY;    /* 原点坐标*/
int MaxX; /*getmaxx*/
int MaxY; /*getmaxy*/
int MaxXres;
int MaxYres;
int MaxColor;  /*最大颜色种数*/


void InitGraphics(char *gpath)
    {
     int gd=DETECT,gm;
     initgraph(&gd,&gm,gpath);
     MaxX=getmaxx(); /*getmaxx*/
     MaxY=getmaxy(); /*getmaxy*/
     MaxXres=MaxX+1;
     MaxYres=MaxY+1;
     MaxColor=getmaxcolor();
     CentreX=0;
     CentreY=MaxY;
    }

void ExitGraphics(void)
    {
     sound(1000);
     delay(500);
     nosound();
     getch();
     cleardevice();
     closegraph();
    }

void SetAxisZP(int x,int y)  /* 设置图形原点座标,默认下以(0,439)为原点*/
    {
     CentreX=x;
     CentreY=y;
    }

void SetAxisMid(void)  /*设置图形原点座标为屏幕中央*/
    {
     int x,y;
     x=MaxX>>1;
     y=MaxY>>1;
     SetAxisZP(x,y);
    }

void Plot(int x,int y,unsigned char color)
    {
     int ScreenX,ScreenY;
     ScreenX=x+CentreX;
     ScreenY=MaxY-(y+CentreY);
     if((ScreenX>=0)&&(ScreenX<=MaxX)&&(ScreenY>=0)&&(ScreenY<=MaxY))
       putpixel(ScreenX,ScreenY,color);
    }
void CylPlot(float R,float Theta,Byte color)
    {
     int x,y;

     Theta=Radians(Theta);
     x=(int)(R*sin(Theta));
     y=(int)(R*cos(Theta));
     Plot(x,y,color);
    }

int GetPixel(int x,int y)
    {
     int ScreenX,ScreenY;
     ScreenX=x+CentreX;
     ScreenY=MaxY-(y+CentreY);
     if((ScreenX>=0)&&(ScreenX<=MaxX)&&(ScreenY>=0)&&(ScreenY<=MaxY))
        return(getpixel(ScreenX,ScreenY));
     else
        return(0);
    }

void Swap(int *first,int *second)
    {
     int tmp;

     tmp=*first;
     *first=*second;
     *second=tmp;
    }

void Circle(int x,int y,int radius, Byte color)  /*600×480  16 color*/
    {
     int a,af,b,bf,target,r2,asp;

     asp=94;

     target=0;
     a=radius;
     b=0;
     r2=Sqr(radius);
     while(a>=b)
        {
         b=Round(sqrt(r2-Sqr(a)));
         Swap(&target,&b);
         while(b<target)
            {
             af=(asp*a)/100;
             bf=(asp*b)/100;
             Plot(x+af,y+b,color);
             Plot(x+bf,y+a,color);
             Plot(x-af,y+b,color);
             Plot(x-bf,y+a,color);
             Plot(x-af,y-b,color);
             Plot(x-bf,y-a,color);
             Plot(x+af,y-b,color);
             Plot(x+bf,y-a,color);
             ++b;
            }
         --a;
        }
    }

void Line(int xx1,int yy1,int xx2,int yy2,Byte color)
    {
     int LgDelta,ShDelta,Cycle,LgStep,ShStep;

     LgDelta=xx2-xx1;
     ShDelta=yy2-yy1;
     if(LgDelta<0)
       {
        LgDelta=-LgDelta;
        LgStep=-1;
       }
     else
        LgStep=1;
     if(ShDelta<0)
       {
        ShDelta=-ShDelta;
        ShStep=-1;
       }
     else
        ShStep=1;
     if(ShDelta<LgDelta)
       {
        Cycle=LgDelta>>1;
        while(xx1!=xx2)
           {
            Plot(xx1,yy1,color);
            Cycle+=ShDelta;
            if(Cycle>LgDelta)
              {
               Cycle-=LgDelta;
               yy1+=ShStep;
              }
            xx1+=LgStep;
           }
        Plot(xx1,yy1,color);
       }
     else
       {
        Cycle=ShDelta>>1;
        Swap(&LgDelta,&ShDelta);
        Swap(&LgStep,&ShStep);
        while(yy1!=yy2)
           {
            Plot(xx1,yy1,color);
            Cycle+=ShDelta;
            if(Cycle>LgDelta)
              {
               Cycle-=LgDelta;
               xx1+=ShStep;
              }
            yy1+=LgStep;
           }
        Plot(xx1,yy1,color);
       }
    }


void ShowAxis(Byte xcolor,Byte ycolor)
    {
     Line(-200,0,200,0,xcolor);
     Line(190,7,200,0,xcolor);
     Line(190,-7,200,0,xcolor);/*x轴*/
     Line(0,-200,0,200,ycolor);
     Line(7,190,0,200,ycolor);
     Line(-7,190,0,200,ycolor);/*y轴*/
    }


void Title()
    {
     textcolor(YELLOW);
     textbackground(BLUE);
     clrscr();
    }

/*FOR 3D GRAPHICS
InitPer            -   observer location and distances
                   -   给三维空间的观察者坐标赋值 Per控制是否是透视图 true-是 false-不是
InitPlotting       -   rotation and tilt angles
                   -   初始化用于三维图形中的变量
MapCoordinates     -   maps 3D space onto the 2D screen
                   -   将三维点投影到观察平面
Plot3D             -   plot a cartesian system point
                   -   在二维屏幕上画给定三维坐标和颜色的点
CylPlot3D          -   plot a Cylindrical system point
                   -   在二维屏幕上画给定半径,与xy平面角度,z高度和颜色的点 以Cylindrical坐标系统给出
SphPlot3D          -   plot a spherical system point
                   -   在二维屏幕上画以球形坐标r半径,Theta xy平面半径线的角度,Phi xz平面半径线的角度
                   -   的点
VecLine3D          -   plots a line from 3D coordinates
                   -   画以向量参数1为起点,向量参数2为终点,color为颜色的3D直线
Line3D             -   画(x1,y1,z1)-(x2,y2,z2)的直线
Vec3D              -  画向量
Ball               -  画一个球体 (x,y,z)球心坐标,R半径,color颜色
*/

Boolean PerspectivePlot;
float   Mx,My,Mz,ds;  /*观察者坐标(Mx,My,Mz),距离(ds)赋值*/

void InitPer(Boolean Per,float x,float y,float z,float m)
    {
     PerspectivePlot=Per;
     Mx=x;
     My=y;
     Mz=z;
     ds=m;
    }

float Angl    ,Tilt;       /* 观察者的旋转角,俯视角*/
float CosA    ,SinA;
float CosB    ,SinB;
float CosACosB,SinASinB;
float CosASinB,SinACosB;

void InitPlotting(float Ang,float Tlt)
    {
     Angl    =Ang;
     Tilt    =Tlt;
     CosA    =CosD(Angl);
     SinA    =SinD(Angl);
     CosB    =CosD(Tilt);
     SinB    =SinD(Tilt);
     CosACosB=CosA*CosB;
     SinASinB=SinA*SinB;
     CosASinB=CosA*SinB;
     SinACosB=SinA*CosB;
    }
                  
void MapCoordinates(float x,float y,float z,int *Xp,int *Yp)
    {
     float Xt,Yt,Zt;

     Xt=(Mx+x*CosA-y*SinA);
     Yt=(My+x*SinASinB+y*CosASinB+z*CosB);
     if(PerspectivePlot)
       {
        Zt=Mz+x*SinACosB+y*CosACosB-z*SinB;
        *Xp=Round(ds*Xt/Zt);
        *Yp=Round(ds*Yt/Zt);
       }
     else
       {
        *Xp=Round(Xt);
        *Yp=Round(Yt);
       }
    }

void Plot3D(float x,float y,float z,Byte Color)
    {
     int Xp,Yp;
     MapCoordinates(x,y,z,&Xp,&Yp);
     Plot(Xp,Yp,Color);
    }

void CylPlot3D(float Rho,float Theta,float z,Byte Color)
    {
     float x,y;

     Theta=Radians(Theta);
     x=Rho*cos(Theta);
     y=Rho*sin(Theta);
     Plot3D(x,y,z,Color);
    }

void SphPlot3D(float r,float Theta,float Phi,Byte Color)
    {
     float x,y,z;

     Theta=Radians(Theta);
     Phi=Radians(Phi);
     x=r*sin(Theta)*cos(Phi);
     y=r*sin(Theta)*sin(Phi);
     z=r*cos(Theta);
     Plot3D(x,y,z,Color);
    }

void VecLine3D(TDA P1,TDA P2,Byte Color)/*向量画3D直线*/
    {
     int   Xp1,Yp1;
     int   Xp2,Yp2;
     float x1,y1,z1;
     float x2,y2,z2;

     UnVec(P1,&x1,&y1,&z1);
     UnVec(P2,&x2,&y2,&z2);
     MapCoordinates(x1,y1,z1,&Xp1,&Yp1);
     MapCoordinates(x2,y2,z2,&Xp2,&Yp2);
     Line(Xp1,Yp1,Xp2,Yp2,Color);
    }

void Line3D(float x1,float y1,float z1,float x2,float y2,float z2,int Color)
    {
     int   Xp1,Yp1;
     int   Xp2,Yp2;
     MapCoordinates(x1,y1,z1,&Xp1,&Yp1);
     MapCoordinates(x2,y2,z2,&Xp2,&Yp2);
     Line(Xp1,Yp1,Xp2,Yp2,Color);     
    }

void Vec3D(TDA P1,int Color)  /*画P1向量*/
    {
     int   Xp1,Yp1;
     float x1,y1,z1;
     UnVec(P1,&x1,&y1,&z1);
     MapCoordinates(x1,y1,z1,&Xp1,&Yp1);
     Line(0,0,Xp1,Yp1,Color);
    }

void Ball(int x,int y,int z,int R,Byte color)  /*画一个球体 (x,y,z)球心坐标,R半径,color颜色*/
    {
     int Theta,Phi;
     for(Theta=0;Theta<180;Theta+=20)
        {
         for(Phi=0;Phi<180;Phi++)
           {
            Plot3D(x+R*CosD(Phi)*CosD(Theta),y+R*CosD(Phi)*SinD(Theta),z+R*SinD(Phi),color);
            Plot3D(x+R*CosD(Phi)*CosD(Theta),y+R*CosD(Phi)*SinD(Theta),z-R*SinD(Phi),color);
           }
        }
     for(Phi=-80;Phi<90;Phi+=20)
        {
         for(Theta=0;Theta<180;Theta++)
           {
            Plot3D(x+R*CosD(Phi)*CosD(Theta),y+R*CosD(Phi)*SinD(Theta),z+R*SinD(Phi),color);
            Plot3D(x+R*CosD(Phi)*CosD(Theta),y-R*CosD(Phi)*SinD(Theta),z+R*SinD(Phi),color);
           }
        }
    Plot3D(x,y,z,color+2);
    }

/*
GetPixel3D-  gets 3D pixel
          -  从指定3D点返回颜色

COLORS:
     BLACK     0 黑色 DARKGRAY      8 深灰
     BLUE      1 兰色 LIGHTBLUE     9 深兰
     GREEN     2 绿色 LIGHTGREEN   10 淡绿
     CYAN      3 青色 LIGHTCYAN    11 淡青
     RED       4 红色 LIGHTRED     12 淡红
     MAGENTA   5 洋红 LIGHTMAGENTA 13 淡洋红
     BROWN     6 棕色 YELLOW       14 黄色
     LIGHTGRAY 7 淡灰 WHITE        15 白色

*/


int GetPixel3D(int x,int y,int z)
   {
    int xp,yp;
    MapCoordinates(x,y,z,&xp,&yp);
    return(GetPixel(xp,yp));
   }


/* Set up of Coordinate Axes and color Palette

ShowAxis3D   -  显示3D坐标轴 And Set Axis color
                  
ShowPalette  -  在屏幕上画出调色板
*/


void ShowAxis3D(Byte xcolor,Byte ycolor,Byte zcolor)
    {
     int x,y,z;
     
     Line3D(-200, 0,0,200,0,0,xcolor);
     Line3D(190,-7,0,200,0,0,xcolor);
     Line3D(190, 7,0,200,0,0,xcolor); /*x-Axis*/
     Line3D( 0,-200,0,0,200,0,ycolor);
     Line3D(-7,190,0,0,200,0,ycolor);
     Line3D( 7,190,0,0,200,0,ycolor); /*y-Axis*/
     Line3D(0 ,0,-200,0,0,200,zcolor);
     Line3D(0,-7,190,0,0,200,zcolor);
     Line3D(0, 7,190,0,0,200,zcolor); /*z-Axis*/     
          
    }

void ShowPalette(void)
    {
     int  y;
     Byte Color;
     for(Color=1;Color<MaxColor;Color++)
        {       
             for(y=0;y<15;y++)
                 Line(-300,y+15*Color,-290,y+15*Color,Color);
        }
    }


---------------------------------------------------------------------------------------------------
|---MYHEAD.H

#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include <math.h>
#include <graphics.h>
#include "math.inc"
#include "mygraph.inc"

---------------------------------------------------------------------------------------------------
|---readme.txt

/*  by   边明明,西安邮电学院。*/
/*这个是俺自己写的一个c语言图形库和数学库,在tc2.0下测试通过。请大家放心使用O_o!

  所有函数是相对于调整的坐标原点进行画图的,不是以屏幕原点坐标,这一点请大家注意!!!

  特点是所有作图函数加入了颜色设置,
  特别加入了3D作图,3D作图时要初始化观察者的坐标和距离(InitPer())。
  还要初始化观察者相对与x轴旋转角,相对与xy平面的俯视角(InitPoltting())。
  因为所有的3D作图函数都是相对于这个初始值进行作图的。
  

  注意:在包含这个库时还要包含进stdio.h,dos.h,conio.h,math.h,graphics.h,math.inc这些库。
        这里也可以一个include "myhead.h"。这个文件在压缩包里。

  您可以在这个基础上继续建立自己的库。您也可以进行任意修改。O_o!

  这里有几个例子:3Ddemo.c,3Ddemo2.c,3Ddemo3.c。




                                     bmmm906@sohu.com
                                     qq:53793208
                                     西安邮电学院南校区 电科0303班  75号信箱。

---------------------------------------------------------------------------------------------------

[ Last edited by zzz19760225 on 2017-12-6 at 15:57 ]
作者: zzz19760225     时间: 2017-12-6 15:45    标题: 楼主文件再铺开

3ddemo.rar
|----3DDEMO.C
|----3DDEMO.exe
|----3DDEMO2.C
|----3DDEMO2.exe
|----3DDEMO3.C
|----3DDEMO3.exe
---------------------------------------------------------------------------------------------------
|----3DDEMO.C

#include "c:\tc\cexe\myhead.h"
#define LEFT   0x4b00
#define RIGHT  0x4d00
#define UP     0x4800
#define DOWN   0x5000
#define ESC    0x011b

void ShowXYM(void);
void PWM(void);


void main(void)
    {
     int Theta=240,Phi=12;
     int key;

      InitGraphics("c:\\tc\\tc2\\bgi");
      SetAxisMid();
     while(!(kbhit()))
     {
      InitPer(0,0,0,500,50);/*3D Init*/
      Theta%=360;
      Phi  %= 90;
      InitPlotting(Theta,Phi);

      ShowPalette();

      ShowXYM();
      ShowAxis3D(BLUE,GREEN,CYAN);


      PWM();
      key=bioskey(0);
      if(key==LEFT)  Theta+=5;
      if(key==RIGHT) Theta-=5;
      if(key==UP)    Phi  +=2;
      if(key==DOWN)  Phi  -=2;
      if(key==ESC)   ExitGraphics();
      cleardevice();
     }
    }

void ShowXYM(void)
    {
     int x,y;

     for(x=-300;x<=300;x+=40)
        Line3D(x,-300,0,x,300,0,5);
     for(y=-300;y<=300;y+=40)
        Line3D(-300,y,0,300,y,0,5);
    }

void PWM(void)
    {
     long x,y,z;
     for(x=-200;x<=200;x+=20)
     for(y=-200;y<=200;y++)
        {
         z=Round(sqrt(x*x+y*y)/2);
         Plot3D(x,y,z,2);
        }
     for(y=-200;y<=200;y+=20)
     for(x=-200;x<=200;x++)
        {
         z=Round(sqrt(x*x+y*y)/2);
         Plot3D(x,y,z,2);
        }
    }

---------------------------------------------------------------------------------------------------
|----3DDEMO2.C

#include "c:\tc\cexe\myhead.h"
#define LEFT   0x4b00
#define RIGHT  0x4d00
#define UP     0x4800
#define DOWN   0x5000
#define ESC    0x011b

void ShowXYM(Byte);
void PWM(Byte);
void Ball(int,int,int,int,Byte);

void main(void)
    {
     int Theta=240,Phi=12;
     int key;Byte color=0;

      InitGraphics("c:\\tc\\tc2\\bgi");
      SetAxisMid();
     while(!(kbhit()))
     {
      InitPer(0,0,0,500,50);/*3D Init*/
      Theta%=360;
      Phi  %= 90;
      InitPlotting(Theta,Phi);

      ShowPalette();

      ShowXYM(color+2);
      ShowAxis3D(BLUE,YELLOW,CYAN);


     /* PWM(color+4);   */

      Line3D(0,0,0,100,100,100,color+4);
      Ball(100,100,100,100,color+9);
      key=bioskey(0);
      if(key==LEFT)  Theta+=5;
      if(key==RIGHT) Theta-=5;
      if(key==UP)    Phi  +=2;
      if(key==DOWN)  Phi  -=2;
      if(key==ESC)   ExitGraphics();
      cleardevice();
     }
    }

void ShowXYM(Byte color)
    {
     int x,y;

     for(x=-300;x<=300;x+=40)
        Line3D(x,-300,0,x,300,0,color);
     for(y=-300;y<=300;y+=40)
        Line3D(-300,y,0,300,y,0,color);
    }

void PWM(Byte color)
    {
     long x,y,z;
     for(x=-200;x<=200;x+=20)
     for(y=-200;y<=200;y++)
        {
         z=Round(sqrt(x*x+y*y)/2);
         Plot3D(x,y,z,color);
        }
     for(y=-200;y<=200;y+=20)
     for(x=-200;x<=200;x++)
        {
         z=Round(sqrt(x*x+y*y)/2);
         Plot3D(x,y,z,color);
        }
    }
void Ball(int x,int y,int z,int R,Byte color)
    {
     int Theta,Phi;
     for(Theta=0;Theta<180;Theta+=20)
        {
         for(Phi=0;Phi<180;Phi++)
           {
            Plot3D(x+R*CosD(Phi)*CosD(Theta),y+R*CosD(Phi)*SinD(Theta),z+R*SinD(Phi),color);
            Plot3D(x+R*CosD(Phi)*CosD(Theta),y+R*CosD(Phi)*SinD(Theta),z-R*SinD(Phi),color);
           }
        }
     for(Phi=-80;Phi<90;Phi+=20)
        {
         for(Theta=0;Theta<180;Theta++)
           {
            Plot3D(x+R*CosD(Phi)*CosD(Theta),y+R*CosD(Phi)*SinD(Theta),z+R*SinD(Phi),color);
            Plot3D(x+R*CosD(Phi)*CosD(Theta),y-R*CosD(Phi)*SinD(Theta),z+R*SinD(Phi),color);
           }
        }
    Plot3D(x,y,z,color+2);
    }

---------------------------------------------------------------------------------------------------
|----3DDEMO3.C

#include "c:\tc\cexe\myhead.h"
#define LEFT   0x4b00
#define RIGHT  0x4d00
#define UP     0x4800
#define DOWN   0x5000
#define ESC    0x011b

void ShowXYM(Byte);

void Luo(int,int,int,int,Byte);

void main(void)
    {
     int Theta=240,Phi=12;
     int key;Byte color=0;

      InitGraphics("c:\\tc\\tc2\\bgi");
      SetAxisMid();
     while(!(kbhit()))
     {
      InitPer(0,0,0,500,500);/*3D Init*/
      Theta%=360;
      Phi  %= 90;
      InitPlotting(Theta,Phi);

      ShowPalette();

      ShowXYM(color+2);
      ShowAxis3D(BLUE,YELLOW,CYAN);


     /* PWM(color+4);   */

      Line3D(0,0,0,100,100,10,color+4);
      Luo(100,100,10,40,color+9);
      key=bioskey(0);
      if(key==LEFT)  Theta+=5;
      if(key==RIGHT) Theta-=5;
      if(key==UP)    Phi  +=2;
      if(key==DOWN)  Phi  -=2;
      if(key==ESC)   ExitGraphics();
      cleardevice();
     }
    }

void ShowXYM(Byte color)
    {
     int x,y;

     for(x=-300;x<=300;x+=40)
        Line3D(x,-300,0,x,300,0,color);
     for(y=-300;y<=300;y+=40)
        Line3D(-300,y,0,300,y,0,color);
    }

void Luo(int x,int y,int z,int r,Byte color)
    {
     int i;
     for(i=0;i<180;i++)
       {
        Plot3D(x+r*CosD(i),y+r*SinD(i),z,color);
        Plot3D(x+r*CosD(i),y-r*SinD(i),z,color);
       }
     for(i=0;i<=1800;i++)
        Plot3D(x+r*CosD(i),y+r*SinD(i),z+i/10,color);
     Plot3D(x+r*CosD(i),y+r*SinD(i),z,color);
    }

---------------------------------------------------------------------------------------------------

[ Last edited by zzz19760225 on 2017-12-6 at 15:58 ]
作者: black0592     时间: 2018-1-30 10:04
下载看看,谢谢^_^
作者: zhangxiajun     时间: 2018-1-30 10:14
好东西
作者: zhangxiajun     时间: 2018-2-24 16:31
下载下来好好研究研究
作者: forrest0111     时间: 2018-3-19 08:19
不知道对以后有没有帮助,先下载下来再说。
作者: lswd     时间: 2020-2-7 13:38
下载看看
作者: ZyDOS     时间: 2020-10-7 08:18
下载看看