这篇文章在上一篇C++ 实现Vector3的基础之上,添加了第四分量w,并实现了Vector4的相关操作。
有关Vector4的参数、函数、运算操作符参考于微软文档:
微软Vector4
经main函数测试,可以运行通过。
#ifndef _VECTOR4_H_
#define _VECTOR4_H_
#include
using namespace std;
class vector4
{
private:
/* data */
public:
float w; // The W component of the vector.
float x; // The X component of the vector.
float y; // The Y component of the vector.
float z; // The Z component of the vector.
vector4();
vector4(float x, float y, float z, float w); // Creates a vector whose elements have the specified values.
~vector4();
static vector4 One; // Gets a vector whose 3 elements are equal to one.
static vector4 UnitX; // Gets the vector (1,0,0,0).
static vector4 UnitY; // Gets the vector (0,1,0,0).
static vector4 UnitZ; // Gets the vector (0,0,1,0).
static vector4 UnitW; // Gets the vector (0,0,0,1).
static vector4 Zero; // Gets a vector whose 4 elements are equal to zero.
static vector4 Abs(vector4 value); // Returns a vector whose elements are the absolute values of each of the specified vector's elements.
static vector4 Add(vector4 left, vector4 right); // Adds two vectors together.
static float Distance(vector4 value1, vector4 value2); // Computes the Euclidean distance between the two given points.
static float DistanceSquared(vector4 value1, vector4 value2); // Returns the Euclidean distance squared between two specified points.
static vector4 Divide(vector4 left, float divisor); // Divides the specified vector by a specified scalar value.
static vector4 Divide(vector4 left, vector4 right); // Divides the first vector by the second.
static float Dot(vector4 vector1, vector4 vector2); // Returns the dot product of two vectors.
const float Length(vector4 left); // Returns the length of this vector object.
const float LengthSquared(vector4 left); // Returns the length of the vector squared.
static vector4 Multiply(float left, vector4 right); // Multiplies a scalar value by a specified vector.
static vector4 Multiply(vector4 left, float right); // Multiplies a vector by a specified scalar.
static vector4 Negate(vector4 value); // Negates a specified vector.
static vector4 Normalize(vector4 value); // Returns a vector with the same direction as the specified vector, but with a length of one.
static vector4 Subtract(vector4 left, vector4 right); // Subtracts the second vector from the first.
};
static vector4 operator+(vector4 left, vector4 right); // Adds two vectors together.
static vector4 operator/(vector4 value1, float value2); // Divides the specified vector by a specified scalar value.
static vector4 operator/(vector4 left, vector4 right); // Divides the first vector by the second.
static bool operator==(vector4 left, vector4 right); // Returns a value that indicates whether each pair of elements in two specified vectors is equal.
static bool operator!=(vector4 left, vector4 right); // Returns a value that indicates whether two specified vectors are not equal.
static vector4 operator*(float left, vector4 right); // Multiples the scalar value by the specified vector.
static vector4 operator*(vector4 left, float right); // Multiples the specified vector by the specified scalar value.
static vector4 operator*(vector4 left, vector4 right); // Returns a new vector whose values are the product of each pair of elements in two specified vectors.
static vector4 operator-(vector4 left, vector4 right); // Subtracts the second vector from the first.
static vector4 operator-(vector4 value); // Negates the specified vector.
#endif
#include "Vector4.h"
#include
using namespace std;
// 实现vector类的构造函数
vector4::vector4() : x(0), y(0), z(0), w(0) {}
vector4::vector4(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) {}
vector4::~vector4() {}
// 实现vector类的参数
vector4 vector4::One = vector4(1, 1, 1, 1);
vector4 vector4::UnitX = vector4(1, 0, 0, 0);
vector4 vector4::UnitY = vector4(0, 1, 0, 0);
vector4 vector4::UnitZ = vector4(0, 0, 1, 0);
vector4 vector4::UnitW = vector4(0, 0, 0, 1);
vector4 vector4::Zero = vector4(0, 0, 0, 0);
// 实现vector类的函数
vector4 vector4::Abs(vector4 value)
{
return vector4(fabsf(value.x), fabsf(value.y), fabsf(value.z), fabsf(value.w));
}
vector4 vector4::Add(vector4 left, vector4 right)
{
return vector4(left.x + right.x, left.y + right.y, left.z + right.z, left.w + right.w);
}
float vector4::Distance(vector4 value1, vector4 value2)
{
float x = value1.x - value2.x;
float y = value1.y - value2.y;
float z = value1.z - value2.z;
float w = value1.w - value2.w;
return sqrt(x * x + y * y + z * z + w * w);
}
float vector4::DistanceSquared(vector4 value1, vector4 value2)
{
float x = value1.x - value2.x;
float y = value1.y - value2.y;
float z = value1.z - value2.z;
float w = value1.w - value2.w;
return x * x + y * y + z * z + w * w;
}
vector4 vector4::Divide(vector4 left, float divisor)
{
return vector4(left.x / divisor, left.y / divisor, left.z / divisor, left.w / divisor);
}
vector4 vector4::Divide(vector4 left, vector4 right)
{
return vector4(left.x / right.x, left.y / right.y, left.z / right.z, left.w / right.w);
}
float vector4::Dot(vector4 vector1, vector4 vector2)
{
return vector1.x * vector2.x + vector1.y * vector2.y + vector1.z * vector2.z + vector1.w * vector2.w;
}
const float vector4::Length(vector4 left)
{
return sqrt(left.x * left.x + left.y * left.y + left.z * left.z + left.w * left.w);
}
const float vector4::LengthSquared(vector4 left)
{
return left.x * left.x + left.y * left.y + left.z * left.z + left.w * left.w;
}
vector4 vector4::Multiply(float left, vector4 right)
{
return vector4(right.x * left, right.y * left, right.z * left, right.w * left);
}
vector4 vector4::Multiply(vector4 left, float right)
{
return vector4(right * left.x, right * left.y, right * left.z, right * left.w);
}
vector4 vector4::Negate(vector4 value)
{
return vector4(-value.x, -value.y, -value.z, -value.w);
}
vector4 vector4::Normalize(vector4 value)
{
float len = value.Length(value);
return vector4(value.x / len, value.y / len, value.z / len, value.w / len);
}
vector4 vector4::Subtract(vector4 left, vector4 right)
{
return vector4(left.x - right.x, left.y - right.y, left.z - right.z, left.w - right.w);
}
// 实现运算符
vector4 operator+(vector4 left, vector4 right)
{
return vector4(left.x + right.x, left.y + right.y, left.z + right.z, left.w + right.w);
}
vector4 operator/(vector4 left, float divisor)
{
return vector4(left.x / divisor, left.y / divisor, left.z / divisor, left.w / divisor);
}
vector4 operator/(vector4 left, vector4 right)
{
return vector4(left.x / right.x, left.y / right.y, left.z / right.z, left.w / right.w);
}
bool operator==(vector4 left, vector4 right)
{
return (left.x == right.x && left.y == right.y && left.z == right.z && left.w == right.w);
}
bool operator!=(vector4 left, vector4 right)
{
return (left.x != right.x || left.y != right.y || left.z != right.z || left.w != right.w);
}
vector4 operator*(float left, vector4 right)
{
return vector4(right.x * left, right.y * left, right.z * left, right.w * left);
}
vector4 operator*(vector4 left, float right)
{
return vector4(right * left.x, right * left.y, right * left.z, right * left.w);
}
vector4 operator*(vector4 left, vector4 right)
{
return vector4(left.x * right.x, left.y * right.y, left.z * right.z, left.w * right.w);
}
vector4 operator-(vector4 left, vector4 right)
{
return vector4(left.x - right.x, left.y - right.y, left.z - right.z, left.w - right.w);
}
vector4 operator-(vector4 value)
{
return vector4(-value.x, -value.y, -value.z, -value.w);
}