aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/api/Vector2.hpp
diff options
context:
space:
mode:
authorJAROWMR <jarorutjes07@gmail.com>2024-12-07 15:54:13 +0100
committerJAROWMR <jarorutjes07@gmail.com>2024-12-07 15:54:13 +0100
commita61149733e8661c97f15fc99e5b28489c29a2b22 (patch)
treef164cdb849097b6dffb4dcf093e2b9a1a28adaaf /src/crepe/api/Vector2.hpp
parentfdb4c99e139a264d4e15e6913a3756fc6cccb2f2 (diff)
parent33a072db28d71ba65e59f9491abd42dbf9695fc4 (diff)
merge AI
Diffstat (limited to 'src/crepe/api/Vector2.hpp')
-rw-r--r--src/crepe/api/Vector2.hpp48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/crepe/api/Vector2.hpp b/src/crepe/api/Vector2.hpp
index cad15f8..ff53cb0 100644
--- a/src/crepe/api/Vector2.hpp
+++ b/src/crepe/api/Vector2.hpp
@@ -1,5 +1,7 @@
#pragma once
+#include <cmath>
+
#include "Vector2.h"
namespace crepe {
@@ -115,4 +117,50 @@ bool Vector2<T>::operator!=(const Vector2<T> & other) const {
return !(*this == other);
}
+template <class T>
+void Vector2<T>::truncate(T max) {
+ if (length() > max) {
+ normalize();
+ *this *= max;
+ }
+}
+
+template <class T>
+void Vector2<T>::normalize() {
+ T len = length();
+ if (len > 0) {
+ *this /= len;
+ }
+}
+
+template <class T>
+T Vector2<T>::length() const {
+ return std::sqrt(x * x + y * y);
+}
+
+template <class T>
+T Vector2<T>::length_squared() const {
+ return x * x + y * y;
+}
+
+template <class T>
+T Vector2<T>::dot(const Vector2<T> & other) const {
+ return x * other.x + y * other.y;
+}
+
+template <class T>
+T Vector2<T>::distance(const Vector2<T> & other) const {
+ return (*this - other).length();
+}
+
+template <class T>
+T Vector2<T>::distance_squared(const Vector2<T> & other) const {
+ return (*this - other).length_squared();
+}
+
+template <class T>
+Vector2<T> Vector2<T>::perpendicular() const {
+ return {-y, x};
+}
+
} // namespace crepe