summaryrefslogtreecommitdiff
path: root/lib/maths.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/maths.h')
-rw-r--r--lib/maths.h19
1 files changed, 19 insertions, 0 deletions
diff --git a/lib/maths.h b/lib/maths.h
index 68b45da..2999d01 100644
--- a/lib/maths.h
+++ b/lib/maths.h
@@ -8,6 +8,7 @@
#include <glm/gtc/constants.hpp>
#include <numeric>
#include <optional>
+#include <ranges>
#include <stdexcept>
#include <utility>
@@ -288,6 +289,15 @@ sq(T value)
return value * value;
}
+template<glm::length_t L, typename T, glm::qualifier Q>
+auto
+vectorMagSquared(const glm::vec<L, T, Q> & val)
+{
+ return std::ranges::fold_left(std::views::iota(0, L), T {}, [&val](auto total, auto axis) {
+ return total + sq(val[axis]);
+ });
+}
+
template<glm::qualifier Q = glm::defaultp>
constexpr glm::vec<3, int64_t, Q>
crossProduct(const glm::vec<3, int64_t, Q> & valueA, const glm::vec<3, int64_t, Q> & valueB)
@@ -403,6 +413,15 @@ linesIntersectAt(const glm::vec<2, T, Q> Aabs, const glm::vec<2, T, Q> Babs, con
return Aabs + CVec {(b1 * c2) / -determinant, (a1 * c2) / determinant};
}
+template<std::floating_point T> constexpr auto EPSILON = 0.0001F;
+
+template<std::floating_point T>
+auto
+isWithinLimit(T lhs, T rhs, T limit = EPSILON<T>)
+{
+ return std::abs(lhs - rhs) <= limit;
+}
+
template<Arithmetic T, glm::qualifier Q = glm::defaultp>
std::pair<glm::vec<2, T, Q>, bool>
find_arc_centre(glm::vec<2, T, Q> start, Rotation2D startDir, glm::vec<2, T, Q> end, Rotation2D endDir)