第六章: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 = .f;
this->y = .f;
this->z = .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)
);
}
第六章:3D向量类的更多相关文章
- C#图解教程 第六章 深入理解类
深入理解类 类成员成员修饰符的顺序实例类成员静态字段从类的外部访问静态成员 静态字段示例静态成员的生存期 静态函数成员其他静态类成员类型成员常量常量与静态量属性 属性声明和访问器属性示例使用属性属性和 ...
- PJSUA2开发文档--第六章 媒体 Media类
6. 媒体(Media) 媒体对象是能够产生媒体或接受媒体的对象. Media的重要子类是AudioMedia,它代表音频媒体.PJSUA2支持多种类型的音频媒体对象: 捕获设备的AudioMedia ...
- Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十六章:实例化和截头锥体裁切
原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十六章:实例化和截头锥体裁切 代码工程地址: https://git ...
- Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第六章:在Direct3D中绘制
原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第六章:在Direct3D中绘制 代码工程地址: https://gi ...
- Java基础知识二次学习--第六章 常用类
第六章 常用类 时间:2017年4月26日16:14:49~2017年4月26日16:56:02 章节:06章_01节~06章_06节 视频长度:20:57+1:15+8:44+1:26+11:2 ...
- 《深入理解java虚拟机》第六章 类文件结构
第六章 类文件结构 6.2 无关性的基石 各种不同平台的虚拟机与所有的平台都统一使用的程序存储格式--字节码(ByteCode)是构成平台无关性的基石.java虚拟机不和包括java在内的任何语言 ...
- “全栈2019”Java第三十六章:类
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- c++ 吕凤翥 第六章 类和对象(二)
c++ 吕凤翥 第六章 类和对象(二) 指针 引用 和数组 一:对象指针和对象引用 1.指向类的成员的指针 分为指向成员变量和指向成员函数两种指针 成员变量的格式: 类型说明符 类名: ...
- JVM学习笔记-第六章-类文件结构
JVM学习笔记-第六章-类文件结构 6.3 Class类文件的结构 本章中,笔者只是通俗地将任意一个有效的类或接口锁应当满足的格式称为"Class文件格式",实际上它完全不需要以磁 ...
随机推荐
- Vuex 模块化与项目实例 (2.0)
Vuex 强调使用单一状态树,即在一个项目里只有一个 store,这个 store 集中管理了项目中所有的数据以及对数据的操作行为.但是这样带来的问题是 store 可能会非常臃肿庞大不易维护,所以就 ...
- linux php安装memcached扩展
memcached的安装包括:1.服务端的安装:2.客户端的安装 一.服务器端安装 方法一: memcached 依赖于libevent 库,因此我们需要先安装libevent. 假设将源码放在/us ...
- js中可转bool为false的值
number :0为false,其他都为true. string:""为false,其他字符串都为真,PS: " "空格为true,"false&qu ...
- 前端安全(XSS、CSRF防御)
一.网络安全 OWASP:开放式Web应用程序安全项目(OWASP,Open Web Application Security Project) OWASP是一个开源的 ...
- 完全理解Python迭代对象、迭代器、生成器
在了解Python的数据结构时,容器(container).可迭代对象(iterable).迭代器(iterator).生成器(generator).列表/集合/字典推导式(list,set,dict ...
- 【经验】JavaScript
1.function closeWin(){ window.open('','_self'); window.opener=null; // window. ...
- Java中代理对象的使用小结
在某些情况下,一个客户不想或不能直接引用另一个对象,而代理对象可以在客户端和目标对象之间起到了中介作用,这不仅仅使用代理模式,还可以实现适配器模式.装饰模式等. 代理对象内部含有对真实对象的引用,从而 ...
- Codeblocks快捷键
Codeblocks快捷键: Ctrl+Shift+Enter:复制控制台exe内容 Ctrl+Z:撤销 Ctrl+F:查找 Ctrl+R:查找替换功能 Ctrl+Shift+C:加注释 C ...
- Linux服务器下Java环境搭建
前言: 在centOS下,像阿里云等都预先设置了jdk,不过不是SUN的java JDK,一般情况要重新装jdk,而且一般情况下自己装的Jdk相对来说易控制版本,稳定性更高.所以以下是我卸载预装jdk ...
- 【js】性能问题
执行环境和作用域 一.全局变量和局部变量 每个执行环境都有与之关联的变量对象(变量和函数存储在这里),全局执行环境是最外围的执行环境,根据ECMA实现所在的宿主不同,表示的执行环境的对象也不同.在we ...