From 0dd531b9789b2413ac7a9d2252e7a1773f8d53b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 20 Oct 2013 18:54:22 +0200 Subject: [PATCH] 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(); --- src/Math/Test/Vector2Test.cpp | 7 +++++++ src/Math/Vector2.h | 9 +++++++++ 2 files changed, 16 insertions(+) diff --git a/src/Math/Test/Vector2Test.cpp b/src/Math/Test/Vector2Test.cpp index 1ef3f77ba..b1f5c9fe4 100644 --- a/src/Math/Test/Vector2Test.cpp +++ b/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); diff --git a/src/Math/Vector2.h b/src/Math/Vector2.h index f55483b7d..2fb887fd8 100644 --- a/src/Math/Vector2.h +++ b/src/Math/Vector2.h @@ -134,6 +134,15 @@ template class Vector2: public Vector<2, T> { */ Vector2 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) };