34 requires std::is_arithmetic_v<T>
41 constexpr operator U()
const noexcept
42 requires std::is_arithmetic_v<U>
44 return static_cast<U
>(value);
50 requires std::is_arithmetic_v<
decltype(obj.x)>;
51 requires std::is_arithmetic_v<
decltype(obj.y)>;
52 requires std::is_arithmetic_v<
decltype(obj.width)>;
53 requires std::is_arithmetic_v<
decltype(obj.height)>;
64 requires std::is_arithmetic_v<
decltype(obj.left)>;
65 requires std::is_arithmetic_v<
decltype(obj.top)>;
66 requires std::is_arithmetic_v<
decltype(obj.right)>;
67 requires std::is_arithmetic_v<
decltype(obj.bottom)>;
78 requires std::is_arithmetic_v<
decltype(obj.width)>;
79 requires std::is_arithmetic_v<
decltype(obj.height)>;
112 template <
typename T>
126 enum class rect_ratio_base {
145 template <
typename Number>
146 requires std::is_arithmetic_v<Number>
153 constexpr auto operator<=>(
const rect&)
const noexcept =
default;
155 constexpr Number right()
const noexcept {
159 constexpr Number bottom()
const noexcept {
163 constexpr Number area()
const noexcept {
164 return width * height;
171 template <
typename T>
172 constexpr operator T() const
176 return std::decay_t<T>{
188 template <
typename T>
189 constexpr operator T() const
193 return std::decay_t<T>{
206 template <w
idth_height_pair T>
219 template <w
idth_height_pair T>
229 [[nodiscard]]
constexpr bool collapsed() const noexcept {
230 return width <= std::numeric_limits<Number>::epsilon() || height <= std::numeric_limits<Number>::epsilon();
239 template <bottom_right_rect T>
255 template <equivalent_rect T>
273 template <
typename T>
274 requires std::is_arithmetic_v<T>
275 constexpr bool intersect_with(
const rect<T>& left,
const rect<T>& right)
noexcept {
276 return std::max(left.x, right.x) <= std::min(left.right(), right.right())
277 && std::max(left.y, right.y) <= std::min(left.bottom(), right.bottom());
287 template <
typename T>
288 requires std::is_arithmetic_v<T>
289 constexpr T calc_overlapped_area(
const rect<T>& left,
const rect<T>& right)
noexcept {
290 auto dx = std::min(left.right(), right.right()) - std::max(left.x, right.x);
291 auto dy = std::min(left.bottom(), right.bottom()) - std::max(left.y, right.y);
293 return dx >= 0 && dy >= 0 ? dx * dy : 0;
304 template <
typename T>
305 requires std::is_arithmetic_v<T>
306 constexpr double calc_overlapped_ratio(
307 const rect<T>& left,
const rect<T>& right, rect_ratio_base ratio_base)
noexcept {
308 auto area =
static_cast<double>(calc_overlapped_area(left, right));
310 switch (ratio_base) {
311 case rect_ratio_base::left:
312 return area / left.area();
313 case rect_ratio_base::right:
314 return area / right.area();
315 case rect_ratio_base::smaller:
316 return area / std::min(left.area(), right.area());
322 using recti = rect<std::int32_t>;
323 using rectf = rect<float>;
324 using rectd = rect<double>;
Illustrates a rectangle.
Definition rect.hpp:147
static constexpr rect from(const T &bottom_right_rect) noexcept
Makes a rectangle from a compatible rectangle that contains member variables x, y,...
Definition rect.hpp:240
static constexpr rect from(const T &equivalent_rect) noexcept
Makes a rectangle from an equivalent rectangle that contains member variables x, y,...
Definition rect.hpp:256
constexpr bool collapsed() const noexcept
Checks whether the area of the rectangle is zero (i.e. collapsed to a zero-dimensional point).
Definition rect.hpp:229
constexpr T to_extent() const noexcept(nothrow_width_height_pair< T >)
Retrieves the width-height pair.
Definition rect.hpp:207
constexpr void assign_from_extent(const T &pair) noexcept
Sets the width and height from a width-height pair.
Definition rect.hpp:220