From ef5273d2c2ab801b11eb435a04bff96f6e778b1c Mon Sep 17 00:00:00 2001 From: Paweł Redman Date: Wed, 18 Oct 2017 00:27:48 +0200 Subject: Continue refactoring. --- src/math.hpp | 45 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) (limited to 'src/math.hpp') 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 &a, const vec_t &b) @@ -205,27 +211,48 @@ public: } }; -// Shorthands -typedef vec_t v2f_t; -typedef vec_t v2d_t; - template class rect_t { public: - vec_t a, b; + vec_t v[2]; rect_t() = default; - rect_t(vec_t a_, vec_t b_) + rect_t(vec_t a, vec_t b) { - a = a_; - b = b_; + v[0] = a; + v[1] = b; + } + + vec_t& operator[](size_t i) + { + return v[i]; + } + + const vec_t& operator[](size_t i) const + { + return v[i]; + } + + bool intersects(const rect_t &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 r) { - stream << "(" << r.a << ", " << r.b << ")"; + stream << "(" << r[0] << ", " << r[1] << ")"; return stream; } }; +// Shorthands +typedef vec_t v2f_t; +typedef vec_t v2d_t; +typedef rect_t rectf_t; +typedef rect_t rectd_t; + -- cgit