Code:
#pragma once
#include <Windows.h>
#include <math.h>
class Vector
{
public:
float r[3];
Vector(void)
{
}
Vector(float r[3])
{
this->r[0] = r[0];
this->r[1] = r[2];
this->r[1] = r[2];
}
Vector(float r1, float r2, float r3)
{
this->r[0] = r1;
this->r[1] = r2;
this->r[2] = r3;
}
Vector(Vector &v)
{
this->r[0] = v.r[0];
this->r[1] = v.r[1];
this->r[2] = v.r[2];
}
~Vector(void)
{
this->r[0] = 0;
this->r[0] = 0;
this->r[0] = 0;
}
inline void operator = (Vector &v)
{
this->r[0] = v.r[0];
this->r[1] = v.r[1];
this->r[2] = v.r[2];
}
inline void operator = (float r[3])
{
this->r[0] = r[0];
this->r[1] = r[1];
this->r[2] = r[2];
}
inline void operator () (Vector &v)
{
*this = v;
}
inline void operator () (float r[3])
{
*this = r;
}
inline void operator () (float r1, float r2, float r3)
{
this->r[0] = r1;
this->r[1] = r2;
this->r[2] = r3;
}
inline bool operator == (Vector &v)
{
return (bool)(this->r[0] == v.r[0] && this->r[1] == v.r[1] && this->r[2] == v.r[2]);
}
inline bool operator == (float r[3])
{
return (bool)(this->r[0] == r[0] && this->r[1] == r[1] && this->r[2] == r[2]);
}
inline bool operator != (Vector &v)
{
return !(*this == v);
}
inline bool operator != (float r[3])
{
return !(*this == r);
}
inline Vector operator + (Vector &v)
{
return Vector(this->r[0] + v.r[0], this->r[1] + v.r[1], this->r[2] + v.r[2]);
}
inline Vector operator + (float r[3])
{
return Vector(this->r[0] + r[0], this->r[1] + r[1], this->r[2] + r[2]);
}
inline void operator += (Vector &v)
{
*this = *this + v;
}
inline void operator += (float r[3])
{
*this = *this + r;
}
inline Vector operator - (Vector &v)
{
return Vector(this->r[0] - v.r[0], this->r[1] - v.r[1], this->r[2] - v.r[2]);
}
inline Vector operator - (float r[3])
{
return Vector(this->r[0] - r[0], this->r[1] - r[1], this->r[2] - r[2]);
}
inline void operator -= (Vector &v)
{
*this = *this - v;
}
inline void operator -= (float r[3])
{
*this = *this - r;
}
inline Vector operator * (float f)
{
return Vector(this->r[0] * f, this->r[1] * f, this->r[2] * f);
}
inline void operator *= (float f)
{
*this = *this * f;
}
inline Vector operator / (float f)
{
return Vector(*this * (1 / f));
}
inline void operator /= (float f)
{
*this = *this / f;
}
float operator [] (unsigned long i)
{
if(i < 3)
return this->r[i];
return 0.0f;
}
static float Length(Vector v)
{
return sqrt(Vector::DotProduct(v, v));
}
static void Normalize(Vector &v)
{
v /= Vector::Length(v);
}
static Vector Normalize(Vector v)
{
return v / Vector::Length(v);
}
static float DotProduct(Vector v1, Vector v2)
{
return (v1.r[0] * v2.r[0] + v1.r[1] * v2.r[1] + v1.r[2] * v2.r[2]);
}
static float AngleRadian(Vector v1, Vector v2)
{
if(v1.r[2] == v2.r[2])
return 0.0f;
return (float)(acos(Vector::DotProduct(v1, v2) / (Vector::Length(v1) *Vector::Length(v2))));
}
static float AngleDegree(Vector v1, Vector v2)
{
if(v1.r[2] == v2.r[2])
return 0.0f;
return (float)(57.2957795f * acos(Vector::DotProduct(v1, v2) / (Vector::Length(v1) *Vector::Length(v2))));
}
static bool IsOrthogonal(Vector v1, Vector v2)
{
return (bool)(Vector::DotProduct(v1, v2) == 0.0f);
}
static Vector CrossProduct(Vector v1, Vector v2)
{
return Vector(v1.r[1] * v2.r[2] - v1.r[2] * v2.r[2], v1.r[2] * v2.r[0] - v1.r[0] * v2.r[2], v1.r[0] * v2.r[1] - v1.r[1] - v2.r[0]);
}
static bool IsCollinear(Vector v1, Vector v2)
{
Vector v = Vector::CrossProduct(v1, v2);
return (bool)(v.r[0] == 0 && v.r[1] == 0 && v.r[2] == 0);
}
static float BoxProduct(Vector v1, Vector v2, Vector v3)
{
return Vector::DotProduct(Vector::CrossProduct(v1, v2), v3);
}
static bool IsCoplanar(Vector v1, Vector v2, Vector v3)
{
return (bool)(Vector::BoxProduct(v1, v2, v3) == 0.0f);
}
static Vector Translate(Vector v1, Vector v2)
{
return v2 - v1;
}
static Vector NormalizedViewVectorRadian(float Yaw, float Pitch)
{
return Vector(cos(Yaw) * cos(Pitch), sin(Yaw) * cos(Pitch), -1.0f * sin(Pitch));
}
static Vector NormalizedViewVectorDegree(float Yaw, float Pitch)
{
return Vector(cos(0.0174532925f * Yaw) * cos(0.0174532925f * Pitch), sin(0.0174532925f * Yaw) * cos(0.0174532925f * Pitch), -1.0f * sin(0.0174532925f * Pitch));
}
static float NormalizedYawRadian(Vector v)
{
if(v.r[1] == 0)
v.r[1] = 0.0001f;
return atan(v.r[0] / v.r[1]);
}
static float NormalizedYawDegree(Vector v)
{
if(v.r[1] == 0)
v.r[1] = 0.0001f;
return 0.0174532925f * atan(v.r[0] / v.r[1]);
}
static float NormalizedPitchRadian(Vector v)
{
return Vector::AngleRadian(v, Vector(v.r[0], v.r[1], 0.0f));
}
static float NormalizedPitchDegree(Vector v)
{
return Vector::AngleDegree(v, Vector(v.r[0], v.r[1], 0.0f));
}
};