Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////
- // Vector.h //
- //////////////
- #include <math.h>
- #include "Main.h"
- typedef float Scalar;
- typedef bool Bool;
- class Vector3
- {
- public:
- Scalar X;
- Scalar Y;
- Scalar Z;
- Vector3();
- Vector3(Scalar X, Scalar Y, Scalar Z);
- void Invert();
- void Normalize();
- void CrossProduct(const Vector3& a, const Vector3& b);
- Vector3& operator+=(const Vector3& Vector);
- Vector3& operator-=(const Vector3& Vector);
- Vector3& operator*=(const Vector3& Vector);
- Vector3& operator/=(const Vector3& Vector);
- Bool operator==(const Scalar& Compare) const;
- Bool operator==(const Vector3& Vector) const;
- Bool operator!=(const Vector3& Vector) const;
- };
- inline Vector3 operator+(const Vector3& a, const Vector3& b)
- {
- return Vector3(a.X + b.X, a.Y + b.Y, a.Z + b.Z);
- }
- inline Vector3 operator-(const Vector3& a, const Vector3& b)
- {
- return Vector3(a.X - b.X, a.Y - b.Y, a.Z - b.Z);
- }
- inline Vector3 operator*(const Vector3& a, const Vector3& b)
- {
- return Vector3(a.X * b.X, a.Y * b.Y, a.Z * b.Z);
- }
- inline Vector3 operator/(const Vector3& a, const Vector3& b)
- {
- return Vector3(a.X / b.X, a.Y / b.Y, a.Z / b.Z);
- }
- inline Vector3 operator*(const Vector3& v, const float* m)
- {
- static Vector3 Result;
- Result.X = m[0] * v.X + m[4] * v.Y + m[8] * v.Z + m[12];
- Result.Y = m[1] * v.X + m[5] * v.Y + m[9] * v.Z + m[13];
- Result.Z = m[2] * v.X + m[6] * v.Y + m[10] * v.Z + m[14];
- return Result;
- }
- ////////////////
- // Vector.cpp //
- ////////////////
- #include "Vector.h"
- Vector3::Vector3()
- {
- X = 0.0f;
- Y = 0.0f;
- Z = 0.0f;
- }
- Vector3::Vector3(Scalar X, Scalar Y, Scalar Z)
- {
- this->X = X;
- this->Y = Y;
- this->Z = Z;
- }
- void Vector3::Invert()
- {
- this->X = -this->X;
- this->Y = -this->Y;
- this->Z = -this->Z;
- }
- void Vector3::Normalize()
- {
- Scalar Magnitude = sqrt(this->X * this->X + this->Y * this->Y + this->Z * this->Z);
- if(Magnitude == 0.0f) return;
- this->X /= Magnitude;
- this->Y /= Magnitude;
- this->Z /= Magnitude;
- }
- void Vector3::CrossProduct(const Vector3& a, const Vector3& b)
- {
- this->X = a.Y * b.Z - a.Z * b.Y;
- this->Y = a.Z * b.X - a.X * b.Z; // -a.X * b.Z + b.X * a.Z
- this->Z = a.X * b.Y - a.Y * b.X;
- }
- Vector3& Vector3::operator+=(const Vector3& Vector)
- {
- this->X += Vector.X;
- this->Y += Vector.Y;
- this->Z += Vector.Z;
- return *this;
- }
- Vector3& Vector3::operator-=(const Vector3& Vector)
- {
- this->X -= Vector.X;
- this->Y -= Vector.Y;
- this->Z -= Vector.Z;
- return *this;
- }
- Vector3& Vector3::operator*=(const Vector3& Vector)
- {
- this->X *= Vector.X;
- this->Y *= Vector.Y;
- this->Z *= Vector.Z;
- return *this;
- }
- Vector3& Vector3::operator/=(const Vector3& Vector)
- {
- this->X /= Vector.X;
- this->Y /= Vector.Y;
- this->Z /= Vector.Z;
- return *this;
- }
- Bool Vector3::operator==(const Scalar& Compare) const
- {
- return (FloatCloseEnough(this->X, Compare) && FloatCloseEnough(this->Y, Compare) && FloatCloseEnough(this->Z, Compare));
- }
- Bool Vector3::operator==(const Vector3& Vector) const
- {
- return (FloatCloseEnough(this->X, Vector.X) && FloatCloseEnough(this->Y, Vector.Y) && FloatCloseEnough(this->Z, Vector.Z));
- }
- Bool Vector3::operator!=(const Vector3& Vector) const
- {
- return !(*this == Vector);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement