Initial Vector2Of implementation

This commit is contained in:
Pascal Serrarens 2025-12-19 12:32:34 +01:00
parent 5c6f1a4169
commit 9886f98f6b
3 changed files with 177 additions and 11 deletions

View File

@ -180,3 +180,93 @@ Vector2 Vector2::Lerp(const Vector2& v1, const Vector2& v2, float f) {
Vector2 v = v1 + (v2 - v1) * f; Vector2 v = v1 + (v2 - v1) * f;
return v; return v;
} }
#pragma region Vector2Of
template <typename T>
Vector2Of<T>::Vector2Of() {}
template <typename T>
Vector2Of<T>::Vector2Of(T horizontal, T vertical)
: horizontal(horizontal), vertical(vertical) {}
template <typename T>
const Vector2Of<T> Vector2Of<T>::zero = Vector2Of(T{}, T{});
template <typename T>
float Vector2Of<T>::MagnitudeOf(const Vector2Of& v) {
T sqr = v.horizontal * v.horizontal + v.vertical * v.vertical;
float sqrFloat = static_cast<float>(sqr);
return sqrtf(sqrFloat);
}
template <typename T>
float LinearAlgebra::Vector2Of<T>::Magnitude() const {
T sqr = this->horizontal * this->horizontal + this->vertical * this->vertical;
float sqrFloat = static_cast<float>(sqr);
return sqrtf(sqrFloat);
}
template <typename T>
Vector2Of<T> Vector2Of<T>::operator-(const Vector2Of& v) const {
return Vector2Of(this->horizontal - v.horizontal,
this->vertical - v.vertical);
}
template <typename T>
Vector2Of<T> Vector2Of<T>::operator-=(const Vector2Of& v) {
this->horizontal -= v.horizontal;
this->vertical -= v.vertical;
return *this;
}
template <typename T>
Vector2Of<T> Vector2Of<T>::operator+(const Vector2Of& v) const {
return Vector2Of(this->horizontal + v.horizontal,
this->vertical + v.vertical);
}
template <typename T>
Vector2Of<T> Vector2Of<T>::operator+=(const Vector2Of& v) {
this->horizontal += v.horizontal;
this->vertical += v.vertical;
return *this;
}
// template <typename T>
// Vector2Of<float> Vector2Of<T>::operator/=(float f) {
// this->x /= f;
// this->y /= f;
// return *this;
// }
template <typename T>
float Vector2Of<T>::Distance(const Vector2Of& v1, const Vector2Of& v2) {
return MagnitudeOf(v1 - v2);
}
template <typename T>
Vector2 Vector2Of<T>::Normalize(const Vector2Of<T>& v) {
float num = Vector2Of<T>::MagnitudeOf(v);
Vector2 result = Vector2::zero;
if (num > Float::epsilon) {
result = v / num;
}
return result;
}
template <typename T>
Vector2 Vector2Of<T>::normalized() const {
float num = Vector2Of<T>::MagnitudeOf(*this);
Vector2 result = Vector2::zero;
if (num > Float::epsilon) {
result = *this / num;
}
return result;
}
// Explicit instantiation for int
template class Vector2Of<int>;
template class Vector2Of<float>;
#pragma endregion Vector2Of

View File

