Browse Source

Math: ability to create Rectangle from other of arbitrary type.

pull/7/head
Vladimír Vondruš 14 years ago
parent
commit
a7a4c28c03
  1. 15
      src/Math/Geometry/Rectangle.h
  2. 5
      src/Math/Geometry/Test/RectangleTest.cpp

15
src/Math/Geometry/Rectangle.h

@ -42,6 +42,21 @@ template<class T> 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<float> floatingPoint({1.3f, 2.7f}, {-15.0f, 7.0f});
* auto integral = Rectangle<std::int8_t>::from(floatingPoint);
* // integral == {{1, 2}, {-15, 7}}
* @endcode
*/
template<class U> inline constexpr static Rectangle<T> from(const Rectangle<U>& other) {
return {Vector2<T>::from(other.bottomLeft()), Vector2<T>::from(other.topRight())};
}
/**
* @brief Default constructor
*

5
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<std::int32_t> Rectanglei;
typedef Geometry::Rectangle<float> Rectangle;
typedef Geometry::Rectangle<std::int32_t> Rectanglei;
typedef Vector2<std::int32_t> 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() {

Loading…
Cancel
Save