diff options
author | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-12-11 15:00:21 +0100 |
---|---|---|
committer | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-12-11 15:00:21 +0100 |
commit | e980f88c08e05d3c3d0ae7ff7422ec9e686ecfa4 (patch) | |
tree | dbf603db33c75e13cfeb255d8fcf789f4b2b82bc /src/crepe/api/Vector2.hpp | |
parent | a597b0059f30e9d56331c6ece34fdcabb4028616 (diff) | |
parent | 78c4a8772526f40c531b5402b56932b0a41e22e8 (diff) |
Merge branch 'master' into niels/UI
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 |