第六章: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文件格式",实际上它完全不需要以磁 ...
随机推荐
- orcale 之 SQL 语言基础
SQL 全称是结构化查询语句(Structure Query Language),是数据库操作的国际化语言,对所有的数据库产品都要支持. SQL 语言的分类 我们按照其功能可以大致分为四类: 数据定义 ...
- E. Change-free
Student Arseny likes to plan his life for n days ahead. He visits a canteen every day and he has alr ...
- 学生管理系统(C语言)
#include <stdio.h> #include <stdlib.h> #include <string.h> #define N 3 #define LEN ...
- devexpress实现单元格根据条件显示不同的样式(颜色、字体、对齐方式,大小等)
1.devexpress控件库之所以被大家所喜爱,是因为它将许多常用的东西都封装成了属性.可以通过一些简单的配置,将以前某些需要大篇幅代码才可实现的效果展示出来.这里是一个实现了将[第二列数据在表格0 ...
- MINA、Netty、Twisted一起学(十一):SSL/TLS
什么是SSL/TLS 不使用SSL/TLS的网络通信,一般都是明文传输,网络传输内容在传输过程中很容易被窃听甚至篡改,非常不安全.SSL/TLS协议就是为了解决这些安全问题而设计的.SSL/TLS协议 ...
- SQL SERVER的统计信息
1 什么是统计信息 统计信息 描述了 表格或者索引视图中的某些列的值 的分布情况,属于数据库对象.根据统计信息,查询优化器就能评估查询过程中需要读取的行数及结果集情况,同时也能创建高质量的查询 ...
- Javascript中好用更改时间的方法
<script type="text/javascript"> //格式化时间格式的字符串 String.prototype.myTimes = function () ...
- JS判断手机当前的系统类型
<script language="javascript"> window.onload = function () { var n = navigator.userA ...
- ASP渲染下拉框使时间依次减少
<% x=year(now()) y=year(now())-1 Do While y>2002%><li><a href="#201 ...
- java基础:模拟ATM取款机
package com.atm; import java.util.Scanner; /** * ATM类实现 * * @author 向往的生活 */ public class ATM { publ ...