Browse Source

Math: rotation complex number.

pull/7/head
Vladimír Vondruš 13 years ago
parent
commit
e7ceddf0fc
  1. 24
      src/Math/Complex.h
  2. 3
      src/Math/Matrix3.h
  3. 6
      src/Math/Quaternion.h
  4. 17
      src/Math/Test/ComplexTest.cpp

24
src/Math/Complex.h

@ -66,6 +66,19 @@ template<class T> class Complex {
return Rad<T>(std::acos(normalizedA._real*normalizedB._real + normalizedA._imaginary*normalizedB._imaginary));
}
/**
* @brief Rotation complex number
* @param angle Rotation angle (counterclockwise)
*
* @f[
* c = cos \theta + i sin \theta
* @f]
* @see rotationAngle(), Matrix3::rotation(), Quaternion::rotation()
*/
inline static Complex<T> rotation(Rad<T> angle) {
return {std::cos(T(angle)), std::sin(T(angle))};
}
/**
* @brief Default constructor
*
@ -101,6 +114,17 @@ template<class T> class Complex {
/** @brief Imaginary part */
inline constexpr T imaginary() const { return _imaginary; }
/**
* @brief Rotation angle of complex number
*
* @f[
* \theta = atan2(b, a)
* @f]
*/
inline Rad<T> rotationAngle() const {
return Rad<T>(std::atan2(_imaginary, _real));
}
/**
* @brief Add complex number and assign
*

3
src/Math/Matrix3.h

@ -64,7 +64,8 @@ template<class T> class Matrix3: public Matrix<3, T> {
* @brief 2D rotation matrix
* @param angle Rotation angle (counterclockwise)
*
* @see rotation() const, Matrix4::rotation(Rad, const Vector3&)
* @see rotation() const, Complex::rotation(),
* Matrix4::rotation(Rad, const Vector3&)
*/
static Matrix3<T> rotation(Rad<T> angle) {
T sine = std::sin(T(angle));

6
src/Math/Quaternion.h

@ -108,15 +108,15 @@ template<class T> class Quaternion {
/**
* @brief Rotation quaternion
* @param angle Rotation angle (counterclockwise, in radians)
* @param angle Rotation angle (counterclockwise)
* @param normalizedAxis Normalized rotation axis
*
* Expects that the rotation axis is normalized. @f[
* q = [\boldsymbol a \cdot sin \frac \theta 2, cos \frac \theta 2]
* @f]
* @see rotationAngle(), rotationAxis(), DualQuaternion::rotation(),
* Matrix4::rotation(), Vector3::xAxis(), Vector3::yAxis(),
* Vector3::zAxis()
* Matrix4::rotation(), Complex::rotation(), Vector3::xAxis(),
* Vector3::yAxis(), Vector3::zAxis()
*/
inline static Quaternion<T> rotation(Rad<T> angle, const Vector3<T>& normalizedAxis) {
CORRADE_ASSERT(MathTypeTraits<T>::equals(normalizedAxis.dot(), T(1)),

17
src/Math/Test/ComplexTest.cpp

@ -46,6 +46,7 @@ class ComplexTest: public Corrade::TestSuite::Tester {
void invertedNormalized();
void angle();
void rotation();
void debug();
};
@ -72,10 +73,12 @@ ComplexTest::ComplexTest() {
&ComplexTest::invertedNormalized,
&ComplexTest::angle,
&ComplexTest::rotation,
&ComplexTest::debug);
}
typedef Math::Deg<float> Deg;
typedef Math::Rad<float> Rad;
typedef Math::Complex<float> Complex;
typedef Math::Vector2<float> Vector2;
@ -228,6 +231,20 @@ void ComplexTest::angle() {
CORRADE_COMPARE(angle, Rad(2.933128f));
}
void ComplexTest::rotation() {
Complex a = Complex::rotation(Deg(120.0f));
CORRADE_COMPARE(a, Complex(-0.5f, 0.8660254f));
CORRADE_COMPARE_AS(a.rotationAngle(), Deg(120.0f), Rad);
/* Verify negative angle */
Complex b = Complex::rotation(Deg(-240.0f));
CORRADE_COMPARE(b, Complex(-0.5f, 0.8660254f));
CORRADE_COMPARE_AS(b.rotationAngle(), Deg(120.0f), Rad);
/* Default-constructed complex number has zero angle */
CORRADE_COMPARE_AS(Complex().rotationAngle(), Deg(0.0f), Rad);
}
void ComplexTest::debug() {
std::ostringstream o;

Loading…
Cancel
Save