Math/VectorI: Add modulo overloads

This commit is contained in:
SirLynix 2023-08-12 12:02:12 +02:00
parent 8291ced2eb
commit 1ef61cc5ad
6 changed files with 134 additions and 5 deletions

View File

@ -66,6 +66,8 @@ namespace Nz
constexpr Vector2 operator*(T scale) const;
constexpr Vector2 operator/(const Vector2& vec) const;
constexpr Vector2 operator/(T scale) const;
constexpr Vector2 operator%(const Vector2& vec) const;
constexpr Vector2 operator%(T mod) const;
constexpr Vector2& operator=(const Vector2&) = default;
constexpr Vector2& operator=(Vector2&&) = default;
@ -76,6 +78,8 @@ namespace Nz
constexpr Vector2& operator*=(T scale);
constexpr Vector2& operator/=(const Vector2& vec);
constexpr Vector2& operator/=(T scale);
constexpr Vector2& operator%=(const Vector2& vec);
constexpr Vector2& operator%=(T mod);
constexpr bool operator==(const Vector2& vec) const;
constexpr bool operator!=(const Vector2& vec) const;
@ -111,8 +115,9 @@ namespace Nz
template<typename T> std::ostream& operator<<(std::ostream& out, const Vector2<T>& vec);
template<typename T> constexpr Vector2<T> operator*(T scale, const Nz::Vector2<T>& vec);
template<typename T> constexpr Vector2<T> operator/(T scale, const Nz::Vector2<T>& vec);
template<typename T> constexpr Vector2<T> operator*(T scale, const Vector2<T>& vec);
template<typename T> constexpr Vector2<T> operator/(T scale, const Vector2<T>& vec);
template<typename T> constexpr Vector2<T> operator%(T mod, const Vector2<T>& vec);
}
namespace std

View File

@ -396,6 +396,18 @@ namespace Nz
return Vector2(x / scale, y / scale);
}
template<typename T>
constexpr Vector2<T> Vector2<T>::operator%(const Vector2& vec) const
{
return Vector2(Mod(x, vec.x), Mod(y, vec.y));
}
template<typename T>
constexpr Vector2<T> Vector2<T>::operator%(T mod) const
{
return Vector2(Mod(x, mod), Mod(y, mod));
}
/*!
* \brief Adds the components of other vector to this vector
* \return A reference to this vector where components are the sum of this vector and the other one
@ -486,6 +498,24 @@ namespace Nz
return *this;
}
template<typename T>
constexpr Vector2<T>& Vector2<T>::operator%=(const Vector2& vec)
{
x = Mod(x, vec.x);
y = Mod(y, vec.y);
return *this;
}
template<typename T>
constexpr Vector2<T>& Vector2<T>::operator%=(T value)
{
x = Mod(x, value);
y = Mod(y, value);
return *this;
}
/*!
* \brief Compares the vector to other one
* \return true if the vectors are the same
@ -724,6 +754,12 @@ namespace Nz
return Vector2<T>(scale / vec.x, scale / vec.y);
}
template<typename T>
constexpr Vector2<T> operator%(T mod, const Vector2<T>& vec)
{
return Vector2<T>(Mod(mod, vec.x), Mod(mod, vec.y));
}
/*!
* \brief Serializes a Vector2
* \return true if successfully serialized
@ -782,3 +818,4 @@ namespace std
}
#include <Nazara/Core/DebugOff.hpp>
#include "Vector2.hpp"

View File

@ -70,6 +70,8 @@ namespace Nz
constexpr Vector3 operator*(T scale) const;
constexpr Vector3 operator/(const Vector3& vec) const;
constexpr Vector3 operator/(T scale) const;
constexpr Vector3 operator%(const Vector3& vec) const;
constexpr Vector3 operator%(T mod) const;
constexpr Vector3& operator=(const Vector3&) = default;
constexpr Vector3& operator=(Vector3&&) = default;
@ -80,6 +82,8 @@ namespace Nz
constexpr Vector3& operator*=(T scale);
constexpr Vector3& operator/=(const Vector3& vec);
constexpr Vector3& operator/=(T scale);
constexpr Vector3& operator%=(const Vector3& vec);
constexpr Vector3& operator%=(T mod);
constexpr bool operator==(const Vector3& vec) const;
constexpr bool operator!=(const Vector3& vec) const;
@ -129,6 +133,7 @@ namespace Nz
template<typename T> constexpr Vector3<T> operator*(T scale, const Vector3<T>& vec);
template<typename T> constexpr Vector3<T> operator/(T scale, const Vector3<T>& vec);
template<typename T> constexpr Vector3<T> operator%(T scale, const Vector3<T>& vec);
}
namespace std

View File

@ -464,6 +464,18 @@ namespace Nz
return Vector3(x / scale, y / scale, z / scale);
}
template<typename T>
constexpr Vector3<T> Vector3<T>::operator%(const Vector3& vec) const
{
return Vector3(Mod(x, vec.x), Mod(y, vec.y), Mod(z, vec.z));
}
template<typename T>
constexpr Vector3<T> Vector3<T>::operator%(T mod) const
{
return Vector3(Mod(x, mod), Mod(y, mod), Mod(z, mod));
}
/*!
* \brief Adds the components of other vector to this vector
* \return A reference to this vector where components are the sum of this vector and the other one
@ -560,6 +572,26 @@ namespace Nz
return *this;
}
template<typename T>
constexpr Vector3<T>& Vector3<T>::operator%=(const Vector3& vec)
{
x = Mod(x, vec.x);
y = Mod(y, vec.y);
z = Mod(z, vec.z);
return *this;
}
template<typename T>
constexpr Vector3<T>& Vector3<T>::operator%=(T mod)
{
x = Mod(x, mod);
y = Mod(y, mod);
z = Mod(z, mod);
return *this;
}
/*!
* \brief Compares the vector to other one
* \return true if the vectors are the same
@ -976,6 +1008,12 @@ namespace Nz
{
return Vector3<T>(scale / vec.x, scale / vec.y, scale / vec.z);
}
template<typename T>
constexpr Vector3<T> operator%(T mod, const Vector3<T>& vec)
{
return Vector3<T>(Mod(mod, vec.x), Mod(mod, vec.y), Mod(mod, vec.z));
}
}
namespace std
@ -989,7 +1027,6 @@ namespace std
*
* \param v Vector3 to hash
*/
std::size_t operator()(const Nz::Vector3<T>& v) const
{
return Nz::HashCombine(v.x, v.y, v.z);

View File

@ -66,6 +66,8 @@ namespace Nz
constexpr Vector4 operator*(T scale) const;
constexpr Vector4 operator/(const Vector4& vec) const;
constexpr Vector4 operator/(T scale) const;
constexpr Vector4 operator%(const Vector4& vec) const;
constexpr Vector4 operator%(T mod) const;
constexpr Vector4& operator+=(const Vector4& vec);
constexpr Vector4& operator-=(const Vector4& vec);
@ -73,6 +75,8 @@ namespace Nz
constexpr Vector4& operator*=(T scale);
constexpr Vector4& operator/=(const Vector4& vec);
constexpr Vector4& operator/=(T scale);
constexpr Vector4& operator%=(const Vector4& vec);
constexpr Vector4& operator%=(T mod);
constexpr bool operator==(const Vector4& vec) const;
constexpr bool operator!=(const Vector4& vec) const;
@ -109,6 +113,7 @@ namespace Nz
template<typename T> constexpr Vector4<T> operator*(T scale, const Vector4<T>& vec);
template<typename T> constexpr Vector4<T> operator/(T scale, const Vector4<T>& vec);
template<typename T> constexpr Vector4<T> operator%(T mod, const Vector4<T>& vec);
}
namespace std

