3D数学 自定义向量类进行运算

设计一个3维向量类,可以实现如下运算:

  • 零向量
  • 负向量
  • 向量大小、长度、模
  • 标量与向量的乘除法
  • 单位向量
  • 向量的加法和减法
  • 距离公式
  • 向量点乘
  • 向量叉乘

由于原理很简单,所以不解释,下面给出全部源代码:

/////////////////////////////////////////////////////////////////////////////
//
// 3D数学基础:游戏与图形开发
// 3D Math Primer for Games and Graphics Development
//
// Vector3.h - 3D向量声明
// Vector3.h - Declarations for 3D vector class
//
// 登录gamemath.com以获得该文件的最新版本
// Visit gamemath.com for the latest version of this file.
//
// 额外的注释,请看第六章
// For additional comments, see Chapter 6.
//
/////////////////////////////////////////////////////////////////////////////#ifndef __VECTOR3_H_INCLUDED__
#define __VECTOR3_H_INCLUDED__#include <math.h>/////////////////////////////////////////////////////////////////////////////
//
// Vector3类 - 一个简单的3D向量类
// class Vector3 - a simple 3D vector class
//
/////////////////////////////////////////////////////////////////////////////class Vector3 {
public:// 公有呈现:这里没有太多选择。
// Public representation:  Not many options here.float x,y,z;// 构造函数
// Constructors// 默认构造函数让向量保持不确定的状态// Default constructor leaves vector in// an indeterminate stateVector3() {}// 拷贝构造函数// Copy constructorVector3(const Vector3 &a) : x(a.x), y(a.y), z(a.z) {}// 给定三个值的构造函数// Construct given three valuesVector3(float nx, float ny, float nz) : x(nx), y(ny), z(nz) {}// 标准对象操作
// Standard object maintenance// 赋值。我们遵守C语言惯例并且返回左值引用。// Assignment.  We adhere to C convention and// return reference to the lvalueVector3 &operator =(const Vector3 &a) {x = a.x; y = a.y; z = a.z;return *this;}// 检查相等// Check for equalitybool operator ==(const Vector3 &a) const {return x==a.x && y==a.y && z==a.z;}bool operator !=(const Vector3 &a) const {return x!=a.x || y!=a.y || z!=a.z;}// 向量操作
// Vector operations// 设置向量为0向量// Set the vector to zerovoid zero() { x = y = z = 0.0f; }// 一元运算符减号返回向量的负值。// Unary minus returns the negative of the vectorVector3 operator -() const { return Vector3(-x,-y,-z); }// 二元运算符加号和减号:向量的加减法// Binary + and - add and subtract vectorsVector3 operator +(const Vector3 &a) const {return Vector3(x + a.x, y + a.y, z + a.z);}Vector3 operator -(const Vector3 &a) const {return Vector3(x - a.x, y - a.y, z - a.z);}// 标量的乘法和除法// Multiplication and division by scalarVector3 operator *(float a) const {return Vector3(x*a, y*a, z*a);}Vector3 operator /(float a) const {// 注意:这里没有检查分母是否为0float   oneOverA = 1.0f / a; // NOTE: no check for divide by zero here return Vector3(x*oneOverA, y*oneOverA, z*oneOverA);}// 合并赋值操作来使得和C语言符号约定一致// Combined assignment operators to conform to// C notation conventionVector3 &operator +=(const Vector3 &a) {x += a.x; y += a.y; z += a.z;return *this;}Vector3 &operator -=(const Vector3 &a) {x -= a.x; y -= a.y; z -= a.z;return *this;}Vector3 &operator *=(float a) {x *= a; y *= a; z *= a;return *this;}Vector3 &operator /=(float a) {float   oneOverA = 1.0f / a;x *= oneOverA; y *= oneOverA; z *= oneOverA;return *this;}// 向量单位化// Normalize the vectorvoid    normalize() {float magSq = x*x + y*y + z*z;// 检查除以0的情况if (magSq > 0.0f) { // check for divide-by-zerofloat oneOverMag = 1.0f / sqrt(magSq);x *= oneOverMag;y *= oneOverMag;z *= oneOverMag;}}// 向量点乘。我们重载这个标准乘法符号来做这件事。// Vector dot product.  We overload the standard// multiplication symbol to do thisfloat operator *(const Vector3 &a) const {return x*a.x + y*a.y + z*a.z;}
};/////////////////////////////////////////////////////////////////////////////
//
// 非成员函数
// Nonmember functions
//
/////////////////////////////////////////////////////////////////////////////// 计算向量的大小
// Compute the magnitude of a vectorinline float vectorMag(const Vector3 &a) {return sqrt(a.x*a.x + a.y*a.y + a.z*a.z);
}// 计算两个向量的叉乘
// Compute the cross product of two vectorsinline Vector3 crossProduct(const Vector3 &a, const Vector3 &b) {return Vector3(a.y*b.z - a.z*b.y,a.z*b.x - a.x*b.z,a.x*b.y - a.y*b.x);
}// 标量右乘,出于一致性
// Scalar on the left multiplication, for symmetryinline Vector3 operator *(float k, const Vector3 &v) {return Vector3(k*v.x, k*v.y, k*v.z);
}// 计算两点间的距离
// Compute the distance between two pointsinline float distance(const Vector3 &a, const Vector3 &b) {float dx = a.x - b.x;float dy = a.y - b.y;float dz = a.z - b.z;return sqrt(dx*dx + dy*dy + dz*dz);
}// 计算两点间的距离的平方。当比较距离时,这是非常有用的,因为平方根运算很慢。
// Compute the distance between two points, squared.  Often useful
// when comparing distances, since the square root is slowinline float distanceSquared(const Vector3 &a, const Vector3 &b) {float dx = a.x - b.x;float dy = a.y - b.y;float dz = a.z - b.z;return dx*dx + dy*dy + dz*dz;
}/////////////////////////////////////////////////////////////////////////////
//
// 全局变量
// Global variables
//
/////////////////////////////////////////////////////////////////////////////// 我们提供全局零向量
// We provide a global zero vector constantextern const Vector3 kZeroVector;/////////////////////////////////////////////////////////////////////////////
#endif // #ifndef __VECTOR3_H_INCLUDED__

