⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xvector3d.inl

📁 XMathLib是一个通用的3D图形数学库。 其中包含两个部分: XMathLib和XGeomLib。分别处理数学和几何运算。 数学部分包含向量、矩阵、四元数的运算。以及其它的运算。 几何部分
💻 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 + -