Skip to content

Commit f14c891

Browse files
mustafaquraishawesomekling
authored andcommitted
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`.`
1 parent 5c244a7 commit f14c891

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

Userland/Applications/PixelPaint/EllipseTool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ void EllipseTool::on_mousemove(Layer*, MouseEvent& event)
9090
m_draw_mode = event.layer_event().alt() ? DrawMode::FromCenter : DrawMode::FromCorner;
9191

9292
if (event.layer_event().shift())
93-
m_ellipse_end_position = m_ellipse_start_position.end_point_for_square_aspect_ratio(event.layer_event().position());
93+
m_ellipse_end_position = m_ellipse_start_position.end_point_for_aspect_ratio(event.layer_event().position(), 1.0);
9494
else
9595
m_ellipse_end_position = event.layer_event().position();
9696

Userland/Applications/PixelPaint/RectangleTool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ void RectangleTool::on_mousemove(Layer* layer, MouseEvent& event)
9696
m_draw_mode = event.layer_event().alt() ? DrawMode::FromCenter : DrawMode::FromCorner;
9797

9898
if (event.layer_event().shift())
99-
m_rectangle_end_position = m_rectangle_start_position.end_point_for_square_aspect_ratio(event.layer_event().position());
99+
m_rectangle_end_position = m_rectangle_start_position.end_point_for_aspect_ratio(event.layer_event().position(), 1.0);
100100
else
101101
m_rectangle_end_position = event.layer_event().position();
102102

Userland/Libraries/LibGfx/Point.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,19 @@ void Point<T>::constrain(Rect<T> const& rect)
2020
}
2121

2222
template<typename T>
23-
[[nodiscard]] Point<T> Point<T>::end_point_for_square_aspect_ratio(Point<T> const& previous_end_point) const
23+
[[nodiscard]] Point<T> Point<T>::end_point_for_aspect_ratio(Point<T> const& previous_end_point, float aspect_ratio) const
2424
{
25-
const T dx = previous_end_point.x() - x();
26-
const T dy = previous_end_point.y() - y();
27-
const T x_sign = dx >= 0 ? 1 : -1;
28-
const T y_sign = dy >= 0 ? 1 : -1;
29-
const T abs_size = AK::max(AK::abs(dx), AK::abs(dy));
30-
return { x() + x_sign * abs_size, y() + y_sign * abs_size };
25+
VERIFY(aspect_ratio > 0);
26+
const T x_sign = previous_end_point.x() >= x() ? 1 : -1;
27+
const T y_sign = previous_end_point.y() >= y() ? 1 : -1;
28+
T dx = AK::abs(previous_end_point.x() - x());
29+
T dy = AK::abs(previous_end_point.y() - y());
30+
if (dx > dy) {
31+
dy = (T)((float)dx / aspect_ratio);
32+
} else {
33+
dx = (T)((float)dy * aspect_ratio);
34+
}
35+
return { x() + x_sign * dx, y() + y_sign * dy };
3136
}
3237

3338
template<>

Userland/Libraries/LibGfx/Point.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ class Point {
231231
return { AK::abs(dx_relative_to(other)), AK::abs(dy_relative_to(other)) };
232232
}
233233

234-
[[nodiscard]] Point end_point_for_square_aspect_ratio(Point const&) const;
234+
[[nodiscard]] Point end_point_for_aspect_ratio(Point const& previous_end_point, float aspect_ratio) const;
235235

236236
template<typename U>
237237
[[nodiscard]] Point<U> to_type() const

0 commit comments

Comments
 (0)