common/math_util: Support float type rectangles

This commit is contained in:
ReinUsesLisp 2019-12-26 01:42:31 -03:00
parent c7db1ef565
commit e38ed26b98

View file

@ -24,17 +24,29 @@ struct Rectangle {
: left(left), top(top), right(right), bottom(bottom) {} : left(left), top(top), right(right), bottom(bottom) {}
T GetWidth() const { T GetWidth() const {
return std::abs(static_cast<std::make_signed_t<T>>(right - left)); if constexpr (std::is_floating_point_v<T>) {
return std::abs(right - left);
} else {
return std::abs(static_cast<std::make_signed_t<T>>(right - left));
}
} }
T GetHeight() const { T GetHeight() const {
return std::abs(static_cast<std::make_signed_t<T>>(bottom - top)); if constexpr (std::is_floating_point_v<T>) {
return std::abs(bottom - top);
} else {
return std::abs(static_cast<std::make_signed_t<T>>(bottom - top));
}
} }
Rectangle<T> TranslateX(const T x) const { Rectangle<T> TranslateX(const T x) const {
return Rectangle{left + x, top, right + x, bottom}; return Rectangle{left + x, top, right + x, bottom};
} }
Rectangle<T> TranslateY(const T y) const { Rectangle<T> TranslateY(const T y) const {
return Rectangle{left, top + y, right, bottom + y}; return Rectangle{left, top + y, right, bottom + y};
} }
Rectangle<T> Scale(const float s) const { Rectangle<T> Scale(const float s) const {
return Rectangle{left, top, static_cast<T>(left + GetWidth() * s), return Rectangle{left, top, static_cast<T>(left + GetWidth() * s),
static_cast<T>(top + GetHeight() * s)}; static_cast<T>(top + GetHeight() * s)};