From 5d8f4ab878159e641e885b90cb10299ce8f92336 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 7 Apr 2022 14:02:28 +0200 Subject: [PATCH] LibGfx: Add Gfx::Quad to represent arbitrary quadrilaterals This comes with a very barebones API for now. You can ask for the bounding rect of the quad, and also check if a point is inside of it. --- Userland/Libraries/LibGfx/Forward.h | 5 +++ Userland/Libraries/LibGfx/Quad.h | 55 +++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 Userland/Libraries/LibGfx/Quad.h diff --git a/Userland/Libraries/LibGfx/Forward.h b/Userland/Libraries/LibGfx/Forward.h index 72bea7f1cd..5477b5a6f1 100644 --- a/Userland/Libraries/LibGfx/Forward.h +++ b/Userland/Libraries/LibGfx/Forward.h @@ -41,6 +41,9 @@ class Size; template class Rect; +template +class Quad; + using IntLine = Line; using FloatLine = Line; @@ -53,6 +56,8 @@ using FloatPoint = Point; using IntSize = Size; using FloatSize = Size; +using FloatQuad = Quad; + enum class BitmapFormat; enum class ColorRole; enum class TextAlignment; diff --git a/Userland/Libraries/LibGfx/Quad.h b/Userland/Libraries/LibGfx/Quad.h new file mode 100644 index 0000000000..4b1d00c87f --- /dev/null +++ b/Userland/Libraries/LibGfx/Quad.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2022, Andreas Kling + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include + +namespace Gfx { + +template +class Quad { +public: + Quad(Point p1, Point p2, Point p3, Point p4) + : m_p1(p1) + , m_p2(p2) + , m_p3(p3) + , m_p4(p4) + { + } + + Point const& p1() const { return m_p1; } + Point const& p2() const { return m_p2; } + Point const& p3() const { return m_p3; } + Point const& p4() const { return m_p4; } + + Rect bounding_rect() const + { + auto top = min(min(m_p1.y(), m_p2.y()), min(m_p3.y(), m_p4.y())); + auto right = max(max(m_p1.x(), m_p2.x()), max(m_p3.x(), m_p4.x())); + auto bottom = max(max(m_p1.y(), m_p2.y()), max(m_p3.y(), m_p4.y())); + auto left = min(min(m_p1.x(), m_p2.x()), min(m_p3.x(), m_p4.x())); + return { left, top, right - left, bottom - top }; + } + + bool contains(Point point) const + { + // FIXME: There's probably a smarter way to do this. + return Triangle(m_p1, m_p2, m_p3).contains(point) + || Triangle(m_p1, m_p3, m_p4).contains(point) + || Triangle(m_p2, m_p4, m_p1).contains(point) + || Triangle(m_p2, m_p4, m_p3).contains(point); + } + +private: + Point m_p1; + Point m_p2; + Point m_p3; + Point m_p4; +}; + +}