/*分型圖形庫數學函數頭文件*/
/*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];