termaze

maze generation and pathfinding visualizer
git clone git://git.yotsev.xyz/termaze.git
Log | Files | Refs | README | LICENSE

commit 8b1e1b2e345cfa4c11978f5021712f6ec0475d6d
parent b046bd446151662a49ae8d39f7e384ddb83b563f
Author: Petar Yotsev <petar@yotsev.xyz>
Date:   Thu, 24 Jun 2021 15:53:25 +0100

Update vec2 header library

Diffstat:
Mpathfinding.cpp | 12++++++------
Mvec2.hpp | 48++++++++++++++++++++++++++++++++++++++++--------
2 files changed, 46 insertions(+), 14 deletions(-)

diff --git a/pathfinding.cpp b/pathfinding.cpp @@ -304,12 +304,12 @@ std::vector<vec2<int>> pathfinding::AStar( current->g = 0.0f; finish = exits[0]; for (int i = 1; i < exits.size(); ++i) { - if ((exits[i] - current->pos).GetLength() - < (finish - current->pos).GetLength()) { + if ((exits[i] - current->pos).GetMagnitude() + < (finish - current->pos).GetMagnitude()) { finish = exits[i]; } } - current->h = (current->pos - finish).GetLength(); + current->h = (current->pos - finish).GetMagnitude(); current->f = current->g + current->h; while (!openSet.empty()) { @@ -378,14 +378,14 @@ std::vector<vec2<int>> pathfinding::AStar( finish = exits[0]; for (int i = 1; i < exits.size(); ++i) { - if ((exits[i] - neighbor->pos).GetLength() - < (finish - neighbor->pos).GetLength()) { + if ((exits[i] - neighbor->pos).GetMagnitude() + < (finish - neighbor->pos).GetMagnitude()) { finish = exits[i]; } } neighbor->g = g_temp; - neighbor->h = (neighbor->pos - finish).GetLength(); + neighbor->h = (neighbor->pos - finish).GetMagnitude(); neighbor->f = neighbor->h + neighbor->g; neighbor->parent = current; } diff --git a/vec2.hpp b/vec2.hpp @@ -27,10 +27,14 @@ struct vec2 { vec2& operator*=(float rhs); vec2 operator/(float rhs) const; vec2& operator/=(float rhs); - T GetLengthSq() const; - float GetLength() const; + T GetMagnitudeSq() const; + float GetMagnitude() const; vec2<T> GetNormalized() const; vec2<T>& Normalize(); + T GetMagnitudeSq(const vec2<T>& v) const; + float GetMagnitude(const vec2<T>& v) const; + vec2<T> GetNormalized(const vec2<T>& v) const; + void Normalize(vec2<T>& v); template <typename U> friend std::ostream& operator<<(std::ostream& os, const vec2<U>& v); @@ -107,23 +111,23 @@ vec2<T>& vec2<T>::operator/=(float rhs) } template <typename T> -T vec2<T>::GetLengthSq() const +T vec2<T>::GetMagnitudeSq() const { return (x * x) + (y * y); } template <typename T> -float vec2<T>::GetLength() const +float vec2<T>::GetMagnitude() const { - return std::sqrt(GetLengthSq()); + return std::sqrt(GetMagnitudeSq()); } template <typename T> vec2<T> vec2<T>::GetNormalized() const { - const float len = GetLength(); - if (len != 0.0f) { - return *this / len; + const float mag = GetMagnitude(); + if (mag != 0.0f) { + return *this / mag; } return *this; } @@ -135,6 +139,34 @@ vec2<T>& vec2<T>::Normalize() } template <typename T> +T vec2<T>::GetMagnitudeSq(const vec2<T>& v) const +{ + return (v.x * v.x) + (v.y * v.y); +} + +template <typename T> +float vec2<T>::GetMagnitude(const vec2<T>& v) const +{ + return std::sqrt(GetMagnitudeSq(v)); +} + +template <typename T> +vec2<T> vec2<T>::GetNormalized(const vec2<T>& v) const +{ + const float mag = GetMagnitude(v); + if (mag != 0.0f) { + return v / mag; + } + return v; +} + +template <typename T> +void vec2<T>::Normalize(vec2<T>& v) +{ + v = GetNormalized(v); +} + +template <typename T> std::ostream& operator<<(std::ostream& os, const vec2<T>& v) { return os << "(" << v.x << ", " << v.y << ")";