diff options
author | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-12-11 14:21:34 +0100 |
---|---|---|
committer | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-12-11 14:21:34 +0100 |
commit | e6a5bb579d0969245f34119ef2489f47039c523e (patch) | |
tree | e8b0b922cd28d6f83d7bf59ea1b69bbb84d0fd04 /src/crepe/api/Vector2.hpp | |
parent | 7b8de90699aea153e73b5f2cee05c69b966b81be (diff) | |
parent | 78c4a8772526f40c531b5402b56932b0a41e22e8 (diff) |
Merge branch 'master' into niels/remove_singleton
Diffstat (limited to 'src/crepe/api/Vector2.hpp')
-rw-r--r-- | src/crepe/api/Vector2.hpp | 48 |
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 |