summaryrefslogtreecommitdiff
path: root/Libraries/LibGfx
diff options
context:
space:
mode:
authorTom <tomut@yahoo.com>2020-08-14 14:10:39 -0600
committerAndreas Kling <kling@serenityos.org>2020-08-18 12:12:27 +0200
commitf8903acea246e186bfa730342019fbe7c863d882 (patch)
tree3b09e28dc36c2eb2c6a30003e70968e840f749a1 /Libraries/LibGfx
parenta43ba348e17a07e006543e14ff9189aef457b288 (diff)
downloadserenity-f8903acea246e186bfa730342019fbe7c863d882.zip
LibGfx: Add convenience helpers for Rect
Diffstat (limited to 'Libraries/LibGfx')
-rw-r--r--Libraries/LibGfx/Rect.h40
1 files changed, 39 insertions, 1 deletions
diff --git a/Libraries/LibGfx/Rect.h b/Libraries/LibGfx/Rect.h
index 6105491f82..50a6bad661 100644
--- a/Libraries/LibGfx/Rect.h
+++ b/Libraries/LibGfx/Rect.h
@@ -43,7 +43,7 @@ T abst(T value)
template<typename T>
class Rect {
public:
- Rect() {}
+ Rect() { }
Rect(T x, T y, T width, T height)
: m_location(x, y)
@@ -191,6 +191,18 @@ public:
&& bottom() >= other.bottom();
}
+ template<typename Container>
+ bool contains(const Container& others) const
+ {
+ bool have_any = false;
+ for (const auto& other : others) {
+ if (!contains(other))
+ return false;
+ have_any = true;
+ }
+ return have_any;
+ }
+
int primary_offset_for_orientation(Orientation orientation) const { return m_location.primary_offset_for_orientation(orientation); }
void set_primary_offset_for_orientation(Orientation orientation, int value) { m_location.set_primary_offset_for_orientation(orientation, value); }
int secondary_offset_for_orientation(Orientation orientation) const { return m_location.secondary_offset_for_orientation(orientation); }
@@ -270,6 +282,32 @@ public:
&& other.top() <= bottom();
}
+ template<typename Container>
+ bool intersects(const Container& others) const
+ {
+ for (const auto& other : others) {
+ if (intersects(other))
+ return true;
+ }
+ return false;
+ }
+
+ template<typename Container, typename Function>
+ IterationDecision for_each_intersected(const Container& others, Function f) const
+ {
+ if (is_empty())
+ return IterationDecision::Continue;
+ for (const auto& other : others) {
+ auto intersected_rect = intersected(other);
+ if (!intersected_rect.is_empty()) {
+ IterationDecision decision = f(intersected_rect);
+ if (decision != IterationDecision::Continue)
+ return decision;
+ }
+ }
+ return IterationDecision::Continue;
+ }
+
T x() const { return location().x(); }
T y() const { return location().y(); }
T width() const { return m_size.width(); }