@ -138,9 +138,7 @@ struct Vector2 : Vec2 {
/// @return The scaled vector /// @return The scaled vector
/// @remark Each component of the vector will be multipled with the same /// @remark Each component of the vector will be multipled with the same
/// factor f. /// factor f.
friend Vector2 operator*(const Vector2& v, float f) { friend Vector2 operator*(const Vector2& v, float f) { return Vector2(v.x * f, v.y * f); }
return Vector2(v.x * f, v.y * f);
}
friend Vector2 operator*(float f, const Vector2& v) { friend Vector2 operator*(float f, const Vector2& v) {
return Vector2(v.x * f, v.y * f); return Vector2(v.x * f, v.y * f);
// return Vector2(f * v.x, f * v.y); // return Vector2(f * v.x, f * v.y);
@ -150,12 +148,8 @@ struct Vector2 : Vec2 {
/// @param f The scaling factor /// @param f The scaling factor
/// @return The scaled vector /// @return The scaled vector
/// @remark Each componet of the vector will be divided by the same factor. /// @remark Each componet of the vector will be divided by the same factor.
friend Vector2 operator/(const Vector2& v, float f) { friend Vector2 operator/(const Vector2& v, float f) { return Vector2(v.x / f, v.y / f); }
return Vector2(v.x / f, v.y / f); friend Vector2 operator/(float f, const Vector2& v) { return Vector2(f / v.x, f / v.y); }
}
friend Vector2 operator/(float f, const Vector2& v) {
return Vector2(f / v.x, f / v.y);
}
Vector2 operator/=(float f); Vector2 operator/=(float f);
/// @brief The dot product of two vectors /// @brief The dot product of two vectors
@ -201,9 +195,90 @@ struct Vector2 : Vec2 {
static Vector2 Lerp(const Vector2& v1, const Vector2& v2, float f); static Vector2 Lerp(const Vector2& v1, const Vector2& v2, float f);
}; };
template <typename T>
class Vector2Of {
public:
/// @brief The distance in the horizontal direction, left = negative, right = positive
T horizontal = T{};
/// @brief The distance in the vertical direction, down = negative, up = positive
T vertical = T{};
/// @brief A new 2-dimensional zero vector
Vector2Of();
/// @brief A new 2-dimensional vector
/// @param horizontal The distance in the horizontal direction, left = negative, right =
/// positive
/// @param vertical The distance in the vertical direction, down = negative, up =
/// positive
Vector2Of(T horizontal, T vertical);
/// @brief Converting constructor: allow Vector2Of<U> -> Vector2Of<T>
/// @tparam U
/// @param other
template <typename U>
constexpr Vector2Of(const Vector2Of<U>& other) noexcept
: horizontal(static_cast<T>(other.horizontal)), vertical(static_cast<T>(other.vertical)) {}
template <typename U>
constexpr Vector2Of& operator=(const Vector2Of<U>& other) noexcept {
this.horizontal = static_cast<T>(other.horizontal);
this.vertical = static_cast<T>(other.vertical);
}
/// @brief A vector with zero for all axis
const static Vector2Of zero;
/// @brief The vector length
/// @param v The vector for which you need the length
/// @return The vector length
static float MagnitudeOf(const Vector2Of& v);
/// @brief The vector length
/// @return The vector length
float Magnitude() const;
/// @brief Subtract a vector from this vector
/// @param v The vector to subtract from this vector
/// @return The result of the subtraction
Vector2Of operator-(const Vector2Of& v) const;
Vector2Of operator-=(const Vector2Of& v);
/// @brief Add a vector to this vector
/// @param v The vector to add to this vector
/// @return The result of the addition
Vector2Of operator+(const Vector2Of& v) const;
Vector2Of operator+=(const Vector2Of& v);
/// @brief Scale the vector uniformly down
/// @param f The scaling factor
/// @return The scaled vector
/// @remark Each componet of the vector will be divided by the same factor.
// operator/ for Vector2Of<int> is provided as a free function below (inside namespace)
// Vector2Of<float> operator/=(float f);
/// @brief The distance between two vectors
/// @param v1 The first vector
/// @param v2 The second vector
/// @return The distance between the two vectors
static float Distance(const Vector2Of& v1, const Vector2Of& v2);
static Vector2 Normalize(const Vector2Of& v);
/// @brief Convert the vector to a length 1
/// @return The vector normalized to a length of 1
Vector2 normalized() const;
};
//template class Vector2Of<float>;
using Vector2Int = Vector2Of<int>;
using Vector2Float = Vector2Of<float>;
template <typename T>
inline Vector2 operator/(const Vector2Of<T>& v, float f) {
return Vector2(v.horizontal / f, v.vertical / f);
}
} // namespace LinearAlgebra } // namespace LinearAlgebra
using namespace LinearAlgebra; using namespace LinearAlgebra;
#include "Polar.h"
#endif #endif

View File

@ -4,6 +4,7 @@
#include <math.h> #include <math.h>
#include "Vector2.h" #include "Vector2.h"
#include "Polar.h"
#define FLOAT_INFINITY std::numeric_limits<float>::infinity() #define FLOAT_INFINITY std::numeric_limits<float>::infinity()