diff options
Diffstat (limited to 'src/common/math_util.h')
| -rw-r--r-- | src/common/math_util.h | 50 |
1 files changed, 45 insertions, 5 deletions
diff --git a/src/common/math_util.h b/src/common/math_util.h index 510c4e56d..54485bf53 100644 --- a/src/common/math_util.h +++ b/src/common/math_util.h | |||
| @@ -4,6 +4,7 @@ | |||
| 4 | 4 | ||
| 5 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include <algorithm> | ||
| 7 | #include <cstdlib> | 8 | #include <cstdlib> |
| 8 | #include <type_traits> | 9 | #include <type_traits> |
| 9 | 10 | ||
| @@ -20,10 +21,32 @@ struct Rectangle { | |||
| 20 | 21 | ||
| 21 | constexpr Rectangle() = default; | 22 | constexpr Rectangle() = default; |
| 22 | 23 | ||
| 24 | constexpr Rectangle(T width, T height) : right(width), bottom(height) {} | ||
| 25 | |||
| 23 | constexpr Rectangle(T left_, T top_, T right_, T bottom_) | 26 | constexpr Rectangle(T left_, T top_, T right_, T bottom_) |
| 24 | : left(left_), top(top_), right(right_), bottom(bottom_) {} | 27 | : left(left_), top(top_), right(right_), bottom(bottom_) {} |
| 25 | 28 | ||
| 26 | [[nodiscard]] T GetWidth() const { | 29 | [[nodiscard]] constexpr T Left() const { |
| 30 | return left; | ||
| 31 | } | ||
| 32 | |||
| 33 | [[nodiscard]] constexpr T Top() const { | ||
| 34 | return top; | ||
| 35 | } | ||
| 36 | |||
| 37 | [[nodiscard]] constexpr T Right() const { | ||
| 38 | return right; | ||
| 39 | } | ||
| 40 | |||
| 41 | [[nodiscard]] constexpr T Bottom() const { | ||
| 42 | return bottom; | ||
| 43 | } | ||
| 44 | |||
| 45 | [[nodiscard]] constexpr bool IsEmpty() const { | ||
| 46 | return (GetWidth() <= 0) || (GetHeight() <= 0); | ||
| 47 | } | ||
| 48 | |||
| 49 | [[nodiscard]] constexpr T GetWidth() const { | ||
| 27 | if constexpr (std::is_floating_point_v<T>) { | 50 | if constexpr (std::is_floating_point_v<T>) { |
| 28 | return std::abs(right - left); | 51 | return std::abs(right - left); |
| 29 | } else { | 52 | } else { |
| @@ -31,7 +54,7 @@ struct Rectangle { | |||
| 31 | } | 54 | } |
| 32 | } | 55 | } |
| 33 | 56 | ||
| 34 | [[nodiscard]] T GetHeight() const { | 57 | [[nodiscard]] constexpr T GetHeight() const { |
| 35 | if constexpr (std::is_floating_point_v<T>) { | 58 | if constexpr (std::is_floating_point_v<T>) { |
| 36 | return std::abs(bottom - top); | 59 | return std::abs(bottom - top); |
| 37 | } else { | 60 | } else { |
| @@ -39,18 +62,35 @@ struct Rectangle { | |||
| 39 | } | 62 | } |
| 40 | } | 63 | } |
| 41 | 64 | ||
| 42 | [[nodiscard]] Rectangle<T> TranslateX(const T x) const { | 65 | [[nodiscard]] constexpr Rectangle<T> TranslateX(const T x) const { |
| 43 | return Rectangle{left + x, top, right + x, bottom}; | 66 | return Rectangle{left + x, top, right + x, bottom}; |
| 44 | } | 67 | } |
| 45 | 68 | ||
| 46 | [[nodiscard]] Rectangle<T> TranslateY(const T y) const { | 69 | [[nodiscard]] constexpr Rectangle<T> TranslateY(const T y) const { |
| 47 | return Rectangle{left, top + y, right, bottom + y}; | 70 | return Rectangle{left, top + y, right, bottom + y}; |
| 48 | } | 71 | } |
| 49 | 72 | ||
| 50 | [[nodiscard]] Rectangle<T> Scale(const float s) const { | 73 | [[nodiscard]] constexpr Rectangle<T> Scale(const float s) const { |
| 51 | return Rectangle{left, top, static_cast<T>(static_cast<float>(left + GetWidth()) * s), | 74 | return Rectangle{left, top, static_cast<T>(static_cast<float>(left + GetWidth()) * s), |
| 52 | static_cast<T>(static_cast<float>(top + GetHeight()) * s)}; | 75 | static_cast<T>(static_cast<float>(top + GetHeight()) * s)}; |
| 53 | } | 76 | } |
| 77 | |||
| 78 | [[nodiscard]] constexpr bool operator==(const Rectangle<T>& rhs) const { | ||
| 79 | return (left == rhs.left) && (top == rhs.top) && (right == rhs.right) && | ||
| 80 | (bottom == rhs.bottom); | ||
| 81 | } | ||
| 82 | |||
| 83 | [[nodiscard]] constexpr bool operator!=(const Rectangle<T>& rhs) const { | ||
| 84 | return !operator==(rhs); | ||
| 85 | } | ||
| 86 | |||
| 87 | [[nodiscard]] constexpr bool Intersect(const Rectangle<T>& with, Rectangle<T>* result) const { | ||
| 88 | result->left = std::max(left, with.left); | ||
| 89 | result->top = std::max(top, with.top); | ||
| 90 | result->right = std::min(right, with.right); | ||
| 91 | result->bottom = std::min(bottom, with.bottom); | ||
| 92 | return !result->IsEmpty(); | ||
| 93 | } | ||
| 54 | }; | 94 | }; |
| 55 | 95 | ||
| 56 | template <typename T> | 96 | template <typename T> |