mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 22:27:35 +00:00
LibGfx: Implement Rect::to_rounded<U>()
This replaces the usage of `rounded_int_rect`, whose name did not accurately reflect the rounding operation happening. For example, the position of the rect was not rounded but floored, and the size was pulled through `roundf` before casting to `int` which could result in inadvertent flooring if the resulting floating point could not exactly represent the rounded value.
This commit is contained in:
parent
bec474d99b
commit
ab19b092ed
5 changed files with 16 additions and 9 deletions
|
@ -1,6 +1,7 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
|
||||
* Copyright (c) 2022, Jelle Raaijmakers <jelle@gmta.nl>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
@ -696,6 +697,17 @@ public:
|
|||
return Rect<U>(*this);
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
[[nodiscard]] ALWAYS_INLINE Rect<U> to_rounded() const
|
||||
{
|
||||
return {
|
||||
static_cast<U>(llroundd(x())),
|
||||
static_cast<U>(llroundd(y())),
|
||||
static_cast<U>(llroundd(width())),
|
||||
static_cast<U>(llroundd(height())),
|
||||
};
|
||||
}
|
||||
|
||||
[[nodiscard]] String to_string() const;
|
||||
|
||||
private:
|
||||
|
@ -715,11 +727,6 @@ using FloatRect = Rect<float>;
|
|||
return Gfx::IntRect::from_two_points({ x1, y1 }, { x2, y2 });
|
||||
}
|
||||
|
||||
[[nodiscard]] ALWAYS_INLINE IntRect rounded_int_rect(FloatRect const& float_rect)
|
||||
{
|
||||
return IntRect { floorf(float_rect.x()), floorf(float_rect.y()), roundf(float_rect.width()), roundf(float_rect.height()) };
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace AK {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue