MathUtil: Make Rectangle work with unsigned types.

This commit is contained in:
Tony Wasserka 2014-11-13 20:25:39 +01:00
parent c5c6e095f0
commit e02ff0121f

View File

@ -7,6 +7,7 @@
#include "common/common.h" #include "common/common.h"
#include <algorithm> #include <algorithm>
#include <type_traits>
#include <vector> #include <vector>
namespace MathUtil namespace MathUtil
@ -109,11 +110,11 @@ struct Rectangle
Rectangle(T theLeft, T theTop, T theRight, T theBottom) Rectangle(T theLeft, T theTop, T theRight, T theBottom)
: left(theLeft), top(theTop), right(theRight), bottom(theBottom) : left(theLeft), top(theTop), right(theRight), bottom(theBottom)
{ } { }
bool operator==(const Rectangle& r) { return left==r.left && top==r.top && right==r.right && bottom==r.bottom; } bool operator==(const Rectangle& r) { return left==r.left && top==r.top && right==r.right && bottom==r.bottom; }
T GetWidth() const { return abs(right - left); } T GetWidth() const { return std::abs(static_cast<typename std::make_signed<T>::type>(right - left)); }
T GetHeight() const { return abs(bottom - top); } T GetHeight() const { return std::abs(static_cast<typename std::make_signed<T>::type>(bottom - top)); }
// If the rectangle is in a coordinate system with a lower-left origin, use // If the rectangle is in a coordinate system with a lower-left origin, use
// this Clamp. // this Clamp.
@ -127,7 +128,7 @@ struct Rectangle
// If the rectangle is in a coordinate system with an upper-left origin, // If the rectangle is in a coordinate system with an upper-left origin,
// use this Clamp. // use this Clamp.
void ClampUL(T x1, T y1, T x2, T y2) void ClampUL(T x1, T y1, T x2, T y2)
{ {
if (left < x1) left = x1; if (left < x1) left = x1;
if (right > x2) right = x2; if (right > x2) right = x2;