Browse Source

Math: added utility function Vector2::aspectRatio().

Consider this craziness when setting up projection or something similar:

    Vector2i framebufferSize;
    Float aspectRatio = Float(framebufferSize.x())/framebufferSize.y();

And now, behold, the convenience:

    Float aspectRatio = Vector2(framebufferSize).aspectRatio();
pull/23/head
Vladimír Vondruš 13 years ago
parent
commit
0dd531b978
  1. 7
      src/Math/Test/Vector2Test.cpp
  2. 9
      src/Math/Vector2.h

7
src/Math/Test/Vector2Test.cpp

@ -67,6 +67,7 @@ class Vector2Test: public Corrade::TestSuite::Tester {
void axes();
void scales();
void perpendicular();
void aspectRatio();
void debug();
void configuration();
@ -90,6 +91,7 @@ Vector2Test::Vector2Test() {
&Vector2Test::axes,
&Vector2Test::scales,
&Vector2Test::perpendicular,
&Vector2Test::aspectRatio,
&Vector2Test::debug,
&Vector2Test::configuration});
@ -196,6 +198,11 @@ void Vector2Test::perpendicular() {
CORRADE_COMPARE(Vector2::xAxis().perpendicular(), Vector2::yAxis());
}
void Vector2Test::aspectRatio() {
const Vector2 a(3.0f, 4.0f);
CORRADE_COMPARE(a.aspectRatio(), 0.75f);
}
void Vector2Test::debug() {
std::ostringstream o;
Debug(&o) << Vector2(0.5f, 15.0f);

9
src/Math/Vector2.h

@ -134,6 +134,15 @@ template<class T> class Vector2: public Vector<2, T> {
*/
Vector2<T> perpendicular() const { return {-y(), x()}; }
/**
* @brief Aspect ratio
*
* Returns quotient of the two elements. @f[
* a = \frac{v_x}{v_y}
* @f]
*/
T aspectRatio() const { return x()/y(); }
MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(2, Vector2)
};

Loading…
Cancel
Save