?? xvector3d.inl
字號:
#ifndef __XVECTOR_3D_INLINE_INCLUDE__
#define __XVECTOR_3D_INLINE_INCLUDE__
#ifndef IN_MATHLIB_NAMESPACE
#error You cann't include this file out the XMathLib namespace
#endif
//---------------------------------------------------------------------------------------------
//三維向量
//---------------------------------------------------------------------------------------------
class _MATH_LIB_EXPORT_ XVector3D
{
public:
XVector3D(){};
XVector3D(XVector2D& v){x = v.x ; y = v.y ;z = 0 ;}
XVector3D(float _x,float _y ,float _z) {x = _x ; y = _y; z = _z;}
void add(XVector3D& rv){x += rv.x; y += rv.y; z += rv.z;}
void sub(XVector3D& rv){x -= rv.x; y -= rv.y; z -= rv.z;}
void mul(float s){x *= s; y *= s; z *= s;}
void normalize(){float len = sqrt(x * x + y * y + z * z) ; x/=len;y/=len; z/=len;}
void normalize(XVector3D &vout){float len = sqrt(x * x + y * y + z * z) ; vout.x = x/len; vout.y = y/len; vout.z = z/ len ;};
XVector3D operator +(XVector3D& v){ return XVector3D(v.x + x, v.y + y,v.z + z);};
XVector3D operator -(XVector3D& v){ return XVector3D(x - v.x, y - v.y,z - v.z);};
XVector3D operator *(float s){ return XVector3D(x * s, y * s ,z * s);};
bool operator ==(XVector3D& v) {return (v.x == x && v.y == y && v.z == z) ; }
//-----------------------------------------------------
//the lenght of a vector
//-----------------------------------------------------
float fabs(){ return sqrt(x * x + y * y + z * z) ;}
float len(){ return sqrt(x * x + y * y + z * z) ;}
float squardlen(){return (x * x + y * y + z * z) ;}
//-----------------------------------------------------
//dot product
//-----------------------------------------------------
float dp(XVector3D& v){return (v.x*x + v.y *y + v.z * z ); };
//-----------------------------------------------------
//cross product
//-----------------------------------------------------
void cp(XVector3D& v,XVector3D& vOut)
{
vOut.x = y * v.z - v.y * z;
vOut.y = z * v.x - v.z * x;
vOut.z = x * v.y - v.x * y;
}
//-----------------------------------------------------
//cross product
//-----------------------------------------------------
XVector3D cp(XVector3D& v)
{
float nx = y * v.z - v.y * z;
float ny = z * v.x - v.z * x;
float nz = x * v.y - v.x * y;
return XVector3D(nx,ny,nz);
}
XVector3D& operator +=(XVector3D& v){ x += v.x ; y += v.y ; return *this; }
//-----------------------------------------------------
//
//-----------------------------------------------------
XVector3D& operator -=(XVector3D& v){ x -= v.x ; y -= v.y ; return *this; }
//-----------------------------------------------------
//
//-----------------------------------------------------
XVector3D& operator *=(float s){ x *= s ; y *= s; z *= s; return *this; };
//-----------------------------------------------------
//
//-----------------------------------------------------
XVector3D operator ^(XVector3D& v){ return cp(v); }
//-----------------------------------------------------
//向量的夾角
//-----------------------------------------------------
float XVector3D::operator |(XVector3D& v1)
{
float t = dp(v1)/(fabs() * v1.fabs());
return (float)XM_ACosD(t);
}
//-----------------------------------------------------
//是否平行
//-----------------------------------------------------
bool XVector3D::operator||(XVector3D& v1)
{
float x = y * v1.z - v1.y * z;
float y = z * v1.x - v1.z * x;
float z = x * v1.y - v1.x * y;
if(x == 0 && y == 0 && z == 0)
return true;
else
return false;
}
//-----------------------------------------------------
//向量的夾角
//-----------------------------------------------------
float XVector3D::clamp(XVector3D& v1)
{
float t = dp(v1)/(fabs() * v1.fabs());
return (float)XM_ACosD(t);
}
//-----------------------------------------------------
//向量是不是為0
//-----------------------------------------------------
bool isZero(){return (fabs()<0.000000001f);}
public:
union
{
struct
{
float x,y,z;
};
};
float v[3];
};
#endif
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -