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
Notes: sideshowbarker 2024-07-19 04:37:02 +09:00
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; }

View file

@ -1565,7 +1565,7 @@ void Painter::fill_path(Path& path, Color color, WindingRule winding_rule)
#ifdef FILL_PATH_DEBUG
size_t i { 0 };
for (auto& segment : segments)
draw_line(IntPoint(segment.from.x(), segment.from.y()), IntPoint(segment.to.x(), segment.to.y()), Color::from_hsv(++i / segments.size() * 255, 255, 255), 1);
draw_line(segment.from, segment.to, Color::from_hsv(++i / segments.size() * 255, 255, 255), 1);
#endif
}

View file

@ -434,25 +434,29 @@ void HTMLPathElement::paint(const SvgPaintingContext& context, Gfx::Painter& pai
#endif
switch (instruction.type) {
case PathInstructionType::Move:
case PathInstructionType::Move: {
Gfx::FloatPoint point = { data[0], data[1] };
if (absolute) {
path.move_to({ data[0], data[1] });
path.move_to(point);
} else {
ASSERT(!path.segments().is_empty());
path.move_to(Gfx::FloatPoint { data[0], data[1] } + path.segments().last().point());
path.move_to(point + path.segments().last().point());
}
break;
}
case PathInstructionType::ClosePath:
path.close();
break;
case PathInstructionType::Line:
case PathInstructionType::Line: {
Gfx::FloatPoint point = { data[0], data[1] };
if (absolute) {
path.line_to({ data[0], data[1] });
path.line_to(point);
} else {
ASSERT(!path.segments().is_empty());
path.line_to(Gfx::FloatPoint { data[0], data[1] } + path.segments().last().point());
path.line_to(point + path.segments().last().point());
}
break;
}
case PathInstructionType::HorizontalLine: {
ASSERT(!path.segments().is_empty());
auto last_point = path.segments().last().point();
@ -467,9 +471,9 @@ void HTMLPathElement::paint(const SvgPaintingContext& context, Gfx::Painter& pai
ASSERT(!path.segments().is_empty());
auto last_point = path.segments().last().point();
if (absolute) {
path.line_to(Gfx::FloatPoint{ last_point.x(), data[0] });
path.line_to(Gfx::FloatPoint { last_point.x(), data[0] });
} else {
path.line_to(Gfx::FloatPoint{ last_point.x(), data[0] + last_point.y() });
path.line_to(Gfx::FloatPoint { last_point.x(), data[0] + last_point.y() });
}
break;
}
@ -560,15 +564,19 @@ void HTMLPathElement::paint(const SvgPaintingContext& context, Gfx::Painter& pai
break;
}
case PathInstructionType::QuadraticBezierCurve:
case PathInstructionType::QuadraticBezierCurve: {
Gfx::FloatPoint through = { data[0], data[1] };
Gfx::FloatPoint point = { data[2], data[3] };
if (absolute) {
path.quadratic_bezier_curve_to({ data[0], data[1] }, { data[2], data[3] });
path.quadratic_bezier_curve_to(through, point);
} else {
ASSERT(!path.segments().is_empty());
auto last_point = path.segments().last().point();
path.quadratic_bezier_curve_to({ data[0] + last_point.x(), data[1] + last_point.y() }, { data[2] + last_point.x(), data[3] + last_point.y() });
path.quadratic_bezier_curve_to(through + last_point, point + last_point);
}
break;
}
case PathInstructionType::Curve:
case PathInstructionType::SmoothCurve:
case PathInstructionType::SmoothQuadraticBezierCurve:

View file

@ -42,7 +42,7 @@ void LineBox::add_fragment(const LayoutNode& layout_node, int start, int length,
m_fragments.last().m_length = (start - m_fragments.last().m_start) + length;
m_fragments.last().set_width(m_fragments.last().width() + width);
} else {
m_fragments.append(make<LineBoxFragment>(layout_node, start, length, Gfx::FloatPoint(m_width, 0), Gfx::FloatSize(width, height)));
m_fragments.append(make<LineBoxFragment>(layout_node, start, length, Gfx::FloatPoint(m_width, 0.0f), Gfx::FloatSize(width, height)));
}
m_width += width;