diff options
Diffstat (limited to 'src/math.hpp')
-rw-r--r-- | src/math.hpp | 45 |
1 files changed, 36 insertions, 9 deletions
diff --git a/src/math.hpp b/src/math.hpp index b958092..f20cc99 100644 --- a/src/math.hpp +++ b/src/math.hpp @@ -44,6 +44,12 @@ public: return v[i]; } + void broadcast(T x) + { + for (size_t i = 0; i < N; i++) + v[i] = x; + } + // Comparison friend bool operator==(const vec_t<T, N> &a, const vec_t<T, N> &b) @@ -205,27 +211,48 @@ public: } }; -// Shorthands -typedef vec_t<float, 2> v2f_t; -typedef vec_t<double, 2> v2d_t; - template <typename T, size_t N> class rect_t { public: - vec_t<T, N> a, b; + vec_t<T, N> v[2]; rect_t() = default; - rect_t(vec_t<T, N> a_, vec_t<T, N> b_) + rect_t(vec_t<T, N> a, vec_t<T, N> b) { - a = a_; - b = b_; + v[0] = a; + v[1] = b; + } + + vec_t<T, N>& operator[](size_t i) + { + return v[i]; + } + + const vec_t<T, N>& operator[](size_t i) const + { + return v[i]; + } + + bool intersects(const rect_t<T, N> &b) const + { + for (size_t i = 0; i < N; i++) + if (v[1][i] < b[0][i] || v[0][i] > b[1][i]) + return false; + + return true; } friend std::ostream& operator<<(std::ostream& stream, rect_t<T, N> r) { - stream << "(" << r.a << ", " << r.b << ")"; + stream << "(" << r[0] << ", " << r[1] << ")"; return stream; } }; +// Shorthands +typedef vec_t<float, 2> v2f_t; +typedef vec_t<double, 2> v2d_t; +typedef rect_t<float, 2> rectf_t; +typedef rect_t<double, 2> rectd_t; + |