Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #8775 from leoetlino/rect
MathUtil: Fix Rectangle::GetWidth/Height for unsigned types
  • Loading branch information
leoetlino committed May 3, 2020
2 parents 920cf38 + 07ab79d commit 907b130
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions Source/Core/Common/MathUtil.h
Expand Up @@ -6,6 +6,7 @@

#include <algorithm>
#include <cmath>
#include <type_traits>
#include <vector>

#include "Common/CommonTypes.h"
Expand Down Expand Up @@ -71,8 +72,8 @@ struct Rectangle
return left == r.left && top == r.top && right == r.right && bottom == r.bottom;
}

T GetWidth() const { return std::abs(right - left); }
T GetHeight() const { return std::abs(bottom - top); }
constexpr T GetWidth() const { return GetDistance(left, right); }
constexpr T GetHeight() const { return GetDistance(top, bottom); }
// If the rectangle is in a coordinate system with a lower-left origin, use
// this Clamp.
void ClampLL(T x1, T y1, T x2, T y2)
Expand All @@ -92,6 +93,15 @@ struct Rectangle
top = std::clamp(top, y1, y2);
bottom = std::clamp(bottom, y1, y2);
}

private:
constexpr T GetDistance(T a, T b) const
{
if constexpr (std::is_unsigned<T>())
return b > a ? b - a : a - b;
else
return std::abs(b - a);
}
};

template <typename T>
Expand Down

0 comments on commit 907b130

Please sign in to comment.