C++ Exercises(一)

一个3D向量类

//  Vertex3D.h: interface for the Vertex3D class.
//
/ /
class  Vertex3D  
{
// 3维向量类
private :
    
double  x,y,z;
public :
    Vertex3D();
    Vertex3D(
double  tx, double  ty, double  tz);
    Vertex3D(
const  Vertex3D &  vt); // 拷贝构造函数

    Vertex3D
&   operator   =  ( const  Vertex3D  & vt); // 重载赋值运算符

    
bool   operator   ==  ( const  Vertex3D  & vt) const ; // 重载"=="运算符
     bool   operator   !=  ( const  Vertex3D  & vt) const ; // 重载"!="运算符

    Vertex3D 
operator   - () const ; // 负向量
    Vertex3D  operator   -  ( const  Vertex3D  & vt) const ; // 向量减法
    Vertex3D  operator   +  ( const  Vertex3D  & vt) const ; // 向量加法
    Vertex3D  operator   *  ( double  fac) const ; // 与标量相乘
    Vertex3D  operator   /  ( double  fac) const ; // 与标量相除
    Vertex3D &   operator   +=  ( const  Vertex3D  & vt);
    Vertex3D
&   operator   -=  ( const  Vertex3D  & vt);
    Vertex3D
&   operator   *=  ( double  fac); // 与标量相乘
    Vertex3D &   operator   /=  ( double  fac); // 与标量相除
     double   operator   *  ( const  Vertex3D  & vt) const ; // 向量点乘


    
void  reset();
    
void  normalize(); // 向量单位化
     double  VertexLength() const ; // 向量模
     virtual   ~ Vertex3D();

    friend Vertex3D crossProduct(
const  Vertex3D  & a, const  Vertex3D  & b);
    friend 
double  Distance( const  Vertex3D  & a, const  Vertex3D  & b);
};


//  Vertex3D.cpp: implementation of the Vertex3D class.
//
/ /

#include 
" Vertex3D.h "
#include 
< math.h >

/ /
//  Construction/Destruction
/ /

Vertex3D::Vertex3D()
{
    
this -> =   0.0f ;
    
this -> =   0.0f ;
    
this -> =   0.0f ;
}

Vertex3D::Vertex3D(
double  tx, double  ty, double  tz)
{
    
this -> =  tx;
    
this -> =  ty;
    
this -> =  tz;
}
Vertex3D::Vertex3D(
const  Vertex3D  & vt)
{
    
this -> =  vt.x;
    
this -> =  vt.y;
    
this -> =  vt.z;
}

Vertex3D
&  Vertex3D:: operator   =  ( const  Vertex3D &  vt)
{
    
this -> =  vt.x;
    
this -> =  vt.y;
    
this -> =  vt.z;
    
return   * this ;
}

bool  Vertex3D:: operator   ==  ( const  Vertex3D &  vt) const
{
// 判断向量是否相等
     return   this -> x == vt.x && this -> y == vt.y && this -> z == vt.z;
}

bool  Vertex3D:: operator   !=  ( const  Vertex3D &  vt) const
{
    
return   this -> x != vt.x && this -> y != vt.y && this -> z != vt.z;
}

Vertex3D Vertex3D::
operator   -  () const
{
// 负向量
     return  Vertex3D( - x, - y, - z);
}

Vertex3D Vertex3D::
operator   -  ( const  Vertex3D &  vt) const
{
// 向量减法
     return  Vertex3D(x - vt.x,y - vt.y,z - vt.z);
}

Vertex3D Vertex3D::
operator   +  ( const  Vertex3D &  vt) const
{
// 向量加法
     return  Vertex3D(x + vt.x,y + vt.y,z + vt.z);
}

Vertex3D Vertex3D::
operator   *  ( double  fac) const
{
// 与标量乘法
     return  Vertex3D(x * fac,y * fac,z * fac);
}

Vertex3D Vertex3D::
operator   /  ( double  fac) const
{
// 与标量相除
     return  Vertex3D(x / fac,y / fac,z / fac);
}

Vertex3D
&  Vertex3D:: operator   +=  ( const  Vertex3D  & vt)
{
    
this -> +=  vt.x;
    
this -> +=  vt.y;
    
this -> +=  vt.z;
    
return   * this ;
}

Vertex3D
&  Vertex3D:: operator   -=  ( const  Vertex3D  & vt)
{
    
this -> -=  vt.x;
    
this -> -=  vt.y;
    
this -> -=  vt.z;
    
return   * this ;
}

Vertex3D
&  Vertex3D:: operator   *=  ( double  fac)
{
    
this -> *=  fac;
    
this -> *=  fac;
    
this -> *=  fac;
    
return   * this ;
}

Vertex3D
&  Vertex3D:: operator   /=  ( double  fac)
{
    
this -> /=  fac;
    
this -> /=  fac;
    
this -> /=  fac;
    
return   * this ;
}

void  Vertex3D::reset()
{
// 置为零向量 
     this -> =   0.0f ;
    
this -> =   0.0f ;
    
this -> =   0.0f ;
}

double  Vertex3D::VertexLength() const
{
// 向量长度
     double  tmp  =  x * x + y * y + z * z;
    
return  sqrt(tmp);
}

void  Vertex3D::normalize()
{
// 向量单位化
     double  len  =   this -> VertexLength(); // 获取向量长度
     if (len > 0.0f )
    {
        
double  tmp  =   1.0f / len;
        
this -> *=  tmp;
        
this -> *=  tmp;
        
this -> *=  tmp;
    }
}

double  Vertex3D:: operator   *  ( const  Vertex3D  & vt) const
{
// 向量点乘
     return  x * vt.x + y * vt.y + z * vt.z;
}


Vertex3D::
~ Vertex3D()
{

}

Vertex3D crossProduct(
const  Vertex3D  & a, const  Vertex3D  & b)
{
// 向量叉乘
     return  Vertex3D(a.y * b.z  -  a.z * b.y,a.z * b.x  -  a.x * b.z,a.x * b.y  -  a.y * b.x);
}

double  Distance( const  Vertex3D  & a, const  Vertex3D  & b)
{
// 向量距离
     double  dx  =  a.x  -  b.x,dy  =  a.y  -  b.y,dz  =  a.z  -  b.z;
    
return  sqrt(dx * dx + dy * dy + dz * dz);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值