Implement Vector2::RandomBox

This commit is contained in:
2024-07-10 14:13:14 -04:00
parent 27efa7da92
commit 2195752e1e
2 changed files with 21 additions and 1 deletions

View File

@@ -2,6 +2,7 @@
#include <cstddef>
#include <J3ML/Algorithm/RNG.hpp>
namespace J3ML::LinearAlgebra {
using namespace J3ML;
@@ -106,6 +107,16 @@ namespace J3ML::LinearAlgebra {
[[nodiscard]] bool IsZero(float epsilonSq = 1e-6f) const;
[[nodiscard]] bool IsPerpendicular(const Vector2& other, float epsilonSq=1e-5f) const;
/// Tests if two vectors are equal, up to the given epsilon.
/** @see IsPerpendicular(). */
bool Equals(const Vector2& rhs, float epsilon = 1e-3f) const {
return Math::EqualAbs(x, rhs.x, epsilon) &&
Math::EqualAbs(y, rhs.y, epsilon);
}
bool Equals(float x_, float y_, float epsilon = 1e-3f) const {
return Math::EqualAbs(x, x_, epsilon) &&
Math::EqualAbs(y, y_, epsilon);
}
bool operator == (const Vector2& rhs) const;
bool operator != (const Vector2& rhs) const;
@@ -369,7 +380,9 @@ namespace J3ML::LinearAlgebra {
@see Orthogonalize(), AreOrthogonal(), AreOrthonormal() */
static void Orthonormalize(Vector2& a, Vector2& b);
/// Returns a random Vector2 with each entry randomized between the range [minElem, maxElem].
static Vector2 RandomBox(Algorithm::RNG& rng, float minElem, float maxElem);
};
Vector2 operator*(float lhs, const Vector2 &rhs);
}
}

View File

@@ -461,6 +461,13 @@ namespace J3ML::LinearAlgebra {
b -= a.Dot(b) * a;
}
Vector2 Vector2::RandomBox(Algorithm::RNG &rng, float minElem, float maxElem) {
float x = rng.Float(minElem, maxElem);
float y = rng.Float(minElem, maxElem);
return {x, y};
}
bool Vector2::AreOrthogonal(const Vector2 &a, const Vector2 &b, float epsilon) {
return a.IsPerpendicular(b, epsilon);
}