(1) Find the negative vector, a=(ax,ay,az). The negative vector is (-ax,-ay,-az);
Vector operations:
(2) The addition and subtraction of vectors is equal to the addition and subtraction of each component, a=(ax,ay,az), b=(bx,by,bz). ab=(ax-bx,ay-by,az-bz) ;
(3) The multiplication and division between scalar and vector is equal to the multiplication and division of each component, k=2, a=(ax,ay,az). k*a=(kax, kay, kaz);
Special operations:
(4) Vector standardization , vector a is divided by its own modulus length (size), a / ||a||;
(5) Vector point multiplication , vector a=(a1, a2, a3), b=(b1, b2, b3). The components are multiplied and then summed, the similarity of the two vectors,
Generally, the components are multiplied and then summed;
(6) Vector cross product , axb, the result of cross product is a vector (y1z2-z1y2, z1x2-x1z2, x1y2-y1x2)
(7) Find vector modulus, vector length, size, a=(ax,ay,az)
(8) The distance between two vectors, a=(ax,ay,az), b=(bx,by,bz).
#include <iostream>
#include <algorithm>
#include <math.h>
// 3D 向量类
class Vector3 {
public:
float x, y, z;
// 构造函数
Vector3() {};
Vector3(float nx, float ny, float nz) : x(nx), y(ny), z(nz) {}
// 复制构造函数
Vector3(const Vector3 &a): x(a.x), y(a.y), z(a.z) {}
// 标准对象操作
// 重载"=="操作符
bool operator ==(const Vector3& a) const
{
return a.x == x && a.y == y && a.z == z;
}
bool operator !=(const Vector3& a) const
{
return a.x != x || a.y != y || a.z != z;
}
// 向量运算
// 置为0向量
void zero() { x = y = z = 0.0f; }
// 重载一元“-”运算符,求负向量
Vector3 operator -() { return Vector3(-x, -y, -z); }
// 重载二元“+”和“-”运算符,向量之间相加减
Vector3 operator +(const Vector3& a)
{
return Vector3(a.x + x, a.y + y, a.z + z);
}
Vector3 operator -(const Vector3& a)
{
return Vector3(x - a.x, y - a.y, z - a.z);
}
// 重载二元“*”和“/”运算符,标量和向量之间相乘和相除
Vector3 operator *(float a) const
{
return Vector3(a * x, a * y, a * z);
}
Vector3 operator /(float a) const
{
float one_over_a = 1.0f / a; // 需对0检查
return Vector3(one_over_a * x, one_over_a * y, one_over_a * z);
}
// 重载自反运算符,即会修改自身
Vector3& 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 one_over_a = 1.0f / a;
x *= one_over_a; y *= one_over_a; z *= one_over_a;
}
// 向量标准化: a / ||a||
void normalize()
{
float dis = sqrt(x * x + y * y + z * z);
if (dis > 0.0f)
{
float one_over_dis = 1.0f / dis;
x *= one_over_dis;
y *= one_over_dis;
z *= one_over_dis;
}
}
// 向量点乘,重载乘号: a.b = ||a|| * ||b|| * cosθ,或者分量相乘再求和。
float operator *(const Vector3& a) const
{
return x * a.x + y * a.y + z * a.z;
}
};
// 求向量模,即求向量长度
inline float vectorLen(const Vector3& a)
{
return sqrt(a.x * a.x + a.y * a.y + a.z * a.z);
}
// 计算两向量的叉乘: axb
inline Vector3 crossProduct(const Vector3& a, const Vector3& b)
{
return Vector3(
a.y*b.z - a.z*b.y,
b.x*a.z - a.x*b.z,
a.x*b.y - a.y*b.x
);
}
// 重载“*”
inline Vector3 operator *(float k, const Vector3 &a)
{
return Vector3(k * a.x, k * a.y, k * a.z);
}
// 两点距离
inline 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);
}
// 提供一个全局0向量
extern const Vector3 kzeroVector;