View File

@ -400,6 +400,18 @@ namespace Nz
return Vector4(x / scale, y / scale, z / scale, w / scale);
}
template<typename T>
constexpr Vector4<T> Vector4<T>::operator%(const Vector4& vec) const
{
return Vector4(Mod(x, vec.x), Mod(y, vec.y), Mod(z, vec.z), Mod(w, vec.w));
}
template<typename T>
constexpr Vector4<T> Vector4<T>::operator%(T mod) const
{
return Vector4(Mod(x, mod), Mod(y, mod), Mod(z, mod), Mod(z, mod));
}
/*!
* \brief Adds the components of other vector to this vector
* \return A reference to this vector where components are the sum of this vector and the other one
@ -501,6 +513,28 @@ namespace Nz
return *this;
}
template<typename T>
constexpr Vector4<T>& Vector4<T>::operator%=(const Vector4& vec)
{
x = Mod(x, vec.x);
y = Mod(y, vec.y);
z = Mod(z, vec.z);
w = Mod(w, vec.w);
return *this;
}
template<typename T>
constexpr Vector4<T>& Vector4<T>::operator%=(T mod)
{
x = Mod(x, mod);
y = Mod(y, mod);
z = Mod(z, mod);
w = Mod(w, mod);
return *this;
}
/*!
* \brief Compares the vector to other one
@ -781,7 +815,7 @@ namespace Nz
template<typename T>
constexpr Vector4<T> operator*(T scale, const Vector4<T>& vec)
{
return Nz::Vector4<T>(scale * vec.x, scale * vec.y, scale * vec.z, scale * vec.w);
return Vector4<T>(scale * vec.x, scale * vec.y, scale * vec.z, scale * vec.w);
}
/*!
@ -793,7 +827,13 @@ namespace Nz
template<typename T>
constexpr Vector4<T> operator/(T scale, const Vector4<T>& vec)
{
return Nz::Vector4<T>(scale / vec.x, scale / vec.y, scale / vec.z, scale / vec.w);
return Vector4<T>(scale / vec.x, scale / vec.y, scale / vec.z, scale / vec.w);
}
template<typename T>
constexpr Vector4<T> operator%(T mod, const Vector4<T>& vec)
{
return Vector4<T>(Mod(mod, vec.x), Mod(mod, vec.y), Mod(mod, vec.z), Mod(mod, vec.w));
}
}