第一节:类接口的设计

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向量类的更多相关文章

  1. C#图解教程 第六章 深入理解类

    深入理解类 类成员成员修饰符的顺序实例类成员静态字段从类的外部访问静态成员 静态字段示例静态成员的生存期 静态函数成员其他静态类成员类型成员常量常量与静态量属性 属性声明和访问器属性示例使用属性属性和 ...

  2. PJSUA2开发文档--第六章 媒体 Media类

    6. 媒体(Media) 媒体对象是能够产生媒体或接受媒体的对象. Media的重要子类是AudioMedia,它代表音频媒体.PJSUA2支持多种类型的音频媒体对象: 捕获设备的AudioMedia ...

  3. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十六章:实例化和截头锥体裁切

    原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十六章:实例化和截头锥体裁切 代码工程地址: https://git ...

  4. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第六章:在Direct3D中绘制

    原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第六章:在Direct3D中绘制 代码工程地址: https://gi ...

  5. 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 ...

  6. 《深入理解java虚拟机》第六章 类文件结构

    第六章 类文件结构   6.2 无关性的基石 各种不同平台的虚拟机与所有的平台都统一使用的程序存储格式--字节码(ByteCode)是构成平台无关性的基石.java虚拟机不和包括java在内的任何语言 ...

  7. “全栈2019”Java第三十六章:类

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  8. c++ 吕凤翥 第六章 类和对象(二)

    c++ 吕凤翥 第六章 类和对象(二) 指针   引用  和数组 一:对象指针和对象引用 1.指向类的成员的指针 分为指向成员变量和指向成员函数两种指针 成员变量的格式:     类型说明符  类名: ...

  9. JVM学习笔记-第六章-类文件结构

    JVM学习笔记-第六章-类文件结构 6.3 Class类文件的结构 本章中,笔者只是通俗地将任意一个有效的类或接口锁应当满足的格式称为"Class文件格式",实际上它完全不需要以磁 ...

随机推荐

  1. MONO 如何打包 .NET程序独立运行(winform篇)

    .NET程序独立运行是指运行.NET的电脑上,不需要安装.NET框架. .NET程序集“独立运行”并非真正的独立,它是运行在mono运行时基础之上的.由于这个运行时可以独立存在,所以,我们不需要在目标 ...

  2. hibernate切换数据源

    起因: 公司的当前产品,主要是两个项目集成的,一个是java项目,还有一个是php项目,两个项目用的是不同的数据源,但都是mysql数据库,因为java这边的开发工作已经基本完成了,而php那边任务还 ...

  3. 使用grunt完成requirejs的合并压缩和js文件的版本控制

    最近有一个项目使用了 requirejs 来解决前端的模块化,但是随着页面和模块的越来越多,我发现我快要hold不住这些可爱的js文件了,具体表现在每个页面都要设置一堆 requirejs 的配置( ...

  4. 微软.NET年芳15:我在Azure上搭建Photon服务器(C#.NET)

    网上火热的“微软.NET年芳15”文章,我也得写点什么嘛,毕竟我还是现任的微软MVP. 摘录网上的“.NET 15周年”信息如下: 微软的 .NET 框架本周迎来了 15 岁生日..NET 的第一个版 ...

  5. HQL查询——查询返回对象类型分析

    关于HQL查询,我们可以结合hibernate的API文档,重点围绕org.hibernate.Query接口,分析其方法,此接口的实例对象是通过通过session.对象的creatQuery(Str ...

  6. 如何用一张图片代替 'input:file' 上传本地文件??

    今天去面试,碰到了一道题,也许是因为紧张或者喝水喝多了,一时竟然没有转过弯来,回来之后一细想原来这么简单,哭笑不得,特此记录一下! 原题是这样的:  如何用一张图片代替 'input:file' 上传 ...

  7. Laravel路由和控制器的绑定

    路由和控制器的关系 路由文件地址在\app\Http\routes.php,我们来看两种不同的路由. Route::get('/', function () { return view('welcom ...

  8. iOS 推送全解析,你不可不知的所有 Tips!

    本文旨在对 iOS 推送进行一个完整的剖析,如果你之前对推送一无所知,那么在你认真地阅读了全文后必将变成一个推送老手,你将会对其中的各种细节和原理有充分的理解.以下是 pikacode 使用 iOS ...

  9. spring-framework-reference阅读笔记(一)

    Spring Framework Runtime 首先需要对Spring FrameWok框架有个直观的认识 Java日志框架的发展史 在读到Spring依赖JCL的时候,对Java的日志系统做点普及 ...

  10. ThinkPhp知识大全(非常详细)

    php框架 一.真实项目开发步骤: 多人同时开发项目,协作开发项目.分工合理.效率有提高(代码风格不一样.分工不好) 测试阶段 上线运行 对项目进行维护.修改.升级(单个人维护项目,十分困难,代码风格 ...