1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 01:47:35 +00:00

LibGfx: Add FloatPoint methods

Adds some conversion constructors, as well as the missing arithmetic
operations.
This commit is contained in:
Matthew Olsson 2020-07-22 14:50:54 -07:00 committed by Andreas Kling
parent 5985eac81d
commit 9cce7f57dd
4 changed files with 54 additions and 14 deletions

View file

@ -30,6 +30,7 @@
#include <AK/String.h>
#include <LibGfx/Orientation.h>
#include <LibGfx/Point.h>
#include <LibM/math.h>
namespace Gfx {
@ -38,12 +39,25 @@ class FloatRect;
class FloatPoint {
public:
FloatPoint() { }
FloatPoint(int x, int y)
: m_x(x)
, m_y(y)
{
}
FloatPoint(float x, float y)
: m_x(x)
, m_y(y)
{
}
FloatPoint(double x, double y)
: m_x(x)
, m_y(y)
{
}
explicit FloatPoint(const IntPoint& other)
: m_x(other.x())
, m_y(other.y())
@ -96,6 +110,7 @@ public:
FloatPoint operator-() const { return { -m_x, -m_y }; }
FloatPoint operator-(float number) const { return { m_x - number, m_y - number }; }
FloatPoint operator-(const FloatPoint& other) const { return { m_x - other.m_x, m_y - other.m_y }; }
FloatPoint& operator-=(const FloatPoint& other)
{
@ -104,14 +119,24 @@ public:
return *this;
}
FloatPoint operator+(float number) const { return { m_x + number, m_y + number }; }
FloatPoint operator+(const FloatPoint& other) const { return { m_x + other.m_x, m_y + other.m_y }; }
FloatPoint& operator+=(const FloatPoint& other)
{
m_x += other.m_x;
m_y += other.m_y;
return *this;
}
FloatPoint operator+(const FloatPoint& other) const { return { m_x + other.m_x, m_y + other.m_y }; }
FloatPoint operator*(float factor) const { return { m_x * factor, m_y * factor }; }
FloatPoint& operator*=(float factor)
{
m_x *= factor;
m_y *= factor;
return *this;
}
FloatPoint operator/(float factor) const { return { m_x / factor, m_y / factor }; }
FloatPoint& operator/=(float factor)
{
m_x /= factor;
@ -119,6 +144,13 @@ public:
return *this;
}
float distance_from(const FloatPoint& other) const
{
if (*this == other)
return 0;
return sqrtf(powf(m_x - other.m_x, 2.0f) + powf(m_y - other.m_y, 2.0f));
}
String to_string() const { return String::format("[%g,%g]", x(), y()); }
bool is_null() const { return !m_x && !m_y; }