3D数学 自定义三维向量类进行运算相关推荐

  1. 【python学习】自定义三维向量类 加减乘除查看值和长度

    <中学生可以这样学Python>P166 自定义三维向量类 ##自定义三维向量类 class Vector3:#构造方法,初始化,定义向量坐标def __init__(self,x,y,z ...

  2. 【Python养成】:案例(设计三维向量类、实现向量的加法、减法以及向量与标量的乘法和除法运算、编写自定义类,模拟内置集、编写自定义类,模拟双端队列。)

    学习内容:设计三维向量类.实现向量的加法.减法以及向量与标量的乘法和除法运算 设计三维向量类.实现向量的加法.减法以及向量与标量的乘法和除法运算 实验代码: class Vector_3D:def _ ...

  3. 自定义一个三维向量类,并实现相关运算

    自定义一个三维向量类,并实现向量之间的加法.减法,以及向量与标量之间的乘法与除法运算 class Vector:def __init__(self,a,b,c):self.x=aself.y=bsel ...

  4. 7-3 三维向量运算设计一个三维向量类,实现向量加法、减法以及向量与标量的乘法和除法运算。

    7-3 三维向量运算 设计一个三维向量类,实现向量加法.减法以及向量与标量的乘法和除法运算.后面添加下面代码完成: 天杀的出题人,非得放个图片在这,放个代码块会死吗? 运行的时候,要把这张图片里的内容 ...

  5. 3D数学读书笔记——向量运算及在c++上的实现

    本系列文章由birdlove1987编写,转载请注明出处. 文章链接: http://blog.csdn.net/zhurui_idea/article/details/24782661 开始之前:接 ...

  6. python设计一个三维向量类_一个简单的三维向量类

    最近在看<3D数学基础:图形与游戏开发>.该书第六章实现了一个简单三维向量类.我看了一下代码,发现有些地方是错的,于是做了一些小修改.该三维向量类实现了一些常用的向量运算,如点乘.叉乘等. ...

  7. 三维向量类Vector类封装,包含三维向量一些基本运算

    (1)网上参考的三维向量类 /*--------------------------------------------------* 类名称:三维向量类-Vector.h* 类作用:用于三维空间中向 ...

  8. java 三维向量类_三维向量类

    还是在读书的时候帮外专业朋友做作业,用GDI实现三维空间的立方体绘制和旋转的操作,那个时候自己根据<线性代数与空间解析几何>以及<计算机图形学>等课程的相关知识写了一个三维向量 ...

  9. 一个简单的三维向量类

    作者:朱金灿 来源:http://blog.csdn.net/clever101/ 最近在看<3D数学基础:图形与游戏开发>.该书第六章实现了一个简单三维向量类.我看了一下代码,发现有些地 ...

最新文章

  1. NLP深度学习:近期趋势概述 1
  2. 鼠标右键新建菜单删除或添加项目
  3. Knn算法(约会问题应用)
  4. 新思路保障网络安全 基于平台的网络安全架构体系
  5. DeepLearning.AI笔记:二、神经网络编程基础
  6. 很高兴加入 英文_XR车载公司Holoride加入高通XR计划
  7. 第四十一期:从Windows到鸿蒙——操作系统的前世与今生
  8. python开发商城实战_python框架Django实战商城项目之工程搭建
  9. BZOJ2729 [HNOI2012]排队 【高精 + 组合数学】
  10. 素短语,最左素短语-编译原理
  11. Windows Phone 7 使用Canvas Grid StackPanel进行布局管理
  12. html字体代码大全_HTML基础笔记(一)
  13. csv 读写 python_Python CSV读写
  14. html网页正确代码,html简单网页代码(html代码格式开头)
  15. Linux执行sql文件
  16. 【无标题】 2022淘宝天猫双十一喵果总动员玩法攻略
  17. GitHub上十大热门Python项目
  18. light动名词_—Thelightintheofficeisstillon.—Oh,Iforgot_____.[ ]A.tur
  19. ctfshow 做题 MISC入门 模块1-10
  20. 基金投资入门 5:基金的业务类型及交易中的费用

热门文章

  1. 计算机基础-了解软硬件-00:文章内容规划、了解软件和硬件的区别
  2. 算法笔记.胡凡 第6章 C++标准模板库(STL)介绍
  3. 铭利达深交所上市:年营收122亿 实控人陶诚家族色彩浓厚
  4. 【转】HDMI之TMDS信号
  5. uniapp开发的微信公众号,微信设置字体大小或者关怀模式,页面布局字体大小不受影响的解决方法
  6. 多多自走棋改动_《多多自走棋》2.0版本有哪些改动 2.0版本更新改动内容汇总...
  7. 无法重新启动计算机进入恢复环境,win10系统出现电脑一直重新启动的有效解决方法...
  8. Qt信号与槽的拓展和案例
  9. 45吨大米送千名老人 浙江东阳一男子坚持敬老行善33年
  10. Ubuntu20.4系统隐藏顶栏