?? xvector.inl
字號:
#ifndef __XVECTOR_INLINE_INCLUDE__
#define __XVECTOR_INLINE_INCLUDE__
#ifndef IN_MATHLIB_NAMESPACE
#error You cann't include this file out the XMathLib namespace
#endif
class _MATH_LIB_EXPORT_ XVector
{
public:
//Constructor build a vector
XVector(float _x = 0,float _y = 0,float _z = 0,float _w = 1):x(_x),y(_y),z(_z),w(_w){};
XVector(XVector2D& v){x = v.x ; y = v.y ; z = 0 ; w = 1 ;};
XVector(XVector3D& v){x = v.x ; y = v.y ; z = v.z ; w = 1 ;};
~XVector(){};//Force a Null destorier
public:
float XVector::fabs() {return (float) sqrt(x*x + y*y + z*z); }
float XVector::dp3(XVector& v1) {return v1.x * x + v1.y * y + v1.z * z;}
float XVector::dp4(XVector& v1) {return v1.x * x + v1.y * y + v1.z * z + v1.w * w;}
void XVector::add(XVector& v1) { x += v1.x; y += v1.y; z += v1.z; }
void XVector::sub(XVector& v1) { x += v1.x; y += v1.y; z += v1.z; }
void rhw(){ x/=w ; y/=w ; z/=w ; w=1; }//歸一化
float len(){ return sqrt(x * x + y * y + z * z) ;}
float squardlen(){return (x * x + y * y + z * z) ;}
//-------------------------------------------------------
//叉積
//-------------------------------------------------------
XVector XVector::cp(XVector& v1)
{
float nx = y * v1.z - v1.y * z;
float ny = z * v1.x - v1.z * x;
float nz = x * v1.y - v1.x * y;
return XVector(nx,ny,nz,1);
}
//-------------------------------------------------------
//向量單位化
//-------------------------------------------------------
void XVector::normalize()
{
float len = (float) sqrt(x*x + y*y + z*z);
if(len == 0)
return ;
x/=len;
y/=len;
z/=len;
w = 1;
};
void XVector::normalize(XVector& out)
{
float len = (float) sqrt(x*x + y*y + z*z);
if(len == 0)
return ;
out.x/=len;
out.y/=len;
out.z/=len;
out.w = 1;
};
public:
//-------------------------------------------------------
//以下為重載的運算
//讓向量能和其它數字一樣運算.
//-------------------------------------------------------
void operator *=(float s){x*=s;y*=s;z*=s;};
bool operator ==(XVector& v) {return (v.x == x && v.y == y && v.z == z && v.w == w) ; }
XVector XVector::operator -(XVector& v1)
{
return XVector(x - v1.x , y-v1.y ,z - v1.z, 1 );
}
XVector XVector::operator -()
{
return XVector(-x , -y ,- z, w );
}
XVector XVector::operator +(XVector& v1)
{
return XVector(x + v1.x , y + v1.y ,z + v1.z, 1 );
}
XVector XVector::operator *(float s)
{
return XVector(x * s,y * s,z * s , 1);
}
float XVector::operator *(XVector& v1)
{
return (v1.x * x + v1.y * y + v1.z * z) ;
}
//-------------------------------------------------------
//Cross product
//-------------------------------------------------------
XVector XVector::operator ^(XVector& v1)
{
float nx = y * v1.z - v1.y * z;
float ny = z * v1.x - v1.z * x;
float nz = x * v1.y - v1.x * y;
return XVector(nx,ny,nz,1);
}
//-------------------------------------------------------
//求夾角
//-------------------------------------------------------
float XVector::operator |(XVector& v1)
{
float t = dp3(v1)/(fabs() * v1.fabs());
return (float)XM_ACosD(t);
}
//-------------------------------------------------------
//是否平行
//-------------------------------------------------------
bool XVector::operator||(XVector& v1)
{
float nx = y * v1.z - v1.y * z;
float ny = z * v1.x - v1.z * x;
float nz = x * v1.y - v1.x * y;
if(nx == 0 && ny == 0 && nz == 0)
return true;
else
return false;
}
//-------------------------------------------------------
//向量夾角
//-------------------------------------------------------
float XVector::Clamp(XVector& v1)
{
float t = dp3(v1)/(fabs() * v1.fabs());
return (float)XM_ACosD(t);
}
public://We set this data public is for easy access
union
{
struct
{
float x;
float y;
float z;
float w;//Four component the w is useful ......
};
//for texture coordinate
struct
{
float s,t,r,q;
};
float v[4];
};
};
typedef XVector XVector4D;
#endif
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -