diff options
| author | max-001 <maxsmits21@kpnmail.nl> | 2024-12-04 16:50:23 +0100 | 
|---|---|---|
| committer | max-001 <maxsmits21@kpnmail.nl> | 2024-12-04 16:50:23 +0100 | 
| commit | 94d95cb13e76d6cd3ec892a7f0b2bab938a9ba6a (patch) | |
| tree | 19d3d33f6a24dca691be7f349ed6e69eae81142b /src | |
| parent | f9f5600b60d6944dc9a7dd502988703d59d0cd62 (diff) | |
Extended Vector2
Diffstat (limited to 'src')
| -rw-r--r-- | src/crepe/api/Vector2.h | 24 | ||||
| -rw-r--r-- | src/crepe/api/Vector2.hpp | 48 | ||||
| -rw-r--r-- | src/test/Vector2Test.cpp | 148 | 
3 files changed, 220 insertions, 0 deletions
| diff --git a/src/crepe/api/Vector2.h b/src/crepe/api/Vector2.h index c278c87..bbcb932 100644 --- a/src/crepe/api/Vector2.h +++ b/src/crepe/api/Vector2.h @@ -66,6 +66,30 @@ struct Vector2 {  	//! Checks if this vector is not equal to another vector.  	bool operator!=(const Vector2<T> & other) const; + +	//! Truncates the vector to a maximum length. +	void truncate(T max); + +	//! Normalizes the vector. +	void normalize(); + +	//! Returns the length of the vector. +	T length() const; + +	//! Returns the squared length of the vector. +	T length_squared() const; + +	//! Returns the dot product of this vector and another vector. +	T dot(const Vector2<T> & other) const; + +	//! Returns the distance between this vector and another vector. +	T distance(const Vector2<T> & other) const; + +	//! Returns the squared distance between this vector and another vector. +	T distance_squared(const Vector2<T> & other) const; + +	//! Returns the perpendicular vector to this vector. +	Vector2 perpendicular() const;  };  } // namespace crepe 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 diff --git a/src/test/Vector2Test.cpp b/src/test/Vector2Test.cpp index 17bca41..1e21af9 100644 --- a/src/test/Vector2Test.cpp +++ b/src/test/Vector2Test.cpp @@ -382,3 +382,151 @@ TEST_F(Vector2Test, NotEquals) {  	EXPECT_FALSE(long_vec1 != long_vec1);  	EXPECT_TRUE(long_vec1 != long_vec2);  } + +TEST_F(Vector2Test, Truncate) { +	Vector2<int> vec = {3, 4}; +	vec.truncate(3); +	EXPECT_EQ(vec.x, 0); +	EXPECT_EQ(vec.y, 0); + +	Vector2<double> vec2 = {3.0, 4.0}; +	vec2.truncate(3.0); +	EXPECT_FLOAT_EQ(vec2.x, 1.8); +	EXPECT_FLOAT_EQ(vec2.y, 2.4); + +	Vector2<long> vec3 = {3, 4}; +	vec3.truncate(3); +	EXPECT_EQ(vec3.x, 0); +	EXPECT_EQ(vec3.y, 0); + +	Vector2<float> vec4 = {3.0f, 4.0f}; +	vec4.truncate(3.0f); +	EXPECT_FLOAT_EQ(vec4.x, 1.8f); +	EXPECT_FLOAT_EQ(vec4.y, 2.4f); +} + +TEST_F(Vector2Test, Normalize) { +	Vector2<int> vec = {3, 4}; +	vec.normalize(); +	EXPECT_EQ(vec.x, 0); +	EXPECT_EQ(vec.y, 0); + +	Vector2<double> vec2 = {3.0, 4.0}; +	vec2.normalize(); +	EXPECT_FLOAT_EQ(vec2.x, 0.6); +	EXPECT_FLOAT_EQ(vec2.y, 0.8); + +	Vector2<long> vec3 = {3, 4}; +	vec3.normalize(); +	EXPECT_EQ(vec3.x, 0); +	EXPECT_EQ(vec3.y, 0); + +	Vector2<float> vec4 = {3.0f, 4.0f}; +	vec4.normalize(); +	EXPECT_FLOAT_EQ(vec4.x, 0.6f); +	EXPECT_FLOAT_EQ(vec4.y, 0.8f); +} + +TEST_F(Vector2Test, Length) { +	Vector2<int> vec = {3, 4}; +	EXPECT_EQ(vec.length(), 5); + +	Vector2<double> vec2 = {3.0, 4.0}; +	EXPECT_FLOAT_EQ(vec2.length(), 5.0); + +	Vector2<long> vec3 = {3, 4}; +	EXPECT_EQ(vec3.length(), 5); + +	Vector2<float> vec4 = {3.0f, 4.0f}; +	EXPECT_FLOAT_EQ(vec4.length(), 5.0f); +} + +TEST_F(Vector2Test, LengthSquared) { +	Vector2<int> vec = {3, 4}; +	EXPECT_EQ(vec.length_squared(), 25); + +	Vector2<double> vec2 = {3.0, 4.0}; +	EXPECT_FLOAT_EQ(vec2.length_squared(), 25.0); + +	Vector2<long> vec3 = {3, 4}; +	EXPECT_EQ(vec3.length_squared(), 25); + +	Vector2<float> vec4 = {3.0f, 4.0f}; +	EXPECT_FLOAT_EQ(vec4.length_squared(), 25.0f); +} + +TEST_F(Vector2Test, Dot) { +	Vector2<int> vec1 = {3, 4}; +	Vector2<int> vec2 = {5, 6}; +	EXPECT_EQ(vec1.dot(vec2), 39); + +	Vector2<double> vec3 = {3.0, 4.0}; +	Vector2<double> vec4 = {5.0, 6.0}; +	EXPECT_FLOAT_EQ(vec3.dot(vec4), 39.0); + +	Vector2<long> vec5 = {3, 4}; +	Vector2<long> vec6 = {5, 6}; +	EXPECT_EQ(vec5.dot(vec6), 39); + +	Vector2<float> vec7 = {3.0f, 4.0f}; +	Vector2<float> vec8 = {5.0f, 6.0f}; +	EXPECT_FLOAT_EQ(vec7.dot(vec8), 39.0f); +} + +TEST_F(Vector2Test, Distance) { +	Vector2<int> vec1 = {1, 1}; +	Vector2<int> vec2 = {4, 5}; +	EXPECT_EQ(vec1.distance(vec2), 5); + +	Vector2<double> vec3 = {1.0, 1.0}; +	Vector2<double> vec4 = {4.0, 5.0}; +	EXPECT_FLOAT_EQ(vec3.distance(vec4), 5.0); + +	Vector2<long> vec5 = {1, 1}; +	Vector2<long> vec6 = {4, 5}; +	EXPECT_EQ(vec5.distance(vec6), 5); + +	Vector2<float> vec7 = {1.0f, 1.0f}; +	Vector2<float> vec8 = {4.0f, 5.0f}; +	EXPECT_FLOAT_EQ(vec7.distance(vec8), 5.0f); +} + +TEST_F(Vector2Test, DistanceSquared) { +	Vector2<int> vec1 = {3, 4}; +	Vector2<int> vec2 = {5, 6}; +	EXPECT_EQ(vec1.distance_squared(vec2), 8); + +	Vector2<double> vec3 = {3.0, 4.0}; +	Vector2<double> vec4 = {5.0, 6.0}; +	EXPECT_FLOAT_EQ(vec3.distance_squared(vec4), 8.0); + +	Vector2<long> vec5 = {3, 4}; +	Vector2<long> vec6 = {5, 6}; +	EXPECT_EQ(vec5.distance_squared(vec6), 8); + +	Vector2<float> vec7 = {3.0f, 4.0f}; +	Vector2<float> vec8 = {5.0f, 6.0f}; +	EXPECT_FLOAT_EQ(vec7.distance_squared(vec8), 8.0f); +} + +TEST_F(Vector2Test, Perpendicular) { +	Vector2<int> vec = {3, 4}; +	Vector2<int> result = vec.perpendicular(); +	EXPECT_EQ(result.x, -4); +	EXPECT_EQ(result.y, 3); + +	Vector2<double> vec2 = {3.0, 4.0}; +	Vector2<double> result2 = vec2.perpendicular(); +	EXPECT_FLOAT_EQ(result2.x, -4.0); +	EXPECT_FLOAT_EQ(result2.y, 3.0); + +	Vector2<long> vec3 = {3, 4}; +	Vector2<long> result3 = vec3.perpendicular(); +	EXPECT_EQ(result3.x, -4); +	EXPECT_EQ(result3.y, 3); + +	Vector2<float> vec4 = {3.0f, 4.0f}; +	Vector2<float> result4 = vec4.perpendicular(); +	EXPECT_FLOAT_EQ(result4.x, -4.0f); +	EXPECT_FLOAT_EQ(result4.y, 3.0f); +} |