LibGfx: Add Size::match_aspect_ratio

This will create a size that matches the given aspect ratio while
preserving one of the two dimensions.
This commit is contained in:
kleines Filmröllchen 2022-10-21 17:05:20 +02:00 committed by Andrew Kaster
parent 5b5b4f57fa
commit af439cf3af
Notes: sideshowbarker 2024-07-17 04:09:07 +09:00

View file

@ -93,6 +93,25 @@ public:
return static_cast<float>(width()) / static_cast<float>(height());
}
// Horizontal means preserve the width, Vertical means preserve the height.
[[nodiscard]] constexpr Size<T> match_aspect_ratio(float aspect_ratio, Orientation side_to_preserve) const
{
VERIFY(aspect_ratio != 0.0f);
auto matched = *this;
auto height_corresponding_to_width = static_cast<T>(static_cast<float>(width()) / aspect_ratio);
auto width_corresponding_to_height = static_cast<T>(static_cast<float>(height()) * aspect_ratio);
switch (side_to_preserve) {
case Orientation::Vertical:
matched.m_width = width_corresponding_to_height;
break;
case Orientation::Horizontal:
matched.m_height = height_corresponding_to_width;
break;
}
return matched;
}
template<typename U>
[[nodiscard]] constexpr bool contains(Size<U> const& other) const
{