一个3D向量类
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->//Vertex3D.h:interfacefortheVertex3Dclass.
//
//////////////////////////////////////////////////////////////////////
classVertex3D
{//3维向量类
private:
doublex,y,z;
public:
Vertex3D();
Vertex3D(doubletx,doublety,doubletz);
Vertex3D(constVertex3D&vt);//拷贝构造函数
Vertex3D&operator=(constVertex3D&vt);//重载赋值运算符
booloperator==(constVertex3D&vt)const;//重载"=="运算符
booloperator!=(constVertex3D&vt)const;//重载"!="运算符
Vertex3Doperator-()const;//负向量
Vertex3Doperator-(constVertex3D&vt)const;//向量减法
Vertex3Doperator+(constVertex3D&vt)const;//向量加法
Vertex3Doperator*(doublefac)const;//与标量相乘
Vertex3Doperator/(doublefac)const;//与标量相除
Vertex3D&operator+=(constVertex3D&vt);
Vertex3D&operator-=(constVertex3D&vt);
Vertex3D&operator*=(doublefac);//与标量相乘
Vertex3D&operator/=(doublefac);//与标量相除
doubleoperator*(constVertex3D&vt)const;//向量点乘
voidreset();
voidnormalize();//向量单位化
doubleVertexLength()const;//向量模
virtual~Vertex3D();
friendVertex3DcrossProduct(constVertex3D&a,constVertex3D&b);
frienddoubleDistance(constVertex3D&a,constVertex3D&b);
};
//
//////////////////////////////////////////////////////////////////////
classVertex3D
{//3维向量类
private:
doublex,y,z;
public:
Vertex3D();
Vertex3D(doubletx,doublety,doubletz);
Vertex3D(constVertex3D&vt);//拷贝构造函数
Vertex3D&operator=(constVertex3D&vt);//重载赋值运算符
booloperator==(constVertex3D&vt)const;//重载"=="运算符
booloperator!=(constVertex3D&vt)const;//重载"!="运算符
Vertex3Doperator-()const;//负向量
Vertex3Doperator-(constVertex3D&vt)const;//向量减法
Vertex3Doperator+(constVertex3D&vt)const;//向量加法
Vertex3Doperator*(doublefac)const;//与标量相乘
Vertex3Doperator/(doublefac)const;//与标量相除
Vertex3D&operator+=(constVertex3D&vt);
Vertex3D&operator-=(constVertex3D&vt);
Vertex3D&operator*=(doublefac);//与标量相乘
Vertex3D&operator/=(doublefac);//与标量相除
doubleoperator*(constVertex3D&vt)const;//向量点乘
voidreset();
voidnormalize();//向量单位化
doubleVertexLength()const;//向量模
virtual~Vertex3D();
friendVertex3DcrossProduct(constVertex3D&a,constVertex3D&b);
frienddoubleDistance(constVertex3D&a,constVertex3D&b);
};
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->//Vertex3D.cpp:implementationoftheVertex3Dclass.
//
//////////////////////////////////////////////////////////////////////
#include"Vertex3D.h"
#include<math.h>
//////////////////////////////////////////////////////////////////////
//Construction/Destruction
//////////////////////////////////////////////////////////////////////
Vertex3D::Vertex3D()
{
this->x=0.0f;
this->y=0.0f;
this->z=0.0f;
}
Vertex3D::Vertex3D(doubletx,doublety,doubletz)
{
this->x=tx;
this->y=ty;
this->z=tz;
}
Vertex3D::Vertex3D(constVertex3D&vt)
{
this->x=vt.x;
this->y=vt.y;
this->z=vt.z;
}
Vertex3D&Vertex3D::operator=(constVertex3D&vt)
{
this->x=vt.x;
this->y=vt.y;
this->z=vt.z;
return*this;
}
boolVertex3D::operator==(constVertex3D&vt)const
{//判断向量是否相等
returnthis->x==vt.x&&this->y==vt.y&&this->z==vt.z;
}
boolVertex3D::operator!=(constVertex3D&vt)const
{
returnthis->x!=vt.x&&this->y!=vt.y&&this->z!=vt.z;
}
Vertex3DVertex3D::operator-()const
{//负向量
returnVertex3D(-x,-y,-z);
}
Vertex3DVertex3D::operator-(constVertex3D&vt)const
{//向量减法
returnVertex3D(x-vt.x,y-vt.y,z-vt.z);
}
Vertex3DVertex3D::operator+(constVertex3D&vt)const
{//向量加法
returnVertex3D(x+vt.x,y+vt.y,z+vt.z);
}
Vertex3DVertex3D::operator*(doublefac)const
{//与标量乘法
returnVertex3D(x*fac,y*fac,z*fac);
}
Vertex3DVertex3D::operator/(doublefac)const
{//与标量相除
returnVertex3D(x/fac,y/fac,z/fac);
}
Vertex3D&Vertex3D::operator+=(constVertex3D&vt)
{
this->x+=vt.x;
this->y+=vt.y;
this->z+=vt.z;
return*this;
}
Vertex3D&Vertex3D::operator-=(constVertex3D&vt)
{
this->x-=vt.x;
this->y-=vt.y;
this->z-=vt.z;
return*this;
}
Vertex3D&Vertex3D::operator*=(doublefac)
{
this->x*=fac;
this->y*=fac;
this->z*=fac;
return*this;
}
Vertex3D&Vertex3D::operator/=(doublefac)
{
this->x/=fac;
this->y/=fac;
this->z/=fac;
return*this;
}
voidVertex3D::reset()
{//置为零向量
this->x=0.0f;
this->y=0.0f;
this->z=0.0f;
}
doubleVertex3D::VertexLength()const
{//向量长度
doubletmp=x*x+y*y+z*z;
returnsqrt(tmp);
}
voidVertex3D::normalize()
{//向量单位化
doublelen=this->VertexLength();//获取向量长度
if(len>0.0f)
{
doubletmp=1.0f/len;
this->x*=tmp;
this->y*=tmp;
this->z*=tmp;
}
}
doubleVertex3D::operator*(constVertex3D&vt)const
{//向量点乘
returnx*vt.x+y*vt.y+z*vt.z;
}
Vertex3D::~Vertex3D()
{
}
Vertex3DcrossProduct(constVertex3D&a,constVertex3D&b)
{//向量叉乘
returnVertex3D(a.y*b.z-a.z*b.y,a.z*b.x-a.x*b.z,a.x*b.y-a.y*b.x);
}
doubleDistance(constVertex3D&a,constVertex3D&b)
{//向量距离
doubledx=a.x-b.x,dy=a.y-b.y,dz=a.z-b.z;
returnsqrt(dx*dx+dy*dy+dz*dz);
}
//
//////////////////////////////////////////////////////////////////////
#include"Vertex3D.h"
#include<math.h>
//////////////////////////////////////////////////////////////////////
//Construction/Destruction
//////////////////////////////////////////////////////////////////////
Vertex3D::Vertex3D()
{
this->x=0.0f;
this->y=0.0f;
this->z=0.0f;
}
Vertex3D::Vertex3D(doubletx,doublety,doubletz)
{
this->x=tx;
this->y=ty;
this->z=tz;
}
Vertex3D::Vertex3D(constVertex3D&vt)
{
this->x=vt.x;
this->y=vt.y;
this->z=vt.z;
}
Vertex3D&Vertex3D::operator=(constVertex3D&vt)
{
this->x=vt.x;
this->y=vt.y;
this->z=vt.z;
return*this;
}
boolVertex3D::operator==(constVertex3D&vt)const
{//判断向量是否相等
returnthis->x==vt.x&&this->y==vt.y&&this->z==vt.z;
}
boolVertex3D::operator!=(constVertex3D&vt)const
{
returnthis->x!=vt.x&&this->y!=vt.y&&this->z!=vt.z;
}
Vertex3DVertex3D::operator-()const
{//负向量
returnVertex3D(-x,-y,-z);
}
Vertex3DVertex3D::operator-(constVertex3D&vt)const
{//向量减法
returnVertex3D(x-vt.x,y-vt.y,z-vt.z);
}
Vertex3DVertex3D::operator+(constVertex3D&vt)const
{//向量加法
returnVertex3D(x+vt.x,y+vt.y,z+vt.z);
}
Vertex3DVertex3D::operator*(doublefac)const
{//与标量乘法
returnVertex3D(x*fac,y*fac,z*fac);
}
Vertex3DVertex3D::operator/(doublefac)const
{//与标量相除
returnVertex3D(x/fac,y/fac,z/fac);
}
Vertex3D&Vertex3D::operator+=(constVertex3D&vt)
{
this->x+=vt.x;
this->y+=vt.y;
this->z+=vt.z;
return*this;
}
Vertex3D&Vertex3D::operator-=(constVertex3D&vt)
{
this->x-=vt.x;
this->y-=vt.y;
this->z-=vt.z;
return*this;
}
Vertex3D&Vertex3D::operator*=(doublefac)
{
this->x*=fac;
this->y*=fac;
this->z*=fac;
return*this;
}
Vertex3D&Vertex3D::operator/=(doublefac)
{
this->x/=fac;
this->y/=fac;
this->z/=fac;
return*this;
}
voidVertex3D::reset()
{//置为零向量
this->x=0.0f;
this->y=0.0f;
this->z=0.0f;
}
doubleVertex3D::VertexLength()const
{//向量长度
doubletmp=x*x+y*y+z*z;
returnsqrt(tmp);
}
voidVertex3D::normalize()
{//向量单位化
doublelen=this->VertexLength();//获取向量长度
if(len>0.0f)
{
doubletmp=1.0f/len;
this->x*=tmp;
this->y*=tmp;
this->z*=tmp;
}
}
doubleVertex3D::operator*(constVertex3D&vt)const
{//向量点乘
returnx*vt.x+y*vt.y+z*vt.z;
}
Vertex3D::~Vertex3D()
{
}
Vertex3DcrossProduct(constVertex3D&a,constVertex3D&b)
{//向量叉乘
returnVertex3D(a.y*b.z-a.z*b.y,a.z*b.x-a.x*b.z,a.x*b.y-a.y*b.x);
}
doubleDistance(constVertex3D&a,constVertex3D&b)
{//向量距离
doubledx=a.x-b.x,dy=a.y-b.y,dz=a.z-b.z;
returnsqrt(dx*dx+dy*dy+dz*dz);
}
本文介绍了一个3D向量类的设计与实现细节,包括基本运算如加法、减法、标量乘法及向量点乘等,并提供了向量单位化、长度计算等实用功能。
119

被折叠的 条评论
为什么被折叠?



