第六章:3D向量类

第一节:类接口的设计

1.好的类在设计之前首先要回答下列问题:“这些类将包含哪些数据?”,“这个类将提供什么样的操作?”,“在哪些数据上执行操作?”。

  我们已经知道我们要设计的是3D向量类,用来存储x,y,z分量的。这个类将包含前面我们所描述的对向量的所有操作。

  • 存取向量的各个分量(x,y,z)(成员变量)
  • 向量间的赋值操作(有参构造函数,拷贝构造函数,赋值运算符)
  • 比较两个向量是否相等
  • 向量的取反操作
  • 两个向量相加,相减
  • 向量与标量相乘,相除
  • 向量的点乘
  • 向量重置为零向量
  • 向量的标准化
  • 向量的模
  • 向量的叉乘
  • 两个点之间的距离

第二节:Vector3类

1.Vector3类的头文件设计

#pragma once
//########################################################
//
// Vector3类 -- 简单的3D向量类
//
//########################################################
#include <math.h>
class Vector3 { public: // 3D向量的三个分量 float x, y, z; public: // 无参构造函数 Vector3(); // 有参构造函数 Vector3(float x, float y, float z); // 拷贝构造函数 Vector3(const Vector3 & vector3); public: // 重载赋值运算符 Vector3& operator=(const Vector3& vector3); // 重载 "==" 操作符(判断向量是否相等) bool operator==(const Vector3& vector3); // 重载 "!=" 操作符(判断向量是否不相等) bool operator!=(const Vector3& vector3); // 重载一元 "-" 操作符(向量取反) Vector3& operator-(); // 重载二元 "+" 操作符(两个向量的相加) Vector3& operator+(const Vector3& vector3); // 重载二元 "-" 操作符(两个向量的相减) Vector3& operator-(const Vector3& vector3); // 重载二元 "*" 操作符(标量与向量相乘) Vector3& operator*(const float k); // 重载二元 "/" 操作符(标量与向量相除) Vector3& operator/(const float k); // 重载自反运算符 Vector3& operator+=(const Vector3& vector3); Vector3& operator-=(const Vector3& vector3); Vector3& operator*=(const float k); Vector3& operator/=(const float k); // 重载二元 "*" 操作符(两个向量的点乘) float operator*(const Vector3& vector3); public: // 重置向量为零向量 void zero(); // 向量的模 float mag(); // 向量的标准化 void normalize(); // 向量的叉乘 Vector3 crossProduct(const Vector3& vector3); // 计算两点的距离 float distance(const Vector3& vector3); };

2.Vector3类的代码实现

#include "Vector3.h"

// 无参构造
Vector3::Vector3()
{
}
// 带参构造
Vector3::Vector3(float x, float y, float z)
{
    this->x = x;
    this->y = y;
    this->z = z;
}
// 拷贝构造函数
Vector3::Vector3(const Vector3& vector3)
{
    this->x = vector3.x;
    this->y = vector3.y;
    this->z = vector3.z;
}
// 重载赋值操作符
Vector3& Vector3::operator=(const Vector3& vector3)
{
    this->x = vector3.x;
    this->y = vector3.y;
    this->z = vector3.z;
    return *this;
}
// 重载 "==" 操作符(判断向量是否相等)
bool Vector3::operator==(const Vector3& vector3)
{
    if (this->x == vector3.x&&this->y == vector3.y&&this->z == vector3.z)
    {
        return true;
    }
    return false;
}
// 重载 "!=" 操作符(判断向量是否不相等)
bool Vector3::operator!=(const Vector3& vector3)
{
    return !(*this == vector3);
}
// 重载一元 "-" 操作符(向量取反)
Vector3& Vector3::operator-()
{
    this->x = -this->x;
    this->y = -this->y;
    this->z = -this->z;
    return *this;
}
// 重载二元 "+" 操作符(两个向量的相加)
Vector3& Vector3::operator+(const Vector3& vector3)
{
    this->x += vector3.x;
    this->y += vector3.y;
    this->z += vector3.z;
    return *this;
}
// 重载二元 "-" 操作符(两个向量的相减)
Vector3& Vector3::operator-(const Vector3& vector3)
{
    this->x -= vector3.x;
    this->y -= vector3.y;
    this->z -= vector3.z;
    return *this;
}
// 重载二元 "*" 操作符(标量与向量相乘)
Vector3& Vector3::operator*(const float k)
{
    this->x *= k;
    this->y *= k;
    this->z *= k;
    return *this;
}
// 重载二元 "/" 操作符(标量与向量相除)
Vector3& Vector3::operator/(const float k)
{
    this->x /= k;
    this->y /= k;
    this->z /= k;
    return *this;
}
// 重载自反运算符
Vector3& Vector3::operator+=(const Vector3& vector3)
{
    *this = *this + vector3;
    return *this;
}
Vector3& Vector3::operator-=(const Vector3& vector3)
{
    *this = *this - vector3;
    return *this;
}
Vector3& Vector3::operator*=(const float k)
{
    *this = *this * k;
    return *this;
}
Vector3& Vector3::operator/=(const float k)
{
    *this = *this / k;
    return *this;
}
// 重载二元 "*" 操作符(两个向量的点乘)
float Vector3::operator*(const Vector3& vector3)
{
    return this->x*vector3.x + this->y*vector3.y + this->z*vector3.z;
}
// 重置向量为零向量
void Vector3::zero()
{
    this->x = 0.f;
    this->y = 0.f;
    this->z = 0.f;
}
// 向量的模
float Vector3::mag()
{
    return sqrt(this->x*this->x + this->y*this->y + this->z*this->z);
}
// 向量的标准化
void Vector3::normalize()
{
    this->x = this->x / this->mag();
    this->y = this->y / this->mag();
    this->z = this->z / this->mag();
}
// 向量的叉乘
Vector3 Vector3::crossProduct(const Vector3& vector3)
{
    Vector3 vec3;
    vec3.x = this->y*vector3.z - this->z*vector3.y;
    vec3.y = this->z*vector3.x - this->x*vector3.z;
    vec3.z = this->x*vector3.y - this->y*vector3.x;
    return vec3;
}
// 计算两点的距离
float Vector3::distance(const Vector3& vector3)
{
    return sqrt(
        (this->x - vector3.x)*(this->x - vector3.x) +
        (this->y - vector3.y)*(this->y - vector3.y) + 
        (this->z - vector3.z)*(this->z - vector3.z)
        );
}
原文地址:https://www.cnblogs.com/metalsteel/p/6647169.html