From 2d07922478dac394a61045e6d9b76d89d98b4934 Mon Sep 17 00:00:00 2001 From: Gawaboumga Date: Fri, 21 Aug 2015 11:34:08 +0200 Subject: [PATCH] Add Unit for vector2 Former-commit-id: 4a143363f24e08d0be12a5ef6bfb46c71b11be4a --- include/Nazara/Math/Vector2.hpp | 4 ++-- include/Nazara/Math/Vector2.inl | 23 +++++++++++++++++++---- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/include/Nazara/Math/Vector2.hpp b/include/Nazara/Math/Vector2.hpp index c42700de5..24eea3806 100644 --- a/include/Nazara/Math/Vector2.hpp +++ b/include/Nazara/Math/Vector2.hpp @@ -27,12 +27,10 @@ class NzVector2 ~NzVector2() = default; T AbsDotProduct(const NzVector2& vec) const; - T AngleBetween(const NzVector2& vec) const; T Distance(const NzVector2& vec) const; float Distancef(const NzVector2& vec) const; - T DotProduct(const NzVector2& vec) const; T GetLength() const; @@ -40,6 +38,7 @@ class NzVector2 NzVector2 GetNormal(T* length = nullptr) const; T GetSquaredLength() const; + NzVector2& MakeUnit(); NzVector2& MakeUnitX(); NzVector2& MakeUnitY(); NzVector2& MakeZero(); @@ -89,6 +88,7 @@ class NzVector2 bool operator>=(const NzVector2& vec) const; static NzVector2 Lerp(const NzVector2& from, const NzVector2& to, T interpolation); + static NzVector2 Unit(); static NzVector2 UnitX(); static NzVector2 UnitY(); static NzVector2 Zero(); diff --git a/include/Nazara/Math/Vector2.inl b/include/Nazara/Math/Vector2.inl index 05a7ae792..c8ec7730f 100644 --- a/include/Nazara/Math/Vector2.inl +++ b/include/Nazara/Math/Vector2.inl @@ -105,6 +105,12 @@ T NzVector2::GetSquaredLength() const return x*x + y*y; } +template +NzVector2& NzVector2::MakeUnit() +{ + return Set(F(1.0), F(1.0)); +} + template NzVector2& NzVector2::MakeUnitX() { @@ -129,7 +135,7 @@ NzVector2& NzVector2::Maximize(const NzVector2& vec) if (vec.x > x) x = vec.x; - if (vec.y > y) + if (vec.y > y) y = vec.y; return *this; @@ -141,7 +147,7 @@ NzVector2& NzVector2::Minimize(const NzVector2& vec) if (vec.x < x) x = vec.x; - if (vec.y < y) + if (vec.y < y) y = vec.y; return *this; @@ -229,7 +235,7 @@ NzVector2& NzVector2::Set(const NzVector4& vec) template T NzVector2::SquaredDistance(const NzVector2& vec) const { - return operator-(vec).GetSquaredLength(); + return (*this - vec).GetSquaredLength(); } template @@ -398,7 +404,7 @@ template bool NzVector2::operator==(const NzVector2& vec) const { return NzNumberEquals(x, vec.x) && - NzNumberEquals(y, vec.y); + NzNumberEquals(y, vec.y); } template @@ -443,6 +449,15 @@ NzVector2 NzVector2::Lerp(const NzVector2& from, const NzVector2& to, T in return NzLerp(from, to, interpolation); } +template +NzVector2 NzVector2::Unit() +{ + NzVector2 vector; + vector.MakeUnit(); + + return vector; +} + template NzVector2 NzVector2::UnitX() {