mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 17:47:44 +00:00
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`.`
This commit is contained in:
parent
5c244a7893
commit
f14c891ba5
4 changed files with 15 additions and 10 deletions
|
@ -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<>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue