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:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user