diff --git a/include/uml/Vector3.h b/include/uml/Vector3.h index 3bc9bc3..06f6507 100644 --- a/include/uml/Vector3.h +++ b/include/uml/Vector3.h @@ -6,8 +6,7 @@ namespace uml { - class Vector3 final - { + class Vector3 { public: float x = 0.f; float y = 0.f; diff --git a/include/uml/Vector4.h b/include/uml/Vector4.h new file mode 100644 index 0000000..82f5bff --- /dev/null +++ b/include/uml/Vector4.h @@ -0,0 +1,42 @@ +// +// Vector4.h +// +#pragma once + +#include + +namespace uml +{ + class Vector4 final : public Vector3 + { + public: + float w = 0.f; + + Vector4(float x = 0.f, float y = 0.f, float z = 0.f, float w = 0.f) : Vector3(x, y, z), w(w) {} + Vector4() = default; + + bool operator==(const Vector4& src) const; + bool operator!=(const Vector4& src) const; + + Vector4& operator+=(const Vector4& v); + Vector4& operator-=(const Vector4& v); + Vector4& operator*=(float scalar); + Vector4& operator*=(const Vector4& v); + Vector4& operator/=(float scalar); + Vector4& operator/=(const Vector4& v); + + [[nodiscard]] float Length() const; + [[nodiscard]] float LengthSqr() const; + [[nodiscard]] float Dot(const Vector4& vOther) const; + Vector4& Abs(); + Vector4 operator-() const; + Vector4 operator+(const Vector4& v) const; + Vector4 operator-(const Vector4& v) const; + Vector4 operator*(float scalar) const; + Vector4 operator*(const Vector4& v) const; + Vector4 operator/(float scalar) const; + Vector4 operator/(const Vector4& v) const; + + [[nodiscard]] float Sum() const; + }; +} diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 7b64eb9..80bd636 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -3,4 +3,5 @@ target_sources(uml PRIVATE matrix.cpp angles.cpp ProjectilePredictor.cpp - color.cpp) \ No newline at end of file + color.cpp + Vector4.cpp) \ No newline at end of file diff --git a/source/Vector4.cpp b/source/Vector4.cpp new file mode 100644 index 0000000..9d3c3f2 --- /dev/null +++ b/source/Vector4.cpp @@ -0,0 +1,123 @@ +// +// Vector4.cpp +// + +#include "Vector4.h" +#include + +namespace uml +{ + bool Vector4::operator==(const Vector4& src) const + { + return Vector3::operator==(src) && w == src.w; + } + + bool Vector4::operator!=(const Vector4& src) const + { + return !(*this == src); + } + + Vector4& Vector4::operator+=(const Vector4& v) + { + Vector3::operator+=(v); + w += v.w; + return *this; + } + + Vector4& Vector4::operator-=(const Vector4& v) + { + Vector3::operator-=(v); + w -= v.w; + return *this; + } + + Vector4& Vector4::operator*=(float scalar) + { + Vector3::operator*=(scalar); + w *= scalar; + return *this; + } + + Vector4& Vector4::operator*=(const Vector4& v) + { + Vector3::operator*=(v); + w *= v.w; + return *this; + } + + Vector4& Vector4::operator/=(float scalar) + { + Vector3::operator/=(scalar); + w /= scalar; + return *this; + } + + Vector4& Vector4::operator/=(const Vector4& v) + { + Vector3::operator/=(v); + w /= v.w; + return *this; + } + + float Vector4::Length() const + { + return std::sqrt(LengthSqr()); + } + + float Vector4::LengthSqr() const + { + return Vector3::LengthSqr() + w * w; + } + + float Vector4::Dot(const Vector4& vOther) const + { + return Vector3::Dot(vOther) + w * vOther.w; + } + + Vector4& Vector4::Abs() + { + Vector3::Abs(); + w = std::abs(w); + return *this; + } + + Vector4 Vector4::operator-() const + { + return Vector4(-x, -y, -z, -w); + } + + Vector4 Vector4::operator+(const Vector4& v) const + { + return Vector4(x + v.x, y + v.y, z + v.z, w + v.w); + } + + Vector4 Vector4::operator-(const Vector4& v) const + { + return Vector4(x - v.x, y - v.y, z - v.z, w - v.w); + } + + Vector4 Vector4::operator*(float scalar) const + { + return Vector4(x * scalar, y * scalar, z * scalar, w * scalar); + } + + Vector4 Vector4::operator*(const Vector4& v) const + { + return Vector4(x * v.x, y * v.y, z * v.z, w * v.w); + } + + Vector4 Vector4::operator/(float scalar) const + { + return Vector4(x / scalar, y / scalar, z / scalar, w / scalar); + } + + Vector4 Vector4::operator/(const Vector4& v) const + { + return Vector4(x / v.x, y / v.y, z / v.z, w / v.w); + } + + float Vector4::Sum() const + { + return x + y + z + w; + } +}