您的位置:首页 > 编程语言 > C语言/C++

Vector3——简单的3D向量类

2015-05-08 11:32 288 查看
参考资料:1、 [美] 邓恩(Dunn F.)著. 3D数学基础——图形设计与开发. 史银雪,陈洪,王荣静 译 清华大学出版社 p57-65

2、http://www.2cto.com/kf/201311/260139.html

编程环境 QT4.8.4 + VS2010

本文用 C++实现一个简单的Vector3类的功能,已经实现的功能有:

1、重载赋值运算符“=”

2、重载“==”和“!=”操作符

3、置为零向量

4、重载一元运算符“-”

5、重载二元预算法“+”“-”

6、标量的乘除法

7、重载自反运算符

8、向量单位化

9、向量的数量积,又叫:点乘

10、向量的向量积,又加:叉乘

11、计算两点间的距离

12、打印向量

程序清单

1、vector3.h

#ifndef VECTOR3_H
#define VECTOR3_H

class Vector3
{
public:
float x, y, z;
//构造函数
//默认构造函数,初始一个零向量
Vector3();
//复制构造函数
Vector3(const Vector3 &a);
//带参数的构造函数,用三个值完成初始化
Vector3(float nx, float ny, float nz);
//析构函数
~Vector3();

//标准对象操作
//重载赋值运算符,并返回引用,以实现左值。
Vector3& operator=(const Vector3 &a);

//重载"=="操作符
bool operator==(const Vector3 &a) const ;
//重载"!="操作符
bool operator!=(const Vector3 &a) const ;

//向量运算

//置为零向量
void Zero();
//重载一元"-"运算符
Vector3 operator-()const;

//重载二元"+"和"-"运算符
Vector3 operator+(const Vector3 &a) const;
Vector3 operator-(const Vector3 &a) const;
//标量的乘、除法
Vector3 operator*(float a) const;
Vector3 operator/(float a) const;

//重载自反运算符
Vector3& operator+=(const Vector3 &a);
Vector3& operator-=(const Vector3 &a);
Vector3& operator*=(float a);
Vector3& operator/=(float a);

//向量标准化
void Normalize();

//向量点乘,重载标准的乘法运算符
float operator*(const Vector3 &a) const;

//求向量模
float VectorMag(const Vector3 &a);
//计算两向量的叉乘
Vector3 CrossProduct(const Vector3 &a,const Vector3 &b);
//计算两点间的距离
float Distance (const Vector3 &a,const Vector3 &b);
//打印向量
void Vector3::PrintVector3();

};

#endif // VECTOR3_H


2、vector3.cpp

#include <iostream>
#include <qmath.h>
#include "vector3.h"

//默认构造函数,初始一个零向量
Vector3::Vector3(){
x=0;
y=0;
z=0;
}
//复制构造函数
Vector3::Vector3(const Vector3 &a){
x=a.x;
y=a.y;
z=a.z;
}
//带参数的构造函数,用三个值完成初始化
Vector3::Vector3(float nx, float ny, float nz){
x=nx;
y=ny;
z=nz;
}
//析构函数
Vector3::~Vector3(){};

//标准对象操作
//重载赋值运算符,并返回引用,以实现左值。
Vector3& Vector3::operator=(const Vector3 &a){
x =a.x;
y =a.y;
z =a.z;
return *this;
}

//重载"=="操作符
bool Vector3::operator==(const Vector3 &a) const{
return x==a.x && y==a.y && z==a.z;
}

//重载"!="操作符
bool Vector3::operator!=(const Vector3 &a) const{
return x!=a.x || y!=a.y || z!=a.z;
}

//置为零向量
void Vector3::Zero(){
x = y = z = 0.0f;
}
//重载一元"-"运算符
Vector3 Vector3::operator-()const
{
return Vector3(-x,-y,-z);
}

//重载二元"+"和"-"运算符
Vector3 Vector3::operator+(const Vector3 &a) const{

return Vector3(x+a.x,y+a.y,z+a.z);
}
Vector3 Vector3::operator-(const Vector3 &a) const{
return Vector3(x-a.x,y-a.y,z-a.z);
}

