From a7a4c28c033741061e6fac7b340173729cacc0c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 31 Dec 2012 18:52:14 +0100 Subject: [PATCH] Math: ability to create Rectangle from other of arbitrary type. --- src/Math/Geometry/Rectangle.h | 15 +++++++++++++++ src/Math/Geometry/Test/RectangleTest.cpp | 5 ++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Math/Geometry/Rectangle.h b/src/Math/Geometry/Rectangle.h index a8bda2d1c..3a0d848a2 100644 --- a/src/Math/Geometry/Rectangle.h +++ b/src/Math/Geometry/Rectangle.h @@ -42,6 +42,21 @@ template class Rectangle { return {bottomLeft, bottomLeft+size}; } + /** + * @brief %Rectangle from another of different type + * + * Performs only default casting on the values, no rounding or + * anything else. Example usage: + * @code + * Rectangle floatingPoint({1.3f, 2.7f}, {-15.0f, 7.0f}); + * auto integral = Rectangle::from(floatingPoint); + * // integral == {{1, 2}, {-15, 7}} + * @endcode + */ + template inline constexpr static Rectangle from(const Rectangle& other) { + return {Vector2::from(other.bottomLeft()), Vector2::from(other.topRight())}; + } + /** * @brief Default constructor * diff --git a/src/Math/Geometry/Test/RectangleTest.cpp b/src/Math/Geometry/Test/RectangleTest.cpp index a09ca0cc7..362bed166 100644 --- a/src/Math/Geometry/Test/RectangleTest.cpp +++ b/src/Math/Geometry/Test/RectangleTest.cpp @@ -23,7 +23,8 @@ CORRADE_TEST_MAIN(Magnum::Math::Geometry::Test::RectangleTest) namespace Magnum { namespace Math { namespace Geometry { namespace Test { -typedef Rectangle Rectanglei; +typedef Geometry::Rectangle Rectangle; +typedef Geometry::Rectangle Rectanglei; typedef Vector2 Vector2i; RectangleTest::RectangleTest() { @@ -61,6 +62,8 @@ void RectangleTest::compare() { void RectangleTest::construct() { CORRADE_COMPARE(Rectanglei(), Rectanglei({0, 0}, {0, 0})); CORRADE_COMPARE(Rectanglei::fromSize({3, 5}, {23, 78}), Rectanglei({3, 5}, {26, 83})); + CORRADE_COMPARE(Rectanglei::from(Rectangle({1.3f, 2.7f}, {-15.0f, 7.0f})), + Rectanglei({1, 2}, {-15, 7})); } void RectangleTest::size() {