mu
vector2d.h
Go to the documentation of this file.
1 
6 #ifndef MU_VECTOR2D_H_
7 #define MU_VECTOR2D_H_
8 
9 #include "mu/vector.h"
10 
11 namespace mu {
12 
18 template <typename T>
19 class Vector2D : public Vector<2, T> {
20  public:
21  /* inherit base class constructors. only constructors specific to Vector2D
22  * are defined here. apply rule of zero */
24 
33  template <class Tt = T>
34  // NOLINTNEXTLINE(runtime/explicit) implicit to make copy-init. work
35  Vector2D(const Vector<2, Tt>& other) : Vector<2, T>(other) {}
36 
44  T& x() noexcept { return Vector<2, T>::data_[0]; }
45 
53  const T& x() const noexcept { return Vector<2, T>::data_[0]; }
54 
62  T& y() noexcept { return Vector<2, T>::data_[1]; }
63 
71  const T& y() const noexcept { return Vector<2, T>::data_[1]; }
72 
84  template <class TScalar = T>
85  typename std::enable_if_t<std::is_arithmetic<TScalar>::value, void> rotate(
86  TScalar angle) {
87  const T kX = x();
88  const T kY = y();
89  Vector<2, T>::data_[0] = ((kX * mu::cos(angle)) - (kY * mu::sin(angle)));
90  Vector<2, T>::data_[1] = ((kX * mu::sin(angle)) + (kY * mu::cos(angle)));
91  }
92 
104  template <class TScalar = T>
105  typename std::enable_if_t<std::is_arithmetic<TScalar>::value, Vector2D<T>>
106  rotated(TScalar angle) {
107  Vector2D<T> ret(*this);
108  ret.rotate(angle);
109  return ret;
110  }
111 };
112 
113 } // namespace mu
114 #endif // MU_VECTOR2D_H_
A generic vector.
Definition: vector.h:48
const T & y() const noexcept
const y component
Definition: vector2d.h:71
T & x() noexcept
x component
Definition: vector2d.h:44
Vector2D(const Vector< 2, Tt > &other)
Construct a new Vector2D object from a Vector object.
Definition: vector2d.h:35
const T & x() const noexcept
const x component
Definition: vector2d.h:53
std::enable_if_t< std::is_arithmetic< TScalar >::value, Vector2D< T > > rotated(TScalar angle)
returns a Vector2D that is rotated by an angle [rad]
Definition: vector2d.h:106
A two dimensional vector.
Definition: vector2d.h:19
Definition: literals.h:11
T & y() noexcept
y component
Definition: vector2d.h:62
std::enable_if_t< std::is_arithmetic< TScalar >::value, void > rotate(TScalar angle)
rotates this Vector by an angle [rad]
Definition: vector2d.h:85