From 7c80f302e489ec1754c4cc03cb1cd41f68785850 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Fri, 31 Dec 2021 16:42:53 -0700 Subject: [PATCH] Use std::optional to remove extra sf::Rect::intersects overload --- include/SFML/Graphics/Rect.hpp | 25 +++++-------------------- include/SFML/Graphics/Rect.inl | 17 +++-------------- test/Graphics/Rect.cpp | 34 +++++++++++----------------------- 3 files changed, 19 insertions(+), 57 deletions(-) diff --git a/include/SFML/Graphics/Rect.hpp b/include/SFML/Graphics/Rect.hpp index 12c648e71..43b72d91c 100644 --- a/include/SFML/Graphics/Rect.hpp +++ b/include/SFML/Graphics/Rect.hpp @@ -29,6 +29,7 @@ // Headers //////////////////////////////////////////////////////////// #include +#include namespace sf @@ -87,7 +88,7 @@ public: /// /// \return True if the point is inside, false otherwise /// - /// \see intersects + /// \see findIntersection /// //////////////////////////////////////////////////////////// constexpr bool contains(const Vector2& point) const; @@ -97,28 +98,12 @@ public: /// /// \param rectangle Rectangle to test /// - /// \return True if rectangles overlap, false otherwise + /// \return Intersection rectangle if intersecting, std::nullopt otherwise /// /// \see contains /// //////////////////////////////////////////////////////////// - constexpr bool intersects(const Rect& rectangle) const; - - //////////////////////////////////////////////////////////// - /// \brief Check the intersection between two rectangles - /// - /// This overload returns the overlapped rectangle in the - /// \a intersection parameter. - /// - /// \param rectangle Rectangle to test - /// \param intersection Rectangle to be filled with the intersection - /// - /// \return True if rectangles overlap, false otherwise - /// - /// \see contains - /// - //////////////////////////////////////////////////////////// - constexpr bool intersects(const Rect& rectangle, Rect& intersection) const; + constexpr std::optional> findIntersection(const Rect& rectangle) const; //////////////////////////////////////////////////////////// /// \brief Get the position of the rectangle's top-left corner @@ -236,7 +221,7 @@ using FloatRect = Rect; /// /// // Test the intersection between r1 and r2 /// sf::IntRect result; -/// bool b3 = r1.intersects(r2, result); // true +/// bool b3 = r1.findIntersection(r2, result); // true /// // result == (4, 2, 16, 3) /// \endcode /// diff --git a/include/SFML/Graphics/Rect.inl b/include/SFML/Graphics/Rect.inl index 75b667f1c..4a220b164 100644 --- a/include/SFML/Graphics/Rect.inl +++ b/include/SFML/Graphics/Rect.inl @@ -81,16 +81,7 @@ constexpr bool Rect::contains(const Vector2& point) const //////////////////////////////////////////////////////////// template -constexpr bool Rect::intersects(const Rect& rectangle) const -{ - Rect intersection; - return intersects(rectangle, intersection); -} - - -//////////////////////////////////////////////////////////// -template -constexpr bool Rect::intersects(const Rect& rectangle, Rect& intersection) const +constexpr std::optional> Rect::findIntersection(const Rect& rectangle) const { // Not using 'std::min' and 'std::max' to avoid depending on '' const auto min = [](T a, T b){ return (a < b) ? a : b; }; @@ -119,13 +110,11 @@ constexpr bool Rect::intersects(const Rect& rectangle, Rect& intersecti // If the intersection is valid (positive non zero area), then there is an intersection if ((interLeft < interRight) && (interTop < interBottom)) { - intersection = Rect({interLeft, interTop}, {interRight - interLeft, interBottom - interTop}); - return true; + return Rect({interLeft, interTop}, {interRight - interLeft, interBottom - interTop}); } else { - intersection = Rect({0, 0}, {0, 0}); - return false; + return std::nullopt; } } diff --git a/test/Graphics/Rect.cpp b/test/Graphics/Rect.cpp index 834681c5a..617c069eb 100644 --- a/test/Graphics/Rect.cpp +++ b/test/Graphics/Rect.cpp @@ -69,31 +69,19 @@ TEST_CASE("sf::Rect class template - [graphics]") SUBCASE("Intersection") { - SUBCASE("intersects(Rect)") - { - sf::IntRect rectangle({0, 0}, {10, 10}); - sf::IntRect intersectingRectangle({5, 5}, {10, 10}); - sf::IntRect nonIntersectingRectangle({-5, -5}, {5, 5}); + const sf::IntRect rectangle({0, 0}, {10, 10}); + const sf::IntRect intersectingRectangle({5, 5}, {10, 10}); - CHECK(rectangle.intersects(intersectingRectangle) == true); - CHECK(rectangle.intersects(nonIntersectingRectangle) == false); - } + const auto intersectionResult = rectangle.findIntersection(intersectingRectangle); + REQUIRE(intersectionResult.has_value()); + CHECK(intersectionResult->top == 5); + CHECK(intersectionResult->left == 5); + CHECK(intersectionResult->width == 5); + CHECK(intersectionResult->height == 5); - SUBCASE("intersects(Rect, Rect)") - { - sf::IntRect rectangle({0, 0}, {10, 10}); - sf::IntRect intersectingRectangle({5, 5}, {10, 10}); - sf::IntRect nonIntersectingRectangle({-5, -5}, {5, 5}); - sf::IntRect intersectionResult; - - CHECK(rectangle.intersects(intersectingRectangle, intersectionResult) == true); - CHECK(intersectionResult.top == 5); - CHECK(intersectionResult.left == 5); - CHECK(intersectionResult.width == 5); - CHECK(intersectionResult.height == 5); - - CHECK(rectangle.intersects(nonIntersectingRectangle, intersectionResult) == false); - } + const sf::IntRect nonIntersectingRectangle({-5, -5}, {5, 5}); + CHECK_FALSE(rectangle.findIntersection(nonIntersectingRectangle).has_value()); + CHECK_FALSE(rectangle.findIntersection(nonIntersectingRectangle)); } SUBCASE("Comparison operations")