1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 19:27:45 +00:00

LibGfx: Add a bunch of [[nodiscard]] to Point

This commit is contained in:
Andreas Kling 2021-06-16 19:26:50 +02:00
parent df0248b096
commit 20fb30d857

View file

@ -71,42 +71,42 @@ public:
void transform_by(AffineTransform const& transform) { *this = transform.map(*this); } void transform_by(AffineTransform const& transform) { *this = transform.map(*this); }
Point<T> translated(Point<T> const& delta) const [[nodiscard]] Point<T> translated(Point<T> const& delta) const
{ {
Point<T> point = *this; Point<T> point = *this;
point.translate_by(delta); point.translate_by(delta);
return point; return point;
} }
Point<T> translated(T dx, T dy) const [[nodiscard]] Point<T> translated(T dx, T dy) const
{ {
Point<T> point = *this; Point<T> point = *this;
point.translate_by(dx, dy); point.translate_by(dx, dy);
return point; return point;
} }
Point<T> translated(T dboth) const [[nodiscard]] Point<T> translated(T dboth) const
{ {
Point<T> point = *this; Point<T> point = *this;
point.translate_by(dboth, dboth); point.translate_by(dboth, dboth);
return point; return point;
} }
Point<T> scaled(Point<T> const& delta) const [[nodiscard]] Point<T> scaled(Point<T> const& delta) const
{ {
Point<T> point = *this; Point<T> point = *this;
point.scale_by(delta); point.scale_by(delta);
return point; return point;
} }
Point<T> scaled(T sx, T sy) const [[nodiscard]] Point<T> scaled(T sx, T sy) const
{ {
Point<T> point = *this; Point<T> point = *this;
point.scale_by(sx, sy); point.scale_by(sx, sy);
return point; return point;
} }
Point<T> transformed(AffineTransform const& transform) const [[nodiscard]] Point<T> transformed(AffineTransform const& transform) const
{ {
Point<T> point = *this; Point<T> point = *this;
point.transform_by(transform); point.transform_by(transform);
@ -114,31 +114,31 @@ public:
} }
void constrain(Rect<T> const&); void constrain(Rect<T> const&);
Point<T> constrained(Rect<T> const& rect) const [[nodiscard]] Point<T> constrained(Rect<T> const& rect) const
{ {
Point<T> point = *this; Point<T> point = *this;
point.constrain(rect); point.constrain(rect);
return point; return point;
} }
Point<T> moved_left(T amount) const { return { x() - amount, y() }; } [[nodiscard]] Point<T> moved_left(T amount) const { return { x() - amount, y() }; }
Point<T> moved_right(T amount) const { return { x() + amount, y() }; } [[nodiscard]] Point<T> moved_right(T amount) const { return { x() + amount, y() }; }
Point<T> moved_up(T amount) const { return { x(), y() - amount }; } [[nodiscard]] Point<T> moved_up(T amount) const { return { x(), y() - amount }; }
Point<T> moved_down(T amount) const { return { x(), y() + amount }; } [[nodiscard]] Point<T> moved_down(T amount) const { return { x(), y() + amount }; }
template<class U> template<class U>
bool operator==(Point<U> const& other) const [[nodiscard]] bool operator==(Point<U> const& other) const
{ {
return x() == other.x() && y() == other.y(); return x() == other.x() && y() == other.y();
} }
template<class U> template<class U>
bool operator!=(Point<U> const& other) const [[nodiscard]] bool operator!=(Point<U> const& other) const
{ {
return !(*this == other); return !(*this == other);
} }
Point<T> operator+(Point<T> const& other) const { return { m_x + other.m_x, m_y + other.m_y }; } [[nodiscard]] Point<T> operator+(Point<T> const& other) const { return { m_x + other.m_x, m_y + other.m_y }; }
Point<T>& operator+=(Point<T> const& other) Point<T>& operator+=(Point<T> const& other)
{ {
@ -147,9 +147,9 @@ public:
return *this; return *this;
} }
Point<T> operator-() const { return { -m_x, -m_y }; } [[nodiscard]] Point<T> operator-() const { return { -m_x, -m_y }; }
Point<T> operator-(Point<T> const& other) const { return { m_x - other.m_x, m_y - other.m_y }; } [[nodiscard]] Point<T> operator-(Point<T> const& other) const { return { m_x - other.m_x, m_y - other.m_y }; }
Point<T>& operator-=(Point<T> const& other) Point<T>& operator-=(Point<T> const& other)
{ {
@ -158,7 +158,7 @@ public:
return *this; return *this;
} }
Point<T> operator*(T factor) const { return { m_x * factor, m_y * factor }; } [[nodiscard]] Point<T> operator*(T factor) const { return { m_x * factor, m_y * factor }; }
Point<T>& operator*=(T factor) Point<T>& operator*=(T factor)
{ {
@ -167,7 +167,7 @@ public:
return *this; return *this;
} }
Point<T> operator/(T factor) const { return { m_x / factor, m_y / factor }; } [[nodiscard]] Point<T> operator/(T factor) const { return { m_x / factor, m_y / factor }; }
Point<T>& operator/=(T factor) Point<T>& operator/=(T factor)
{ {
@ -176,7 +176,7 @@ public:
return *this; return *this;
} }
T primary_offset_for_orientation(Orientation orientation) const [[nodiscard]] T primary_offset_for_orientation(Orientation orientation) const
{ {
return orientation == Orientation::Vertical ? y() : x(); return orientation == Orientation::Vertical ? y() : x();
} }
@ -190,7 +190,7 @@ public:
} }
} }
T secondary_offset_for_orientation(Orientation orientation) const [[nodiscard]] T secondary_offset_for_orientation(Orientation orientation) const
{ {
return orientation == Orientation::Vertical ? x() : y(); return orientation == Orientation::Vertical ? x() : y();
} }
@ -204,41 +204,41 @@ public:
} }
} }
T dx_relative_to(Point<T> const& other) const [[nodiscard]] T dx_relative_to(Point<T> const& other) const
{ {
return x() - other.x(); return x() - other.x();
} }
T dy_relative_to(Point<T> const& other) const [[nodiscard]] T dy_relative_to(Point<T> const& other) const
{ {
return y() - other.y(); return y() - other.y();
} }
// Returns pixels moved from other in either direction // Returns pixels moved from other in either direction
T pixels_moved(Point<T> const& other) const [[nodiscard]] T pixels_moved(Point<T> const& other) const
{ {
return max(abs(dx_relative_to(other)), abs(dy_relative_to(other))); return max(abs(dx_relative_to(other)), abs(dy_relative_to(other)));
} }
float distance_from(Point<T> const& other) const [[nodiscard]] float distance_from(Point<T> const& other) const
{ {
if (*this == other) if (*this == other)
return 0; return 0;
return sqrtf(powf(m_x - other.m_x, 2.0f) + powf(m_y - other.m_y, 2.0f)); return sqrtf(powf(m_x - other.m_x, 2.0f) + powf(m_y - other.m_y, 2.0f));
} }
Point absolute_relative_distance_to(Point const& other) const [[nodiscard]] Point absolute_relative_distance_to(Point const& other) const
{ {
return { abs(dx_relative_to(other)), abs(dy_relative_to(other)) }; return { abs(dx_relative_to(other)), abs(dy_relative_to(other)) };
} }
template<typename U> template<typename U>
Point<U> to_type() const [[nodiscard]] Point<U> to_type() const
{ {
return Point<U>(*this); return Point<U>(*this);
} }
String to_string() const; [[nodiscard]] String to_string() const;
private: private:
T m_x { 0 }; T m_x { 0 };