diff options
author | Zyper <zyperpl@gmail.com> | 2021-08-24 23:39:07 +0200 |
---|---|---|
committer | Brian Gianforcaro <b.gianfo@gmail.com> | 2021-09-15 00:10:14 +0000 |
commit | 12e76bb3df17b3ba84248989cec36348347c6050 (patch) | |
tree | 4eecfa96e742ff065c7e6d63e955ead5001f038f /Userland/Libraries | |
parent | 88a31f3bac665218d401f299fe66314c7e4a9e0d (diff) | |
download | serenity-12e76bb3df17b3ba84248989cec36348347c6050.zip |
LibGfx: Add method for calculating square aspect ratio end point
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibGfx/Point.cpp | 11 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Point.h | 2 |
2 files changed, 13 insertions, 0 deletions
diff --git a/Userland/Libraries/LibGfx/Point.cpp b/Userland/Libraries/LibGfx/Point.cpp index aaa2563362..4add10a72f 100644 --- a/Userland/Libraries/LibGfx/Point.cpp +++ b/Userland/Libraries/LibGfx/Point.cpp @@ -19,6 +19,17 @@ void Point<T>::constrain(Rect<T> const& rect) m_y = AK::clamp<T>(y(), rect.top(), rect.bottom()); } +template<typename T> +[[nodiscard]] Point<T> Point<T>::end_point_for_square_aspect_ratio(Point<T> const& previous_end_point) const +{ + const T dx = previous_end_point.x() - x(); + const T dy = previous_end_point.y() - y(); + const T x_sign = dx >= 0 ? 1 : -1; + const T y_sign = dy >= 0 ? 1 : -1; + const T abs_size = AK::max(AK::abs(dx), AK::abs(dy)); + return { x() + x_sign * abs_size, y() + y_sign * abs_size }; +} + template<> String IntPoint::to_string() const { diff --git a/Userland/Libraries/LibGfx/Point.h b/Userland/Libraries/LibGfx/Point.h index 588bf0716a..f569d83131 100644 --- a/Userland/Libraries/LibGfx/Point.h +++ b/Userland/Libraries/LibGfx/Point.h @@ -231,6 +231,8 @@ public: return { AK::abs(dx_relative_to(other)), AK::abs(dy_relative_to(other)) }; } + [[nodiscard]] Point end_point_for_square_aspect_ratio(Point const&) const; + template<typename U> [[nodiscard]] Point<U> to_type() const { |