/src/serenity/Userland/Libraries/LibGfx/Quad.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2022, Andreas Kling <kling@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <LibGfx/Point.h> |
10 | | #include <LibGfx/Rect.h> |
11 | | #include <LibGfx/Triangle.h> |
12 | | |
13 | | namespace Gfx { |
14 | | |
15 | | template<typename T> |
16 | | class Quad { |
17 | | public: |
18 | | Quad(Point<T> p1, Point<T> p2, Point<T> p3, Point<T> p4) |
19 | 0 | : m_p1(p1) |
20 | 0 | , m_p2(p2) |
21 | 0 | , m_p3(p3) |
22 | 0 | , m_p4(p4) |
23 | 0 | { |
24 | 0 | } |
25 | | |
26 | | Quad(Rect<T> const& rect) |
27 | 0 | : Quad(rect.top_left(), rect.top_right(), rect.bottom_right(), rect.bottom_left()) |
28 | 0 | { |
29 | 0 | } |
30 | | |
31 | 0 | Point<T> const& p1() const { return m_p1; } |
32 | 0 | Point<T> const& p2() const { return m_p2; } |
33 | 0 | Point<T> const& p3() const { return m_p3; } |
34 | 0 | Point<T> const& p4() const { return m_p4; } |
35 | | |
36 | | Rect<T> bounding_rect() const |
37 | 0 | { |
38 | 0 | auto top = min(min(m_p1.y(), m_p2.y()), min(m_p3.y(), m_p4.y())); |
39 | 0 | auto right = max(max(m_p1.x(), m_p2.x()), max(m_p3.x(), m_p4.x())); |
40 | 0 | auto bottom = max(max(m_p1.y(), m_p2.y()), max(m_p3.y(), m_p4.y())); |
41 | 0 | auto left = min(min(m_p1.x(), m_p2.x()), min(m_p3.x(), m_p4.x())); |
42 | 0 | return { left, top, right - left, bottom - top }; |
43 | 0 | } |
44 | | |
45 | | bool contains(Point<T> point) const |
46 | 0 | { |
47 | | // FIXME: There's probably a smarter way to do this. |
48 | 0 | return Triangle(m_p1, m_p2, m_p3).contains(point) |
49 | 0 | || Triangle(m_p1, m_p3, m_p4).contains(point) |
50 | 0 | || Triangle(m_p2, m_p4, m_p1).contains(point) |
51 | 0 | || Triangle(m_p2, m_p4, m_p3).contains(point); |
52 | 0 | } |
53 | | |
54 | 0 | bool operator==(Quad<T> const&) const = default; |
55 | | |
56 | | private: |
57 | | Point<T> m_p1; |
58 | | Point<T> m_p2; |
59 | | Point<T> m_p3; |
60 | | Point<T> m_p4; |
61 | | }; |
62 | | |
63 | | } |