第一节:类接口的设计

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. php引入文件(include 和require的区别)

    引入文件: 首先需要一个php文件: <?php class shao//类名必须和文件名相同!!! { public $xxx="666"; } $shili = new ...

  2. jQuery动态生成不规则表格前后端

    一.需求:有这么一张表 前四个属性当作联合主键 需要把该表所有的行在前端以表格形式显示出来,要求activityId相同时合并成一行,activityCode相同时,合并一行,activityVers ...

  3. 一个RESTful+MySQL程序

    前言 本章内容适合初学者(本人也是初学者). 上一章内容(http://www.cnblogs.com/vanezkw/p/6414392.html)是在浏览器中显示Hello World,今天我们要 ...

  4. Matlab中plot函数全功能解析

    Matlab中plot函数全功能解析 功能 二维曲线绘图 语法 plot(Y)plot(X1,Y1,...)plot(X1,Y1,LineSpec,...)plot(...,'PropertyName ...

  5. API code

    /*--------------------------------------------------- BLOKOUT2.C -- Mouse Button & Capture Demo ...

  6. js五种设计模式

    1.js工厂模式 var lev=function(){ return "嘿哈"; }; function Parent(){ var Child = new object(); ...

  7. java从入门到卖肠粉系列

    java从入门到卖肠粉系列 注:本教程只是从JAVA基础开始,绝对不会跟公司有任何利益冲突,更不会出现一行公司项目的代码 QQ群:9547527 推荐用土豆,百度去上传太慢,百度云在线播放还要转码.. ...

  8. Linux JDK+TOMCAT+MYSQL+redis 安装日志

    检查是否安装iptables #先检查是否安装了iptablesservice iptables status#安装iptablesyum install -y iptables#升级iptables ...

  9. Java进制转换示例

    收藏的代码,以备查询之用.进制之间转换都是以十进制作为中间层的. int os = 16; //十进制转成十六进制: Integer.toHexString(os); //十进制转成八进制 Integ ...

  10. SIFT中的高斯模糊

    高斯模糊是众多模糊算法中的一种,所谓的模糊,就是平滑图像,消除像素之间的差异,最容易想到的方法就是均值平滑. .均值模糊 均值模糊就是取目标像素周围像素的平均值.譬如 像素矩阵. |1|1|1| |1 ...