added vec2

This commit is contained in:
2024-09-02 16:14:03 +03:00
parent a40bf98763
commit de7ef015dd
6 changed files with 277 additions and 62 deletions

74
include/omath/Vector2.h Normal file
View File

@@ -0,0 +1,74 @@
//
// Created by Vlad on 02.09.2024.
//
#pragma once
#include <tuple>
namespace omath
{
class Vector2
{
public:
float x = 0.f;
float y = 0.f;
// Constructors
Vector2() = default;
Vector2(float x, float y);
// Equality operators
bool operator==(const Vector2& src) const;
bool operator!=(const Vector2& src) const;
// 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*=(float fl);
Vector2& operator/=(float fl);
Vector2& operator+=(float fl);
Vector2& operator-=(float fl);
// 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 Length() const;
[[nodiscard]] float LengthSqr() const;
Vector2& Abs();
template<class type>
const type& As() const
{
return *reinterpret_cast<const type*>(this);
}
template<class type>
type& As()
{
return *reinterpret_cast<type*>(this);
}
// Unary negation operator
Vector2 operator-() const;
// 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;
// Normalize the vector
[[nodiscard]] Vector2 Normalized() const;
// Sum of elements
[[nodiscard]] float Sum() const;
[[nodiscard]]
std::tuple<float, float> AsTuple() const;
};
}

View File

@@ -6,20 +6,16 @@
#include <cstdint>
#include <functional>
#include "omath/Vector2.h"
namespace omath
{
class Vector3 {
class Vector3 : public Vector2
{
public:
float x = 0.f;
float y = 0.f;
float z = 0.f;
Vector3(const float x, const float y, const float z)
{
this->x = x;
this->y = y;
this->z = z;
}
Vector3(float x, float y, float z);
Vector3() = default;
bool operator==(const Vector3& src) const;
@@ -50,16 +46,6 @@ namespace omath
Vector3 operator/(float fl) const;
Vector3 operator/(const Vector3& v) const;
template<class type>
const type& As() const
{
return *reinterpret_cast<const type*>(this);
}
template<class type>
type& As()
{
return *reinterpret_cast<type*>(this);
}
[[nodiscard]] Vector3 Cross(const Vector3 &v) const;
[[nodiscard]] static Vector3 CreateVelocity(float pitch, float yaw, float speed);
@@ -74,6 +60,8 @@ namespace omath
[[nodiscard]]
Vector3 Normalized() const;
[[nodiscard]] std::tuple<float, float, float> AsTuple() const;
};
}
// ReSharper disable once CppRedundantNamespaceDefinition