//标量的乘、除法
Vector3 Vector3::operator*(float a) const{
return Vector3(x*a,y*a,z*a);

}
Vector3 Vector3::operator/(float a) const{
float oneOverA = 1.0f /a;//注意这里不对"除零"进行处理
return Vector3(x*oneOverA,y*oneOverA,z*oneOverA);
}

//重载自反运算符
Vector3& Vector3::operator +=(const Vector3 &a){

x+=a.x;
y+=a.y;
z+=a.z;
return *this;
}

Vector3& Vector3::operator -=(const Vector3 &a){
x-=a.x;
y-=a.y;
z-=a.z;
return *this;
}
Vector3& Vector3::operator *=(float a){
x *= a;
y *= a;
z *= a;
return *this;
}
Vector3& Vector3::operator /=(float a){
float oneOverA =1.0f/a;
x *= oneOverA;
y *= oneOverA;
z *= oneOverA;
return *this;
}

//向量标准化
void Vector3::Normalize(){
float magSq = x*x + y*y + z*z;
if (magSq >0.0f){//检查除零
float oneOverMag = 1.0f / sqrt(magSq);
x *= oneOverMag;
y *= oneOverMag;
z *= oneOverMag;
}
}

//向量点乘,重载标准的乘法运算符
float Vector3::operator *(const Vector3 &a) const{
return x*a.x + y*a.y +z *a.z;
}
void Vector3::PrintVector3(){
std::cout <<"("<<x<<","<<y<<","<<z<<")"<<std::endl;

}
//求向量模
float Vector3::VectorMag(const Vector3 &a){
return sqrt(a.x * a.x + a.y * a.y + a.z * a.z);
}

/*
Cross Product叉乘公式
aXb = | i   j   k  |
| a.x a.y a.z|
| b.x b.y b.z| = (a.y*b.z -a.z*b.y)i + (a.z*b.x - a.x*b.z)j + (a.x+b.y - a.y*b.x)k
*/

//计算两向量的叉乘
Vector3 Vector3::CrossProduct(const Vector3 &a,const Vector3 &b){
return Vector3(
a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x
);
}

//计算两点间的距离
float Vector3::Distance (const Vector3 &a,const Vector3 &b){

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);
}


3、测试文件 main.cpp

#include <QtCore/QCoreApplication>
#include "vector3.h"
#include <iostream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//测试构造函数
Vector3 v1;
Vector3 v2(3.0f,4.0f,5.0f);
Vector3 v3(v2);

//测试赋值运算符
Vector3 v4 =v3;
//打印
v1.PrintVector3();
v2.PrintVector3();
v3.PrintVector3();
v4.PrintVector3();
//测试布尔运算 ==
if (v1 ==v3)
{
std::cout<<"v1=v3"<<std::endl;
}
else
{
std::cout<<"v1!=v3"<<std::endl;
}
if (v2 == v3)
{
std::cout<<"v2=v3"<<std::endl;
}
else
{
std::cout<<"v2!=v3"<<std::endl;
}
//测试布尔运算 !=
if (v1 !=v3)
{
std::cout<<"v1!=v3"<<std::endl;
}
else
{
std::cout<<"v1=v3"<<std::endl;
}
if (v2 != v3)
{
std::cout<<"v2!=v3"<<std::endl;
}
else
{
std::cout<<"v2=v3"<<std::endl;
}

//测试置为零向量
v2.Zero();
v1.PrintVector3();
v2.PrintVector3();
v3.PrintVector3();
v4.PrintVector3();
//测试一元"-"运算符
Vector3 v5=-v3;
v5.PrintVector3();

//测试二元预算符
Vector3 v_x(2.0f,3.0f,4.0f);
Vector3 v_y(3.0f,3.0f,4.0f);
Vector3 v_add = v_x+v_y;
Vector3 v_sub = v_x-v_y;
v_add.PrintVector3();
v_sub.PrintVector3();

//求模
float mag = v2.VectorMag(v2);
std::cout <<mag<<std::endl;

//单位化
v2.Normalize();
v2.PrintVector3();
return a.exec();
}


二、运行结果



说明:本程序是在QT4.8.4 +VS2010中实现,如果转到其他编程环境,请自行修改。

源程序下载:http://download.csdn.net/detail/gongchao1212/8675321
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ vector3