Browse Source

Math: Added Vector2::cross().

pull/278/head
Vladimír Vondruš 13 years ago
parent
commit
0cd98456da
  1. 13
      src/Math/Test/Vector2Test.cpp
  2. 6
      src/Math/Test/Vector3Test.cpp
  3. 14
      src/Math/Vector2.h
  4. 8
      src/Math/Vector3.h

13
src/Math/Test/Vector2Test.cpp

@ -26,7 +26,7 @@
#include <TestSuite/Tester.h>
#include <Utility/Configuration.h>
#include "Math/Vector2.h"
#include "Math/Vector3.h"
struct Vec2 {
float x, y;
@ -63,6 +63,7 @@ class Vector2Test: public Corrade::TestSuite::Tester {
void convert();
void access();
void cross();
void axes();
void scales();
@ -70,6 +71,7 @@ class Vector2Test: public Corrade::TestSuite::Tester {
void configuration();
};
typedef Math::Vector3<Int> Vector3i;
typedef Math::Vector2<Float> Vector2;
typedef Math::Vector2<Int> Vector2i;
@ -83,6 +85,7 @@ Vector2Test::Vector2Test() {
&Vector2Test::convert,
&Vector2Test::access,
&Vector2Test::cross,
&Vector2Test::axes,
&Vector2Test::scales,
@ -162,6 +165,14 @@ void Vector2Test::access() {
CORRADE_COMPARE(y, -2.0f);
}
void Vector2Test::cross() {
Vector2i a(1, -1);
Vector2i b(4, 3);
CORRADE_COMPARE(Vector2i::cross(a, b), 7);
CORRADE_COMPARE(Vector3i::cross({a, 0}, {b, 0}), Vector3i(0, 0, Vector2i::cross(a, b)));
}
void Vector2Test::axes() {
constexpr Vector2 x = Vector2::xAxis(5.0f);
constexpr Vector2 y = Vector2::yAxis(6.0f);

6
src/Math/Test/Vector3Test.cpp

@ -180,10 +180,10 @@ void Vector3Test::access() {
}
void Vector3Test::cross() {
Vector3 a(1, -1, 1);
Vector3 b(4, 3, 7);
Vector3i a(1, -1, 1);
Vector3i b(4, 3, 7);
CORRADE_COMPARE(Vector3::cross(a, b), Vector3(-10, -3, 7));
CORRADE_COMPARE(Vector3i::cross(a, b), Vector3i(-10, -3, 7));
}
void Vector3Test::axes() {

14
src/Math/Vector2.h

@ -80,6 +80,20 @@ template<class T> class Vector2: public Vector<2, T> {
*/
inline constexpr static Vector2<T> yScale(T scale) { return Vector2<T>(T(1), scale); }
/**
* @brief 2D cross product
*
* 2D version of cross product, equivalent to calling Vector3::cross()
* with Z coordinate set to `0` and extracting only Z coordinate from
* the result (X and Y coordinates are always zero).
* @f[
* \boldsymbol a \times \boldsymbol b = a_xb_y - a_yb_x
* @f]
*/
inline static T cross(const Vector2<T>& a, const Vector2<T>& b) {
return a.x()*b.y() - a.y()*b.x();
}
/** @copydoc Vector::Vector() */
inline constexpr /*implicit*/ Vector2() {}

8
src/Math/Vector3.h

@ -102,12 +102,12 @@ template<class T> class Vector3: public Vector<3, T> {
* @brief Cross product
*
* @f[
* \boldsymbol a \times \boldsymbol b =
* \begin{pmatrix} c_0 \\ c_1 \\ c_2 \end{pmatrix} =
* \begin{pmatrix}a_1b_2 - a_2b_1 \\ a_2b_0 - a_0b_2 \\ a_0b_1 - a_1b_0 \end{pmatrix}
* \boldsymbol a \times \boldsymbol b =
* \begin{pmatrix}a_yb_z - a_zb_y \\ a_zb_y - a_xb_z \\ a_xb_y - a_yb_x \end{pmatrix}
* @f]
* @see Vector2::cross()
*/
inline constexpr static Vector3<T> cross(const Vector3<T>& a, const Vector3<T>& b) {
inline static Vector3<T> cross(const Vector3<T>& a, const Vector3<T>& b) {
return swizzle<'y', 'z', 'x'>(a)*swizzle<'z', 'x', 'y'>(b) -
swizzle<'z', 'x', 'y'>(a)*swizzle<'y', 'z', 'x'>(b);
}

Loading…
Cancel
Save