From 12e76bb3df17b3ba84248989cec36348347c6050 Mon Sep 17 00:00:00 2001 From: Zyper Date: Tue, 24 Aug 2021 23:39:07 +0200 Subject: [PATCH] LibGfx: Add method for calculating square aspect ratio end point --- Userland/Libraries/LibGfx/Point.cpp | 11 +++++++++++ Userland/Libraries/LibGfx/Point.h | 2 ++ 2 files changed, 13 insertions(+) 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::constrain(Rect const& rect) m_y = AK::clamp(y(), rect.top(), rect.bottom()); } +template +[[nodiscard]] Point Point::end_point_for_square_aspect_ratio(Point 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 [[nodiscard]] Point to_type() const {