Browse Source

Math: added Vector2::perpendicular().

Also updated and crosslinked related documentation.
pull/278/head
Vladimír Vondruš 13 years ago
parent
commit
49154f30c4
  1. 9
      src/Math/Test/Vector2Test.cpp
  2. 7
      src/Math/Vector.h
  3. 10
      src/Math/Vector2.h

9
src/Math/Test/Vector2Test.cpp

@ -66,6 +66,7 @@ class Vector2Test: public Corrade::TestSuite::Tester {
void cross();
void axes();
void scales();
void perpendicular();
void debug();
void configuration();
@ -88,6 +89,7 @@ Vector2Test::Vector2Test() {
&Vector2Test::cross,
&Vector2Test::axes,
&Vector2Test::scales,
&Vector2Test::perpendicular,
&Vector2Test::debug,
&Vector2Test::configuration});
@ -187,6 +189,13 @@ void Vector2Test::scales() {
CORRADE_COMPARE(y, Vector2(1.0f, -0.2f));
}
void Vector2Test::perpendicular() {
const Vector2 a(0.5f, -15.0f);
CORRADE_COMPARE(a.perpendicular(), Vector2(15.0f, 0.5f));
CORRADE_COMPARE(Vector2::dot(a.perpendicular(), a), 0.0f);
CORRADE_COMPARE(Vector2::xAxis().perpendicular(), Vector2::yAxis());
}
void Vector2Test::debug() {
std::ostringstream o;
Debug(&o) << Vector2(0.5f, 15.0f);

7
src/Math/Vector.h

@ -84,10 +84,12 @@ template<std::size_t size, class T> class Vector {
/**
* @brief Dot product
*
* @f[
* Returns `0` if two vectors are orthogonal, `1` if two *normalized*
* vectors are parallel and `-1` if two *normalized* vectors are
* antiparallel. @f[
* \boldsymbol a \cdot \boldsymbol b = \sum_{i=0}^{n-1} \boldsymbol a_i \boldsymbol b_i
* @f]
* @see dot() const
* @see dot() const, operator-(), Vector2::perpendicular()
*/
inline static T dot(const Vector<size, T>& a, const Vector<size, T>& b) {
return (a*b).sum();
@ -270,6 +272,7 @@ template<std::size_t size, class T> class Vector {
* The computation is done in-place. @f[
* \boldsymbol a_i = -\boldsymbol a_i
* @f]
* @see Vector2::perpendicular()
*/
Vector<size, T> operator-() const {
Vector<size, T> out;

10
src/Math/Vector2.h

@ -123,6 +123,16 @@ template<class T> class Vector2: public Vector<2, T> {
inline T& y() { return (*this)[1]; } /**< @brief Y component */
inline constexpr T y() const { return (*this)[1]; } /**< @overload */
/**
* @brief Perpendicular vector
*
* Returns vector rotated 90° counterclockwise. @f[
* \boldsymbol v_\perp = \begin{pmatrix} -v_y \\ v_x \end{pmatrix}
* @f]
* @see dot(const Vector&, const Vector&), operator-() const
*/
inline Vector2<T> perpendicular() const { return {-y(), x()}; }
MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(Vector2, 2)
};

Loading…
Cancel
Save