Browse Source

Math: converting Vector2 to Complex and back.

pull/7/head
Vladimír Vondruš 13 years ago
parent
commit
93952cd72a
  1. 22
      src/Math/Complex.h
  2. 10
      src/Math/Test/ComplexTest.cpp

22
src/Math/Complex.h

@ -24,6 +24,7 @@
#include <Utility/Debug.h>
#include "Math/Matrix.h"
#include "Math/Vector2.h"
namespace Magnum { namespace Math {
@ -94,6 +95,16 @@ template<class T> class Complex {
*/
inline constexpr /*implicit*/ Complex(T real, T imaginary = T(0)): _real(real), _imaginary(imaginary) {}
/**
* @brief Construct complex number from vector
*
* To be used in transformations later. @f[
* c = v_x + iv_y
* @f]
* @see operator Vector2(), transformVector(), transformVectorNormalized()
*/
inline constexpr explicit Complex(const Vector2<T>& vector): _real(vector.x()), _imaginary(vector.y()) {}
/** @brief Equality comparison */
inline bool operator==(const Complex<T>& other) const {
return MathTypeTraits<T>::equals(_real, other._real) &&
@ -111,6 +122,17 @@ template<class T> class Complex {
/** @brief Imaginary part */
inline constexpr T imaginary() const { return _imaginary; }
/**
* @brief Convert complex number to vector
*
* @f[
* \boldsymbol v = \begin{pmatrix} a \\ b \end{pmatrix}
* @f]
*/
inline constexpr explicit operator Vector2<T>() const {
return {_real, _imaginary};
}
/**
* @brief Rotation angle of complex number
*

10
src/Math/Test/ComplexTest.cpp

@ -27,6 +27,7 @@ class ComplexTest: public Corrade::TestSuite::Tester {
void construct();
void constructDefault();
void constructFromVector();
void compare();
void constExpressions();
@ -55,6 +56,7 @@ class ComplexTest: public Corrade::TestSuite::Tester {
ComplexTest::ComplexTest() {
addTests(&ComplexTest::construct,
&ComplexTest::constructDefault,
&ComplexTest::constructFromVector,
&ComplexTest::compare,
&ComplexTest::constExpressions,
@ -100,6 +102,14 @@ void ComplexTest::constructDefault() {
CORRADE_COMPARE(Complex().length(), 1.0f);
}
void ComplexTest::constructFromVector() {
Vector2 vec(1.5f, -3.0f);
Complex a(vec);
CORRADE_COMPARE(a, Complex(1.5f, -3.0f));
CORRADE_COMPARE(Vector2(a), vec);
}
void ComplexTest::compare() {
CORRADE_VERIFY(Complex(3.7f, -1.0f+MathTypeTraits<float>::epsilon()/2) == Complex(3.7f, -1.0f));
CORRADE_VERIFY(Complex(3.7f, -1.0f+MathTypeTraits<float>::epsilon()*2) != Complex(3.7f, -1.0f));

Loading…
Cancel
Save