一个3D向量类

提供以下基本操作:

1.   存取向量的各分量(x, y , z)
2.   向量间的赋值操作
3.   比较两向量是否相同
4.   将向量置为零向量
5.   向量求负
6.   求向量的模
7.   向量与标量的乘除法
8.   向量标准化
9.   向量加减法
10. 计算两点(点用向量表示)间距离
11. 向量点乘
12. 向量叉乘

该向量的操作运算对3D点同样适合。

    #include <math.h>
    
    
class
 cVector3
    {
    
public
:
        
float
 x, y, z;
    
    
public
:
        cVector3() { }
    
        cVector3(
const
 cVector3& v)
        {    
            x = v.x;    y = v.y;    z = v.z;    
        }
    
        cVector3(
float vx, float vy, float
 vz)
        {
            x = vx;        y = vy;        z = vz;
        }
    
        cVector3& 
operator=(const
 cVector3& v)
        {
            x = v.x;    y = v.y;    z = v.z;
    
            
return *this
;
        }
    
        
bool operator==(const
 cVector3& v)
        {
            
return
 (x == v.x && y == v.y && z == v.z);
        }
    
        
bool operator!=(const
 cVector3& v)
        {
            
return
 (x != v.x || y != v.y || z != v.z);
        }
    
        
void
 zero()
        {
            
// set the vector to zero
    
        x = y = z = 0.0f;
        }
    
        cVector3 
operator
-()
        {
            
// unary minus returns the negative of the vector
    
        return
 cVector3(-x, -y, -z);
        }
    
        cVector3 
operator+(const
 cVector3& v)
        {
            
return
 cVector3(x + v.x, y + v.y, z + v.z);
        }
    
        cVector3 
operator-(const
 cVector3& v)
        {
            
return
 cVector3(x - v.x, y - v.y, z - v.z);
        }
    
        cVector3 
operator*(float
 scale)
        {
            
// multiplication and division by scalar
    
        return
 cVector3(x * scale, y * scale, z * scale);
        }
    
        cVector3 
operator/(float
 scale)
        {
            
float temp = 1.0f / scale;    
// NOTE: no check for divide by zero here
    

            
return  cVector3(x * temp, y * temp, z * temp);
        }
    
        cVector3& 
operator+=(const
 cVector3& v)
        {
            x += v.x;    y += v.y;    z += v.z;
    
            
return *this
;
        }
    
        cVector3& 
operator-=(const
 cVector3& v)
        {
            x -= v.x;    y -= v.y;    z -= v.z;
    
            
return *this
;
        }
    
        cVector3& 
operator*=(float
 scale)
        {
            x *= scale;    y *= scale;    z *= scale;
    
            
return *this
;
        }
    
        cVector3& 
operator/=(float
 scale)
        {
            
float
 temp = 1.0f / scale;
    
            x *= temp;    y *= temp;    z *= temp;
    
            
return *this
;
        }
    
        
void
 normalize()
        {
            
// normalize the vector
    

            
float  mag = x * x + y * y + z * z;
    
            
if(mag > 0.0f)    
// check for divide-by-zero
    
        {
                
float
 one_over_mag = 1.0f / sqrt(mag);
    
                x *= one_over_mag;
                y *= one_over_mag;
                z *= one_over_mag;
            }
        }
    
        
float operator*(const
 cVector3& v)
        {
            
// vector dot product, we overload the standard multiplication symbol to do this.
    

            
return  (x * v.x + y * v.y + z * v.z);
        }
    };
    
    
    /***************************************************************************************************/

    
    inline 
float vector_mag(const  cVector3& v)
    {
        
// compute the magnitude of a vector
    
    return
 sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
    }
    
    inline cVector3 cross_product(
const cVector3& a, const
 cVector3& b)
    {
        
// compute the cross product of two vectors
    
    return
 cVector3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
    }
    
    inline cVector3 
operator*(float k, const
 cVector3& v)
    {
        
// scalar on the left multiplication, for symmetry.
    
    return
 cVector3(k * v.x, k * v.y, k * v.z);
    }
    
    inline 
float distance(const cVector3& a, const
 cVector3& b)
    {
        
// compute the distance between two points
    

        
float  dx = a.x - b.x;
        
float
 dy = a.y - b.y;
        
float
 dz = a.z - b.z;
    
        
return
 sqrt(dx * dx + dy * dy + dz * dz);
    }
    
    inline 
float distance_squared(const cVector3& a, const
 cVector3& b)
    {
        
// compute the distance between two points
    

        
float  dx = a.x - b.x;
        
float
 dy = a.y - b.y;
        
float
 dz = a.z - b.z;
    
        
return
 (dx * dx + dy * dy + dz * dz);
    }
    
    
extern const cVector3 g_zero_vector;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值