diff options
author | Paweł Redman <pawel.redman@gmail.com> | 2017-10-18 00:27:48 +0200 |
---|---|---|
committer | Paweł Redman <pawel.redman@gmail.com> | 2017-10-18 00:27:48 +0200 |
commit | ef5273d2c2ab801b11eb435a04bff96f6e778b1c (patch) | |
tree | 05c2a2097757a3d920eb3bb10fbf3e1bccceb938 /src/math.hpp | |
parent | 7d43c4e7ad6b83a23516b26b4aebf74f398c251b (diff) |
Continue refactoring.
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; + |