Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // .h
- class Vector2
- {
- public:
- float X, Y;
- Vector2();
- Vector2(float x, float y);
- };
- class Vector3
- {
- public:
- float X, Y, Z;
- Vector3();
- Vector3(float x, float y, float z);
- float *Convert();
- Vector3 Cross(const Vector3 vec);
- Vector3 Scale(float scale);
- Vector3 Lerp(Vector3 end, float fraction);
- Vector3 Negate();
- Vector3 operator()(float x, float y, float z);
- bool operator==(const Vector3 vec);
- bool operator!=(const Vector3 vec);
- Vector3 operator+(const Vector3 vec);
- Vector3 operator+(const float value);
- Vector3 operator-(const Vector3 vec);
- Vector3 operator-(const float value);
- Vector3 operator*(const Vector3 vec);
- Vector3 operator*(const float value);
- Vector3 operator/(const float value);
- Vector3 operator=(const float* value);
- Vector3 operator*=(const Vector3 vec);
- Vector3 operator*=(const float value);
- Vector3 operator+=(const Vector3 vec);
- Vector3 operator-=(const Vector3 vec);
- Vector3 operator-=(const float value);
- float GetLength() const;
- float GetMagnitude() const;
- float DistanceTo(Vector3 pos);
- float Dot(Vector3& vec);
- void Copy(Vector3 result);
- void Normalize();
- void NormalizeAngles();
- void Zero();
- };
- // .cpp
- Vector2::Vector2()
- {
- X = Y = 0;
- }
- Vector2::Vector2(float x, float y)
- {
- X = x;
- Y = y;
- }
- Vector3::Vector3()
- {
- X = Y = Z = 0;
- }
- Vector3::Vector3(float x, float y, float z)
- {
- X = x;
- Y = y;
- Z = z;
- }
- float *Vector3::Convert()
- {
- float pos[3] = { X, Y, Z };
- return pos;
- }
- Vector3 Vector3::Cross(const Vector3 vec)
- {
- return Vector3(Y * vec.Z - Z * vec.Y, Z * vec.X - X * vec.Z, X * vec.Y - Y * vec.X);
- }
- Vector3 Vector3::Scale(float scale)
- {
- return (*this * scale);
- }
- Vector3 Vector3::Lerp(Vector3 end, float fraction)
- {
- return (((end - *this) * fraction) + *this);
- }
- Vector3 Vector3::Negate()
- {
- return Vector3(-X, -Y, -Z);
- }
- Vector3 Vector3::operator()(float x, float y, float z)
- {
- return Vector3(X + x, Y + y, Z + z);
- }
- bool Vector3::operator==(const Vector3 vec)
- {
- return (X == vec.X && Y == vec.Y && Z == vec.Z);
- }
- bool Vector3::operator!=(const Vector3 vec)
- {
- return !(X == vec.X && Y == vec.Y && Z == vec.Z);
- }
- Vector3 Vector3::operator+(const Vector3 vec)
- {
- return Vector3(X + vec.X, Y + vec.Y, Z + vec.Z);
- }
- Vector3 Vector3::operator+(const float value)
- {
- return Vector3(X + value, Y + value, Z + value);
- }
- Vector3 Vector3::operator-(const Vector3 vec)
- {
- return Vector3(X - vec.X, Y - vec.Y, Z - vec.Z);
- }
- Vector3 Vector3::operator-(const float value)
- {
- return Vector3(X - value, Y - value, Z - value);
- }
- Vector3 Vector3::operator*(const Vector3 vec)
- {
- return Vector3(X * vec.X, Y * vec.Y, Z * vec.Z);
- }
- Vector3 Vector3::operator*(const float value)
- {
- return Vector3(X * value, Y * value, Z * value);
- }
- Vector3 Vector3::operator/(const float value)
- {
- return Vector3(X / value, Y / value, Z / value);
- }
- Vector3 Vector3::operator=(const float* value)
- {
- return *(Vector3*)&value[0];
- }
- Vector3 Vector3::operator*=(const Vector3 vec)
- {
- return Vector3(X *= vec.X, Y *= vec.Y, Z *= vec.Z);
- }
- Vector3 Vector3::operator*=(const float value)
- {
- return Vector3(X *= value, Y *= value, Z *= value);
- }
- Vector3 Vector3::operator+=(const Vector3 vec)
- {
- return Vector3(X += vec.X, Y += vec.Y, Z += vec.Z);
- }
- Vector3 Vector3::operator-=(const Vector3 vec)
- {
- return Vector3(X -= vec.X, Y -= vec.Y, Z -= vec.Z);
- }
- Vector3 Vector3::operator-=(const float value)
- {
- return Vector3(X -= value, Y -= value, Z -= value);
- }
- float Vector3::GetLength() const
- {
- return sqrt((X * X) + (Y * Y) + (Z * Z));
- }
- float Vector3::GetMagnitude() const
- {
- return sqrtf(X * X + Y * Y + Z * Z);
- }
- float Vector3::DistanceTo(Vector3 pos)
- {
- float x = pos.X - X, y = pos.Y - Y, z = pos.Z - Z;
- return sqrt((x * x) + (y * y) + (z * z));
- }
- float Vector3::Dot(Vector3& vec)
- {
- return (X * vec.X) + (Y * vec.Y) + (Z * vec.Z);
- }
- void Vector3::Copy(Vector3 result)
- {
- memcpy(&this[0], &result, 12);
- }
- void Vector3::Normalize()
- {
- float length = GetLength();
- if (length == 0.0f) {
- *this = Vector3();
- return;
- }
- *this *= (length /= 1.0f);
- }
- void Vector3::NormalizeAngles()
- {
- if (X > 180.0f)
- X -= 160.0f;
- if (X < -180.0f)
- X += 360.0f;
- if (Y > 180.0f)
- Y -= 160.0f;
- if (Y < -180.0f)
- Y += 360.0f;
- }
- void Vector3::Zero()
- {
- X = Y = Z = 0;
- }
Add Comment
Please, Sign In to add comment