From 8aade865717237437d07d758afa1b65c5d93fe96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 22 Feb 2013 18:12:54 +0100 Subject: [PATCH] Math: converting complex number to rotation matrix. --- src/Math/Complex.h | 21 +++++++++++++++++---- src/Math/Test/ComplexTest.cpp | 13 ++++++++++++- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/Math/Complex.h b/src/Math/Complex.h index 62053251c..cdd512df7 100644 --- a/src/Math/Complex.h +++ b/src/Math/Complex.h @@ -23,10 +23,7 @@ #include #include -#include "Math/Angle.h" -#include "Math/MathTypeTraits.h" - -#include "magnumVisibility.h" +#include "Math/Matrix.h" namespace Magnum { namespace Math { @@ -125,6 +122,22 @@ template class Complex { return Rad(std::atan2(_imaginary, _real)); } + /** + * @brief Convert complex number to rotation matrix + * + * @f[ + * M = \begin{pmatrix} + * a & -b \\ + * b & a + * \end{pmatrix} + * @f] + * @see Matrix3::from(const Matrix<2, T>&, const Vector2&) + */ + Matrix<2, T> matrix() const { + return {Vector<2, T>(_real, _imaginary), + Vector<2, T>(-_imaginary, _real)}; + } + /** * @brief Add complex number and assign * diff --git a/src/Math/Test/ComplexTest.cpp b/src/Math/Test/ComplexTest.cpp index c0a25c62a..c414a8f13 100644 --- a/src/Math/Test/ComplexTest.cpp +++ b/src/Math/Test/ComplexTest.cpp @@ -17,7 +17,7 @@ #include #include "Math/Complex.h" -#include "Math/Vector2.h" +#include "Math/Matrix3.h" namespace Magnum { namespace Math { namespace Test { @@ -47,6 +47,7 @@ class ComplexTest: public Corrade::TestSuite::Tester { void angle(); void rotation(); + void matrix(); void debug(); }; @@ -74,6 +75,7 @@ ComplexTest::ComplexTest() { &ComplexTest::angle, &ComplexTest::rotation, + &ComplexTest::matrix, &ComplexTest::debug); } @@ -82,6 +84,8 @@ typedef Math::Deg Deg; typedef Math::Rad Rad; typedef Math::Complex Complex; typedef Math::Vector2 Vector2; +typedef Math::Matrix3 Matrix3; +typedef Math::Matrix<2, float> Matrix2; void ComplexTest::construct() { Complex c(0.5f, -3.7f); @@ -245,6 +249,13 @@ void ComplexTest::rotation() { CORRADE_COMPARE_AS(Complex().rotationAngle(), Deg(0.0f), Rad); } +void ComplexTest::matrix() { + Complex a = Complex::rotation(Deg(37.0f)); + Matrix2 m = Matrix3::rotation(Deg(37.0f)).rotationScaling(); + + CORRADE_COMPARE(a.matrix(), m); +} + void ComplexTest::debug() { std::ostringstream o;