2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2021-06-16 17:24:27 +00:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2018-10-10 18:06:58 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-01-03 14:26:47 +00:00
|
|
|
#include <AK/Format.h>
|
2020-02-14 23:58:14 +00:00
|
|
|
#include <AK/StdLibExtras.h>
|
2021-04-12 18:47:09 +00:00
|
|
|
#include <LibGfx/AffineTransform.h>
|
2020-07-26 04:31:47 +00:00
|
|
|
#include <LibGfx/Forward.h>
|
2020-08-05 11:10:56 +00:00
|
|
|
#include <LibGfx/Orientation.h>
|
2020-03-29 17:03:13 +00:00
|
|
|
#include <LibIPC/Forward.h>
|
2020-08-12 08:44:45 +00:00
|
|
|
#include <math.h>
|
2020-03-08 11:05:14 +00:00
|
|
|
#include <stdlib.h>
|
2019-02-09 10:19:38 +00:00
|
|
|
|
2020-02-06 10:56:38 +00:00
|
|
|
namespace Gfx {
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
template<typename T>
|
|
|
|
class Point {
|
2018-10-10 18:06:58 +00:00
|
|
|
public:
|
2021-04-12 18:47:09 +00:00
|
|
|
Point() = default;
|
2020-07-26 04:31:47 +00:00
|
|
|
|
|
|
|
Point(T x, T y)
|
|
|
|
: m_x(x)
|
|
|
|
, m_y(y)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
Point(U x, U y)
|
2019-06-07 09:46:55 +00:00
|
|
|
: m_x(x)
|
|
|
|
, m_y(y)
|
|
|
|
{
|
|
|
|
}
|
2018-10-10 18:06:58 +00:00
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
template<typename U>
|
2021-06-16 17:24:27 +00:00
|
|
|
explicit Point(Point<U> const& other)
|
2020-07-26 04:31:47 +00:00
|
|
|
: m_x(other.x())
|
|
|
|
, m_y(other.y())
|
|
|
|
{
|
|
|
|
}
|
2020-07-22 06:46:15 +00:00
|
|
|
|
2021-04-12 18:47:09 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE T x() const { return m_x; }
|
|
|
|
[[nodiscard]] ALWAYS_INLINE T y() const { return m_y; }
|
2018-10-10 18:06:58 +00:00
|
|
|
|
2021-04-12 18:47:09 +00:00
|
|
|
ALWAYS_INLINE void set_x(T x) { m_x = x; }
|
|
|
|
ALWAYS_INLINE void set_y(T y) { m_y = y; }
|
2018-10-10 18:06:58 +00:00
|
|
|
|
2021-04-12 18:47:09 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE bool is_null() const { return !m_x && !m_y; }
|
|
|
|
[[nodiscard]] ALWAYS_INLINE bool is_empty() const { return m_x <= 0 && m_y <= 0; }
|
|
|
|
|
|
|
|
void translate_by(T dx, T dy)
|
2018-10-10 18:06:58 +00:00
|
|
|
{
|
|
|
|
m_x += dx;
|
|
|
|
m_y += dy;
|
|
|
|
}
|
|
|
|
|
2021-04-12 18:47:09 +00:00
|
|
|
ALWAYS_INLINE void translate_by(T dboth) { translate_by(dboth, dboth); }
|
2021-06-16 17:24:27 +00:00
|
|
|
ALWAYS_INLINE void translate_by(Point<T> const& delta) { translate_by(delta.x(), delta.y()); }
|
2021-04-12 18:47:09 +00:00
|
|
|
|
|
|
|
void scale_by(T dx, T dy)
|
2018-10-12 00:41:27 +00:00
|
|
|
{
|
2021-04-12 18:47:09 +00:00
|
|
|
m_x *= dx;
|
|
|
|
m_y *= dy;
|
2018-10-12 00:41:27 +00:00
|
|
|
}
|
|
|
|
|
2021-04-12 18:47:09 +00:00
|
|
|
ALWAYS_INLINE void scale_by(T dboth) { scale_by(dboth, dboth); }
|
2021-06-16 17:24:27 +00:00
|
|
|
ALWAYS_INLINE void scale_by(Point<T> const& delta) { scale_by(delta.x(), delta.y()); }
|
2021-04-12 18:47:09 +00:00
|
|
|
|
2021-06-16 17:24:27 +00:00
|
|
|
void transform_by(AffineTransform const& transform) { *this = transform.map(*this); }
|
2021-04-12 18:47:09 +00:00
|
|
|
|
2021-06-16 17:24:27 +00:00
|
|
|
Point<T> translated(Point<T> const& delta) const
|
2019-03-31 20:09:10 +00:00
|
|
|
{
|
2020-07-26 04:31:47 +00:00
|
|
|
Point<T> point = *this;
|
2021-04-12 18:47:09 +00:00
|
|
|
point.translate_by(delta);
|
2019-03-31 20:09:10 +00:00
|
|
|
return point;
|
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
Point<T> translated(T dx, T dy) const
|
2019-02-07 22:13:47 +00:00
|
|
|
{
|
2020-07-26 04:31:47 +00:00
|
|
|
Point<T> point = *this;
|
2021-04-12 18:47:09 +00:00
|
|
|
point.translate_by(dx, dy);
|
2019-02-07 22:13:47 +00:00
|
|
|
return point;
|
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
Point<T> translated(T dboth) const
|
|
|
|
{
|
|
|
|
Point<T> point = *this;
|
2021-04-12 18:47:09 +00:00
|
|
|
point.translate_by(dboth, dboth);
|
|
|
|
return point;
|
|
|
|
}
|
|
|
|
|
2021-06-16 17:24:27 +00:00
|
|
|
Point<T> scaled(Point<T> const& delta) const
|
2021-04-12 18:47:09 +00:00
|
|
|
{
|
|
|
|
Point<T> point = *this;
|
|
|
|
point.scale_by(delta);
|
|
|
|
return point;
|
|
|
|
}
|
|
|
|
|
|
|
|
Point<T> scaled(T sx, T sy) const
|
|
|
|
{
|
|
|
|
Point<T> point = *this;
|
|
|
|
point.scale_by(sx, sy);
|
|
|
|
return point;
|
|
|
|
}
|
|
|
|
|
2021-06-16 17:24:27 +00:00
|
|
|
Point<T> transformed(AffineTransform const& transform) const
|
2021-04-12 18:47:09 +00:00
|
|
|
{
|
|
|
|
Point<T> point = *this;
|
|
|
|
point.transform_by(transform);
|
2020-07-26 04:31:47 +00:00
|
|
|
return point;
|
|
|
|
}
|
|
|
|
|
2021-06-16 17:24:27 +00:00
|
|
|
void constrain(Rect<T> const&);
|
|
|
|
Point<T> constrained(Rect<T> const& rect) const
|
2020-08-05 11:10:56 +00:00
|
|
|
{
|
|
|
|
Point<T> point = *this;
|
|
|
|
point.constrain(rect);
|
|
|
|
return point;
|
|
|
|
}
|
2019-01-12 00:00:24 +00:00
|
|
|
|
2021-04-12 18:47:09 +00:00
|
|
|
Point<T> moved_left(T amount) const { return { x() - amount, y() }; }
|
|
|
|
Point<T> moved_right(T amount) const { return { x() + amount, y() }; }
|
|
|
|
Point<T> moved_up(T amount) const { return { x(), y() - amount }; }
|
|
|
|
Point<T> moved_down(T amount) const { return { x(), y() + amount }; }
|
|
|
|
|
2021-01-22 20:12:37 +00:00
|
|
|
template<class U>
|
2021-06-16 17:24:27 +00:00
|
|
|
bool operator==(Point<U> const& other) const
|
2018-10-11 23:03:22 +00:00
|
|
|
{
|
2021-01-22 20:12:37 +00:00
|
|
|
return x() == other.x() && y() == other.y();
|
2018-10-11 23:03:22 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 20:12:37 +00:00
|
|
|
template<class U>
|
2021-06-16 17:24:27 +00:00
|
|
|
bool operator!=(Point<U> const& other) const
|
2019-01-11 02:52:09 +00:00
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
2021-06-16 17:24:27 +00:00
|
|
|
Point<T> operator+(Point<T> const& other) const { return { m_x + other.m_x, m_y + other.m_y }; }
|
2019-09-27 16:59:00 +00:00
|
|
|
|
2021-06-16 17:24:27 +00:00
|
|
|
Point<T>& operator+=(Point<T> const& other)
|
2019-09-27 16:59:00 +00:00
|
|
|
{
|
2020-07-26 04:31:47 +00:00
|
|
|
m_x += other.m_x;
|
|
|
|
m_y += other.m_y;
|
2019-09-27 16:59:00 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
Point<T> operator-() const { return { -m_x, -m_y }; }
|
|
|
|
|
2021-06-16 17:24:27 +00:00
|
|
|
Point<T> operator-(Point<T> const& other) const { return { m_x - other.m_x, m_y - other.m_y }; }
|
2020-07-26 04:31:47 +00:00
|
|
|
|
2021-06-16 17:24:27 +00:00
|
|
|
Point<T>& operator-=(Point<T> const& other)
|
2019-09-27 16:59:00 +00:00
|
|
|
{
|
2020-07-26 04:31:47 +00:00
|
|
|
m_x -= other.m_x;
|
|
|
|
m_y -= other.m_y;
|
2019-09-27 16:59:00 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2019-03-09 15:54:41 +00:00
|
|
|
|
2020-12-18 16:16:17 +00:00
|
|
|
Point<T> operator*(T factor) const { return { m_x * factor, m_y * factor }; }
|
2020-07-26 04:31:47 +00:00
|
|
|
|
|
|
|
Point<T>& operator*=(T factor)
|
2020-05-05 01:15:17 +00:00
|
|
|
{
|
|
|
|
m_x *= factor;
|
|
|
|
m_y *= factor;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-12-18 16:16:17 +00:00
|
|
|
Point<T> operator/(T factor) const { return { m_x / factor, m_y / factor }; }
|
2020-07-26 04:31:47 +00:00
|
|
|
|
2020-12-18 16:16:17 +00:00
|
|
|
Point<T>& operator/=(T factor)
|
2020-05-05 01:15:17 +00:00
|
|
|
{
|
|
|
|
m_x /= factor;
|
|
|
|
m_y /= factor;
|
|
|
|
return *this;
|
|
|
|
}
|
2019-01-14 13:21:51 +00:00
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
T primary_offset_for_orientation(Orientation orientation) const
|
2019-07-20 14:46:33 +00:00
|
|
|
{
|
|
|
|
return orientation == Orientation::Vertical ? y() : x();
|
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
void set_primary_offset_for_orientation(Orientation orientation, T value)
|
2019-07-20 14:46:33 +00:00
|
|
|
{
|
2020-07-26 04:31:47 +00:00
|
|
|
if (orientation == Orientation::Vertical) {
|
2019-07-20 14:46:33 +00:00
|
|
|
set_y(value);
|
2020-07-26 04:31:47 +00:00
|
|
|
} else {
|
2019-07-20 14:46:33 +00:00
|
|
|
set_x(value);
|
2020-07-26 04:31:47 +00:00
|
|
|
}
|
2019-07-20 14:46:33 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
T secondary_offset_for_orientation(Orientation orientation) const
|
2019-07-20 14:46:33 +00:00
|
|
|
{
|
|
|
|
return orientation == Orientation::Vertical ? x() : y();
|
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
void set_secondary_offset_for_orientation(Orientation orientation, T value)
|
2019-07-20 14:46:33 +00:00
|
|
|
{
|
2020-07-26 04:31:47 +00:00
|
|
|
if (orientation == Orientation::Vertical) {
|
2019-07-20 14:46:33 +00:00
|
|
|
set_x(value);
|
2020-07-26 04:31:47 +00:00
|
|
|
} else {
|
2019-07-20 14:46:33 +00:00
|
|
|
set_y(value);
|
2020-07-26 04:31:47 +00:00
|
|
|
}
|
2019-07-20 14:46:33 +00:00
|
|
|
}
|
|
|
|
|
2021-06-16 17:24:27 +00:00
|
|
|
T dx_relative_to(Point<T> const& other) const
|
2019-12-26 07:21:03 +00:00
|
|
|
{
|
|
|
|
return x() - other.x();
|
|
|
|
}
|
|
|
|
|
2021-06-16 17:24:27 +00:00
|
|
|
T dy_relative_to(Point<T> const& other) const
|
2019-12-26 07:21:03 +00:00
|
|
|
{
|
|
|
|
return y() - other.y();
|
|
|
|
}
|
|
|
|
|
2019-11-10 17:28:01 +00:00
|
|
|
// Returns pixels moved from other in either direction
|
2021-06-16 17:24:27 +00:00
|
|
|
T pixels_moved(Point<T> const& other) const
|
2019-11-10 17:28:01 +00:00
|
|
|
{
|
2019-12-26 07:21:03 +00:00
|
|
|
return max(abs(dx_relative_to(other)), abs(dy_relative_to(other)));
|
2019-11-10 17:28:01 +00:00
|
|
|
}
|
|
|
|
|
2021-06-16 17:24:27 +00:00
|
|
|
float distance_from(Point<T> const& other) const
|
2020-07-26 04:31:47 +00:00
|
|
|
{
|
|
|
|
if (*this == other)
|
|
|
|
return 0;
|
|
|
|
return sqrtf(powf(m_x - other.m_x, 2.0f) + powf(m_y - other.m_y, 2.0f));
|
|
|
|
}
|
|
|
|
|
2021-06-16 17:24:27 +00:00
|
|
|
Point absolute_relative_distance_to(Point const& other) const
|
2020-11-30 06:30:15 +00:00
|
|
|
{
|
|
|
|
return { abs(dx_relative_to(other)), abs(dy_relative_to(other)) };
|
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
template<typename U>
|
2021-05-03 14:37:05 +00:00
|
|
|
Point<U> to_type() const
|
2020-07-26 04:31:47 +00:00
|
|
|
{
|
|
|
|
return Point<U>(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
String to_string() const;
|
|
|
|
|
2018-10-10 18:06:58 +00:00
|
|
|
private:
|
2020-07-26 04:31:47 +00:00
|
|
|
T m_x { 0 };
|
|
|
|
T m_y { 0 };
|
2018-10-10 18:06:58 +00:00
|
|
|
};
|
2019-07-04 04:45:50 +00:00
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
using IntPoint = Point<int>;
|
|
|
|
using FloatPoint = Point<float>;
|
2020-02-06 10:56:38 +00:00
|
|
|
|
|
|
|
}
|
2020-02-15 11:04:35 +00:00
|
|
|
|
2021-01-03 14:26:47 +00:00
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct Formatter<Gfx::Point<T>> : Formatter<StringView> {
|
2021-06-16 17:24:27 +00:00
|
|
|
void format(FormatBuilder& builder, Gfx::Point<T> const& value)
|
2021-01-03 14:26:47 +00:00
|
|
|
{
|
|
|
|
Formatter<StringView>::format(builder, value.to_string());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-02-15 11:04:35 +00:00
|
|
|
namespace IPC {
|
2020-07-26 04:31:47 +00:00
|
|
|
|
2021-06-16 17:24:27 +00:00
|
|
|
bool encode(Encoder&, Gfx::IntPoint const&);
|
2020-06-10 08:57:59 +00:00
|
|
|
bool decode(Decoder&, Gfx::IntPoint&);
|
2020-07-26 04:31:47 +00:00
|
|
|
|
2020-02-15 11:04:35 +00:00
|
|
|
}
|