diff --git a/Tests/LibGfx/TestRect.cpp b/Tests/LibGfx/TestRect.cpp index dfeeb3b20b..215c3ec794 100644 --- a/Tests/LibGfx/TestRect.cpp +++ b/Tests/LibGfx/TestRect.cpp @@ -56,3 +56,29 @@ TEST_CASE(rect_closest_to) closest = screen_rect.closest_to(p); EXPECT_EQ(screen_rect.side(closest), Gfx::IntRect::Side::Top); } + +TEST_CASE(rect_unite_horizontally) +{ + Gfx::IntRect rect { 10, 10, 100, 100 }; + Gfx::IntRect huge_rect { 0, 0, 1000, 1000 }; + + rect.unite_horizontally(huge_rect); + + EXPECT_EQ(rect.left(), 0); + EXPECT_EQ(rect.right(), 1000); + EXPECT_EQ(rect.top(), 10); + EXPECT_EQ(rect.bottom(), 110); +} + +TEST_CASE(rect_unite_vertically) +{ + Gfx::IntRect rect { 10, 10, 100, 100 }; + Gfx::IntRect huge_rect { 0, 0, 1000, 1000 }; + + rect.unite_vertically(huge_rect); + + EXPECT_EQ(rect.top(), 0); + EXPECT_EQ(rect.bottom(), 1000); + EXPECT_EQ(rect.left(), 10); + EXPECT_EQ(rect.right(), 110); +} diff --git a/Userland/Libraries/LibGfx/Rect.h b/Userland/Libraries/LibGfx/Rect.h index 37584d11a5..e137bf2aaa 100644 --- a/Userland/Libraries/LibGfx/Rect.h +++ b/Userland/Libraries/LibGfx/Rect.h @@ -870,6 +870,22 @@ public: return { { point.x() - size.width() / 2, point.y() - size.height() / 2 }, size }; } + void unite_horizontally(Rect const& other) + { + auto new_left = min(left(), other.left()); + auto new_right = max(right(), other.right()); + set_left(new_left); + set_right(new_right); + } + + void unite_vertically(Rect const& other) + { + auto new_top = min(top(), other.top()); + auto new_bottom = max(bottom(), other.bottom()); + set_top(new_top); + set_bottom(new_bottom); + } + [[nodiscard]] Rect united(Rect const& other) const { if (is_empty())