几个图形(02)
2008-03-08 12:43:36 来源:WEB开发网核心提示:/*分型图形库数学函数头文件*//*math.inc*/#define Ln10 2.30258509299405E+000 /*数学常数定义*/#define Pi 3.1415927#define PiOver180 1.74532925199433E-002#define PiUnder180 5.729577
/*分型图形库数学函数头文件*/
/*math.inc*/
#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 adians(float Angle) /*弧度换算函数*/
{
return(Angle*PiOver180);
}
float Degress(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 EXPonent) /*求取以e为底的幂函数*/
{
float BPower;
int t;
if(Exponent==0)
return 1;
else{
BPower=1.0;
for(t=1;t<=Exponent;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;
else{
if(x>0)
return 1;
else return 0;
}
}
int IntSign(int x) /*整型符号函数*/
{
if(x<0)
return -1;
else{
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(Sqrt(FirstSqrt)-FirstSqrt+1>OldArg)
return(FirstSqrt-1);
else
return(FirstSqrt);
}
int IntPower(int Base,int Exponent) /*求以e为底的整幂函数*/
{
if(Exponent==0)
return 1;
else
return(Base*IntPower(Base,Exponent-1));
}
/*与向量和矩阵相关的函数库*/
/*Vector and Matrix Routines*/
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,float *r,float *s,float *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/dist;
A[0]*=invdist;
A[1]*=invdist;
A[2]=invdist;
}
else{
puts("Zero_length Vectors Cannot be Normalized");
exit(1);
}
}
void VexMatxult(FDA A,Matx4x4 Matix,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,TDA C) /*整数向量相减函数*/
{
C[0]=A[0]-B[0];
C[1]=A[1]-B[1];
C[2]=A[2]-B[2];
[]
赞助商链接