diff options
author | Mustafa Quraish <mustafaq9@gmail.com> | 2021-09-15 19:34:50 -0400 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-09-17 11:54:13 +0200 |
commit | f14c891ba545f39091a2b86206c6505be1cb6fd4 (patch) | |
tree | e8217953bb95f8ce13a4326f3d6ecb6d6d190ba7 | |
parent | 5c244a789309e2360ccfce06aa0dc7b461b3796c (diff) | |
download | serenity-f14c891ba545f39091a2b86206c6505be1cb6fd4.zip |
LibGfx+PixelPaint: Add `Point::end_point_for_aspect_ratio` method
Previously we only had `Point::end_point_for_square_aspect_ratio`,
which was convenient for PixelPaint but assumed the aspect ratio
was always fixed at 1. This patch replaces it with a new mthod that
takes in an arbitrary aspect ratio and computes the end point based
off that.
There's some explicit casting going on in `Point.cpp` to ensure that
the types line up, since we're templating Point based on `T`.`
-rw-r--r-- | Userland/Applications/PixelPaint/EllipseTool.cpp | 2 | ||||
-rw-r--r-- | Userland/Applications/PixelPaint/RectangleTool.cpp | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Point.cpp | 19 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Point.h | 2 |
4 files changed, 15 insertions, 10 deletions
diff --git a/Userland/Applications/PixelPaint/EllipseTool.cpp b/Userland/Applications/PixelPaint/EllipseTool.cpp index 6acb156b35..74816e1afe 100644 --- a/Userland/Applications/PixelPaint/EllipseTool.cpp +++ b/Userland/Applications/PixelPaint/EllipseTool.cpp @@ -90,7 +90,7 @@ void EllipseTool::on_mousemove(Layer*, MouseEvent& event) m_draw_mode = event.layer_event().alt() ? DrawMode::FromCenter : DrawMode::FromCorner; if (event.layer_event().shift()) - m_ellipse_end_position = m_ellipse_start_position.end_point_for_square_aspect_ratio(event.layer_event().position()); + m_ellipse_end_position = m_ellipse_start_position.end_point_for_aspect_ratio(event.layer_event().position(), 1.0); else m_ellipse_end_position = event.layer_event().position(); diff --git a/Userland/Applications/PixelPaint/RectangleTool.cpp b/Userland/Applications/PixelPaint/RectangleTool.cpp index a5fdde2923..6ff2b57d98 100644 --- a/Userland/Applications/PixelPaint/RectangleTool.cpp +++ b/Userland/Applications/PixelPaint/RectangleTool.cpp @@ -96,7 +96,7 @@ void RectangleTool::on_mousemove(Layer* layer, MouseEvent& event) m_draw_mode = event.layer_event().alt() ? DrawMode::FromCenter : DrawMode::FromCorner; if (event.layer_event().shift()) - m_rectangle_end_position = m_rectangle_start_position.end_point_for_square_aspect_ratio(event.layer_event().position()); + m_rectangle_end_position = m_rectangle_start_position.end_point_for_aspect_ratio(event.layer_event().position(), 1.0); else m_rectangle_end_position = event.layer_event().position(); diff --git a/Userland/Libraries/LibGfx/Point.cpp b/Userland/Libraries/LibGfx/Point.cpp index 4add10a72f..c91c980735 100644 --- a/Userland/Libraries/LibGfx/Point.cpp +++ b/Userland/Libraries/LibGfx/Point.cpp @@ -20,14 +20,19 @@ void Point<T>::constrain(Rect<T> const& rect) } template<typename T> -[[nodiscard]] Point<T> Point<T>::end_point_for_square_aspect_ratio(Point<T> const& previous_end_point) const +[[nodiscard]] Point<T> Point<T>::end_point_for_aspect_ratio(Point<T> const& previous_end_point, float aspect_ratio) 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 }; + VERIFY(aspect_ratio > 0); + const T x_sign = previous_end_point.x() >= x() ? 1 : -1; + const T y_sign = previous_end_point.y() >= y() ? 1 : -1; + T dx = AK::abs(previous_end_point.x() - x()); + T dy = AK::abs(previous_end_point.y() - y()); + if (dx > dy) { + dy = (T)((float)dx / aspect_ratio); + } else { + dx = (T)((float)dy * aspect_ratio); + } + return { x() + x_sign * dx, y() + y_sign * dy }; } template<> diff --git a/Userland/Libraries/LibGfx/Point.h b/Userland/Libraries/LibGfx/Point.h index 44776a4d7b..7b9faebe16 100644 --- a/Userland/Libraries/LibGfx/Point.h +++ b/Userland/Libraries/LibGfx/Point.h @@ -231,7 +231,7 @@ 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; + [[nodiscard]] Point end_point_for_aspect_ratio(Point const& previous_end_point, float aspect_ratio) const; template<typename U> [[nodiscard]] Point<U> to_type() const |