mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-23 14:19:46 -06:00
Merge pull request #8775 from leoetlino/rect
MathUtil: Fix Rectangle::GetWidth/Height for unsigned types
This commit is contained in:
@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
#include <type_traits>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "Common/CommonTypes.h"
|
#include "Common/CommonTypes.h"
|
||||||
@ -71,8 +72,8 @@ struct Rectangle
|
|||||||
return left == r.left && top == r.top && right == r.right && bottom == r.bottom;
|
return left == r.left && top == r.top && right == r.right && bottom == r.bottom;
|
||||||
}
|
}
|
||||||
|
|
||||||
T GetWidth() const { return std::abs(right - left); }
|
constexpr T GetWidth() const { return GetDistance(left, right); }
|
||||||
T GetHeight() const { return std::abs(bottom - top); }
|
constexpr T GetHeight() const { return GetDistance(top, bottom); }
|
||||||
// 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.
|
||||||
void ClampLL(T x1, T y1, T x2, T y2)
|
void ClampLL(T x1, T y1, T x2, T y2)
|
||||||
@ -92,6 +93,15 @@ struct Rectangle
|
|||||||
top = std::clamp(top, y1, y2);
|
top = std::clamp(top, y1, y2);
|
||||||
bottom = std::clamp(bottom, 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>
|
template <typename T>
|
||||||
|
Reference in New Issue
Block a user