第一节:类接口的设计

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. 情人节,教大家使用css画出一朵玫瑰花。

    情人节到了,给大家来一朵高端的玫瑰花. 在网上看到的一个canvas实现的玫瑰花,效果很好,但是代码被压缩过,也没有注释,看的云里雾里的. 今天我教大脚用CSS来实现一朵玫瑰花. 先看效果 首先我们画 ...

  2. 微信公众号开发总结(Node.js + express + winston)

    关于订阅号.服务号.企业号 官方定位 订阅号:主要偏于为用户传达资讯(类似报纸杂志),认证后每天可以群发一条消息,可达到宣传效果,构建与读者之间更好的沟通和管理模式. 服务号:主要偏于服务交互(类似银 ...

  3. 关于hibernate注解的简单应用

    @Override 用途:重写父类的同名方法 单元测试注解 @Test 用途:用于测试 @Before 用途:单测方法走之前执行 @After 用途:单测方法走之后执行 注解的目标:替换小配置.替换h ...

  4. JAVA日常练习—程序输入string转化为int并求和

    实验结果如图:

  5. Java模拟新浪微博登陆抓取数据

    前言:  兄弟们来了来了,最近有人在问如何模拟新浪微博登陆抓取数据,我听后默默地抽了一口老烟,暗暗的对自己说,老汉是时候该你出场了,所以今天有时间就整理整理,浅谈一二. 首先:  要想登陆新浪微博需要 ...

  6. 基于Spring DM管理的Bundle获取Spring上下文对象及指定Bean对象

    在讲述服务注册与引用的随笔中,有提到context.getServiceReferences()方法,通过该方法可以获取到OSGI框架容器中的指定类型的服务引用,从而获取到对应的服务对象.同时该方法还 ...

  7. 安装和配置Symfony

    为了简化创建新项目的过程,Symfony提供一个安装程序. 安装Symfony Installer 使用Symfony Installer是创建新的Symfony项目的唯一推荐方式,这个install ...

  8. hadoop--安装1.2.1版本

    hadoop的安装分为三种方式,第一种单机安装,一般用于调试(其实一般都不用).第二种,伪分布式安装,一般程序员开发会使用这种方式.第三种,分布式安装,在实际环境中应用.今天在这里记下的是第二种,即伪 ...

  9. 为 instance 配置静态 IP - 每天5分钟玩转 OpenStack(157)

    这是 OpenStack 实施经验分享系列的第 7 篇. 传统运维中为服务器配置静态 IP 是再常见不过的了.但在 OpenStack 环境下只能指定 network,IP 都是 Neutron 从 ...

  10. html 5 video

    正项目中, 20秒 2mb左右在速度上可以接受, 但是最总怎样剪都不可以被游览器读取, 因为H.264 和一些我不清楚的. 为了简单解决这小问题, 请使用 http://easyhtml5video. ...