mirror of
https://github.com/orange-cpp/omath.git
synced 2026-02-13 07:03:25 +00:00
vec2 was made constexpr
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
|
||||
#pragma once
|
||||
#include <tuple>
|
||||
#include <cmath>
|
||||
|
||||
|
||||
namespace omath
|
||||
@@ -15,60 +16,158 @@ namespace omath
|
||||
float y = 0.f;
|
||||
|
||||
// Constructors
|
||||
Vector2() = default;
|
||||
Vector2(float x, float y);
|
||||
constexpr Vector2() : x(0.f), y(0.f) {}
|
||||
|
||||
constexpr Vector2(float x, float y) : x(x), y(y) {}
|
||||
|
||||
// Equality operators
|
||||
bool operator==(const Vector2& src) const;
|
||||
bool operator!=(const Vector2& src) const;
|
||||
constexpr bool operator==(const Vector2& src) const
|
||||
{
|
||||
return x == src.x && y == src.y;
|
||||
}
|
||||
|
||||
constexpr bool operator!=(const Vector2& src) const
|
||||
{
|
||||
return !(*this == src);
|
||||
}
|
||||
|
||||
// Compound assignment operators
|
||||
Vector2& operator+=(const Vector2& v);
|
||||
Vector2& operator-=(const Vector2& v);
|
||||
Vector2& operator*=(const Vector2& v);
|
||||
Vector2& operator/=(const Vector2& v);
|
||||
Vector2& operator+=(const Vector2& v)
|
||||
{
|
||||
x += v.x;
|
||||
y += v.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector2& operator*=(float fl);
|
||||
Vector2& operator/=(float fl);
|
||||
Vector2& operator+=(float fl);
|
||||
Vector2& operator-=(float fl);
|
||||
Vector2& operator-=(const Vector2& v)
|
||||
{
|
||||
x -= v.x;
|
||||
y -= v.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector2& operator*=(const Vector2& v)
|
||||
{
|
||||
x *= v.x;
|
||||
y *= v.y;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector2& operator/=(const Vector2& v)
|
||||
{
|
||||
x /= v.x;
|
||||
y /= v.y;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector2& operator*=(float fl)
|
||||
{
|
||||
x *= fl;
|
||||
y *= fl;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector2& operator/=(float fl)
|
||||
{
|
||||
x /= fl;
|
||||
y /= fl;
|
||||
return *this;
|
||||
}
|
||||
Vector2& operator+=(float fl)
|
||||
{
|
||||
x += fl;
|
||||
y += fl;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector2& operator-=(float fl)
|
||||
{
|
||||
x -= fl;
|
||||
y -= fl;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Basic vector operations
|
||||
[[nodiscard]] float DistTo(const Vector2& vOther) const;
|
||||
[[nodiscard]] float DistToSqr(const Vector2& vOther) const;
|
||||
[[nodiscard]] float Dot(const Vector2& vOther) const;
|
||||
[[nodiscard]] float DistTo(const Vector2& vOther) const
|
||||
{
|
||||
return std::sqrt(DistToSqr(vOther));
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr float DistToSqr(const Vector2& vOther) const
|
||||
{
|
||||
return (x - vOther.x) * (x - vOther.x) + (y - vOther.y) * (y - vOther.y);
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr float Dot(const Vector2& vOther) const
|
||||
{
|
||||
return x * vOther.x + y * vOther.y;
|
||||
}
|
||||
|
||||
[[nodiscard]] float Length() const;
|
||||
[[nodiscard]] float LengthSqr() const;
|
||||
Vector2& Abs();
|
||||
|
||||
[[nodiscard]] constexpr float LengthSqr() const
|
||||
{
|
||||
return x * x + y * y;
|
||||
}
|
||||
constexpr Vector2& Abs()
|
||||
{
|
||||
x = x < 0 ? -x : x;
|
||||
y = y < 0 ? -y : y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class type>
|
||||
const type& As() const
|
||||
constexpr const type& As() const
|
||||
{
|
||||
return *reinterpret_cast<const type*>(this);
|
||||
}
|
||||
template<class type>
|
||||
type& As()
|
||||
constexpr type& As()
|
||||
{
|
||||
return *reinterpret_cast<type*>(this);
|
||||
}
|
||||
|
||||
// Unary negation operator
|
||||
Vector2 operator-() const;
|
||||
constexpr Vector2 operator-() const
|
||||
{
|
||||
return {-x, -y};
|
||||
}
|
||||
|
||||
// Binary arithmetic operators
|
||||
Vector2 operator+(const Vector2& v) const;
|
||||
Vector2 operator-(const Vector2& v) const;
|
||||
Vector2 operator*(float fl) const;
|
||||
Vector2 operator/(float fl) const;
|
||||
constexpr Vector2 operator+(const Vector2& v) const
|
||||
{
|
||||
return {x + v.x, y + v.y};
|
||||
}
|
||||
|
||||
constexpr Vector2 operator-(const Vector2& v) const
|
||||
{
|
||||
return {x - v.x, y - v.y};
|
||||
}
|
||||
constexpr Vector2 operator*(float fl) const
|
||||
{
|
||||
return {x * fl, y * fl};
|
||||
}
|
||||
constexpr Vector2 operator/(float fl) const
|
||||
{
|
||||
return {x / fl, y / fl};
|
||||
}
|
||||
|
||||
|
||||
// Normalize the vector
|
||||
[[nodiscard]] Vector2 Normalized() const;
|
||||
|
||||
// Sum of elements
|
||||
[[nodiscard]] float Sum() const;
|
||||
[[nodiscard]] constexpr float Sum() const
|
||||
{
|
||||
return x + y;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
std::tuple<float, float> AsTuple() const;
|
||||
constexpr std::tuple<float, float> AsTuple() const
|
||||
{
|
||||
return std::make_tuple(x, y);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -7,157 +7,19 @@
|
||||
|
||||
namespace omath
|
||||
{
|
||||
// Constructors
|
||||
Vector2::Vector2(const float x, const float y) : x(x), y(y) {}
|
||||
|
||||
// Equality operators
|
||||
bool Vector2::operator==(const Vector2& src) const
|
||||
Vector2 Vector2::Normalized() const
|
||||
{
|
||||
return x == src.x && y == src.y;
|
||||
}
|
||||
const float len = Length();
|
||||
|
||||
bool Vector2::operator!=(const Vector2& src) const
|
||||
{
|
||||
return !(*this == src);
|
||||
}
|
||||
if (len > 0.f)
|
||||
return {x / len, y / len};
|
||||
|
||||
// Compound assignment operators
|
||||
Vector2& Vector2::operator+=(const Vector2& v)
|
||||
{
|
||||
x += v.x;
|
||||
y += v.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector2& Vector2::operator-=(const Vector2& v)
|
||||
{
|
||||
x -= v.x;
|
||||
y -= v.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector2& Vector2::operator*=(float fl)
|
||||
{
|
||||
x *= fl;
|
||||
y *= fl;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector2& Vector2::operator/=(float fl)
|
||||
{
|
||||
x /= fl;
|
||||
y /= fl;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector2& Vector2::operator+=(float fl)
|
||||
{
|
||||
x += fl;
|
||||
y += fl;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector2& Vector2::operator-=(float fl)
|
||||
{
|
||||
x -= fl;
|
||||
y -= fl;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Basic vector operations
|
||||
float Vector2::DistTo(const Vector2& vOther) const
|
||||
{
|
||||
return std::sqrt(DistToSqr(vOther));
|
||||
}
|
||||
|
||||
float Vector2::DistToSqr(const Vector2& vOther) const
|
||||
{
|
||||
return (x - vOther.x) * (x - vOther.x) + (y - vOther.y) * (y - vOther.y);
|
||||
}
|
||||
|
||||
float Vector2::Dot(const Vector2& vOther) const
|
||||
{
|
||||
return x * vOther.x + y * vOther.y;
|
||||
return {0.f, 0.f};
|
||||
}
|
||||
|
||||
float Vector2::Length() const
|
||||
{
|
||||
return std::sqrt(x * x + y * y);
|
||||
}
|
||||
|
||||
float Vector2::LengthSqr() const
|
||||
{
|
||||
return x * x + y * y;
|
||||
}
|
||||
|
||||
Vector2& Vector2::Abs()
|
||||
{
|
||||
x = std::abs(x);
|
||||
y = std::abs(y);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Unary negation operator
|
||||
Vector2 Vector2::operator-() const
|
||||
{
|
||||
return {-x, -y};
|
||||
}
|
||||
|
||||
// Binary arithmetic operators
|
||||
Vector2 Vector2::operator+(const Vector2& v) const
|
||||
{
|
||||
return {x + v.x, y + v.y};
|
||||
}
|
||||
|
||||
Vector2 Vector2::operator-(const Vector2& v) const
|
||||
{
|
||||
return {x - v.x, y - v.y};
|
||||
}
|
||||
|
||||
Vector2 Vector2::operator*(float fl) const
|
||||
{
|
||||
return {x * fl, y * fl};
|
||||
}
|
||||
|
||||
Vector2 Vector2::operator/(float fl) const
|
||||
{
|
||||
return {x / fl, y / fl};
|
||||
}
|
||||
|
||||
// Normalize the vector
|
||||
Vector2 Vector2::Normalized() const
|
||||
{
|
||||
float len = Length();
|
||||
if (len > 0.f) {
|
||||
return {x / len, y / len};
|
||||
}
|
||||
return {0.f, 0.f};
|
||||
}
|
||||
|
||||
// Sum of elements
|
||||
float Vector2::Sum() const
|
||||
{
|
||||
return x + y;
|
||||
}
|
||||
|
||||
Vector2 &Vector2::operator*=(const Vector2 &v)
|
||||
{
|
||||
x *= v.x;
|
||||
y *= v.y;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector2 &Vector2::operator/=(const Vector2 &v)
|
||||
{
|
||||
x /= v.x;
|
||||
y /= v.y;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::tuple<float, float> Vector2::AsTuple() const
|
||||
{
|
||||
return std::make_tuple(x, y);
|
||||
}
|
||||
}
|
||||
@@ -25,14 +25,14 @@ protected:
|
||||
// Test constructor and default values
|
||||
TEST_F(UnitTestVector2, Constructor_Default)
|
||||
{
|
||||
Vector2 v;
|
||||
constexpr Vector2 v;
|
||||
EXPECT_FLOAT_EQ(v.x, 0.0f);
|
||||
EXPECT_FLOAT_EQ(v.y, 0.0f);
|
||||
}
|
||||
|
||||
TEST_F(UnitTestVector2, Constructor_Values)
|
||||
{
|
||||
Vector2 v(1.0f, 2.0f);
|
||||
constexpr Vector2 v(1.0f, 2.0f);
|
||||
EXPECT_FLOAT_EQ(v.x, 1.0f);
|
||||
EXPECT_FLOAT_EQ(v.y, 2.0f);
|
||||
}
|
||||
@@ -40,14 +40,14 @@ TEST_F(UnitTestVector2, Constructor_Values)
|
||||
// Test equality operators
|
||||
TEST_F(UnitTestVector2, EqualityOperator)
|
||||
{
|
||||
Vector2 v3(1.0f, 2.0f);
|
||||
constexpr Vector2 v3(1.0f, 2.0f);
|
||||
EXPECT_TRUE(v1 == v3);
|
||||
EXPECT_FALSE(v1 == v2);
|
||||
}
|
||||
|
||||
TEST_F(UnitTestVector2, InequalityOperator)
|
||||
{
|
||||
Vector2 v3(1.0f, 2.0f);
|
||||
constexpr Vector2 v3(1.0f, 2.0f);
|
||||
EXPECT_FALSE(v1 != v3);
|
||||
EXPECT_TRUE(v1 != v2);
|
||||
}
|
||||
@@ -55,28 +55,28 @@ TEST_F(UnitTestVector2, InequalityOperator)
|
||||
// Test arithmetic operators
|
||||
TEST_F(UnitTestVector2, AdditionOperator)
|
||||
{
|
||||
Vector2 v3 = v1 + v2;
|
||||
constexpr Vector2 v3 = Vector2(1.0f, 2.0f) + Vector2(4.0f, 5.0f);
|
||||
EXPECT_FLOAT_EQ(v3.x, 5.0f);
|
||||
EXPECT_FLOAT_EQ(v3.y, 7.0f);
|
||||
}
|
||||
|
||||
TEST_F(UnitTestVector2, SubtractionOperator)
|
||||
{
|
||||
Vector2 v3 = v2 - v1;
|
||||
constexpr Vector2 v3 = Vector2(4.0f, 5.0f) - Vector2(1.0f, 2.0f);
|
||||
EXPECT_FLOAT_EQ(v3.x, 3.0f);
|
||||
EXPECT_FLOAT_EQ(v3.y, 3.0f);
|
||||
}
|
||||
|
||||
TEST_F(UnitTestVector2, MultiplicationOperator)
|
||||
{
|
||||
Vector2 v3 = v1 * 2.0f;
|
||||
constexpr Vector2 v3 = Vector2(1.0f, 2.0f) * 2.0f;
|
||||
EXPECT_FLOAT_EQ(v3.x, 2.0f);
|
||||
EXPECT_FLOAT_EQ(v3.y, 4.0f);
|
||||
}
|
||||
|
||||
TEST_F(UnitTestVector2, DivisionOperator)
|
||||
{
|
||||
Vector2 v3 = v2 / 2.0f;
|
||||
constexpr Vector2 v3 = Vector2(4.0f, 5.0f) / 2.0f;
|
||||
EXPECT_FLOAT_EQ(v3.x, 2.0f);
|
||||
EXPECT_FLOAT_EQ(v3.y, 2.5f);
|
||||
}
|
||||
@@ -112,59 +112,62 @@ TEST_F(UnitTestVector2, DivisionAssignmentOperator)
|
||||
|
||||
TEST_F(UnitTestVector2, NegationOperator)
|
||||
{
|
||||
Vector2 v3 = -v1;
|
||||
constexpr Vector2 v3 = -Vector2(1.0f, 2.0f);
|
||||
EXPECT_FLOAT_EQ(v3.x, -1.0f);
|
||||
EXPECT_FLOAT_EQ(v3.y, -2.0f);
|
||||
}
|
||||
|
||||
// Test other member functions
|
||||
TEST_F(UnitTestVector2, DistTo)
|
||||
{
|
||||
float dist = v1.DistTo(v2);
|
||||
EXPECT_FLOAT_EQ(dist, sqrt(18.0f));
|
||||
const float dist = v1.DistTo(v2);
|
||||
EXPECT_FLOAT_EQ(dist, std::sqrt(18.0f));
|
||||
}
|
||||
|
||||
TEST_F(UnitTestVector2, DistToSqr)
|
||||
{
|
||||
float distSqr = v1.DistToSqr(v2);
|
||||
constexpr float distSqr = Vector2(1.0f, 2.0f).DistToSqr(Vector2(4.0f, 5.0f));
|
||||
EXPECT_FLOAT_EQ(distSqr, 18.0f);
|
||||
}
|
||||
|
||||
TEST_F(UnitTestVector2, DotProduct)
|
||||
{
|
||||
float dot = v1.Dot(v2);
|
||||
constexpr float dot = Vector2(1.0f, 2.0f).Dot(Vector2(4.0f, 5.0f));
|
||||
EXPECT_FLOAT_EQ(dot, 14.0f);
|
||||
}
|
||||
|
||||
TEST_F(UnitTestVector2, Length)
|
||||
{
|
||||
float length = v1.Length();
|
||||
EXPECT_FLOAT_EQ(length, sqrt(5.0f));
|
||||
const float length = v1.Length();
|
||||
EXPECT_FLOAT_EQ(length, std::sqrt(5.0f));
|
||||
}
|
||||
|
||||
TEST_F(UnitTestVector2, LengthSqr)
|
||||
{
|
||||
float lengthSqr = v1.LengthSqr();
|
||||
constexpr float lengthSqr = Vector2(1.0f, 2.0f).LengthSqr();
|
||||
EXPECT_FLOAT_EQ(lengthSqr, 5.0f);
|
||||
}
|
||||
|
||||
TEST_F(UnitTestVector2, Abs)
|
||||
{
|
||||
Vector2 v3(-1.0f, -2.0f);
|
||||
v3.Abs();
|
||||
constexpr Vector2 v3 = Vector2(-1.0f, -2.0f).Abs();
|
||||
EXPECT_FLOAT_EQ(v3.x, 1.0f);
|
||||
EXPECT_FLOAT_EQ(v3.y, 2.0f);
|
||||
}
|
||||
|
||||
TEST_F(UnitTestVector2, Sum)
|
||||
{
|
||||
float sum = v1.Sum();
|
||||
constexpr float sum = Vector2(1.0f, 2.0f).Sum();
|
||||
EXPECT_FLOAT_EQ(sum, 3.0f);
|
||||
}
|
||||
|
||||
TEST_F(UnitTestVector2, Normalized)
|
||||
{
|
||||
Vector2 v3 = v1.Normalized();
|
||||
const Vector2 v3 = v1.Normalized();
|
||||
EXPECT_NEAR(v3.x, 0.44721f, 0.0001f);
|
||||
EXPECT_NEAR(v3.y, 0.89443f, 0.0001f);
|
||||
}
|
||||
|
||||
static_assert(Vector2(1.0f, 2.0f).LengthSqr() == 5.0f, "LengthSqr should be 5");
|
||||
static_assert(Vector2(1.0f, 2.0f).Dot(Vector2(4.0f, 5.0f)) == 14.0f, "Dot product should be 14");
|
||||
static_assert(Vector2(4.0f, 5.0f).DistToSqr(Vector2(1.0f, 2.0f)) == 18.0f, "DistToSqr should be 18");
|
||||
static_assert(Vector2(-1.0f, -2.0f).Abs() == Vector2(1.0f, 2.0f), "Abs should convert negative values to positive");
|
||||
|
||||
Reference in New Issue
Block a user