simplevectors  0.3.9
Simple vector implementations in C++
vector2d.hpp
Go to the documentation of this file.
1 
10 #ifndef INCLUDE_SVECTOR_VECTOR2D_HPP_
11 #define INCLUDE_SVECTOR_VECTOR2D_HPP_
12 
13 #include <cmath> // std::atan2, std::cos, std::sin
14 
15 #include "simplevectors/core/vector.hpp" // svector::Vector
16 
17 namespace svector {
18 // COMBINER_PY_START
19 
20 typedef Vector<2> Vec2_;
21 
25 class Vector2D : public Vec2_ {
26 public:
27  using Vec2_::Vector;
28 
35  Vector2D(const double x, const double y) {
36  this->m_components[0] = x;
37  this->m_components[1] = y;
38  }
39 
43  Vector2D(const Vec2_ &other) {
44  this->m_components[0] = other[0];
45  this->m_components[1] = other[1];
46  }
47 
55  double x() const { return this->m_components[0]; }
56 
64  void x(const double &newX) { this->m_components[0] = newX; }
65 
73  double y() const { return this->m_components[1]; }
74 
82  void y(const double &newY) { this->m_components[1] = newY; }
83 
93  double angle() const { return std::atan2(this->y(), this->x()); }
94 
106  Vector2D rotate(const double ang) const {
107  //
108  // Rotation matrix:
109  //
110  // | cos(ang) -sin(ang) | |x|
111  // | sin(ang) cos(ang) | |y|
112  //
113 
114  const double xPrime = this->x() * std::cos(ang) - this->y() * std::sin(ang);
115  const double yPrime = this->x() * std::sin(ang) + this->y() * std::cos(ang);
116 
117  return Vector2D{xPrime, yPrime};
118  }
119 
132  template <typename T> T componentsAs() const {
133  return T{this->x(), this->y()};
134  }
135 };
136 // COMBINER_PY_END
137 } // namespace svector
138 
139 #endif
Contains a base vector representation.
A base vector representation.
Definition: vector.hpp:34
Vector()
No-argument constructor.
Definition: vector.hpp:67
std::array< T, D > m_components
An array of components for the vector.
Definition: vector.hpp:630
A simple 2D vector representation.
Definition: vector2d.hpp:25
double angle() const
Angle of vector.
Definition: vector2d.hpp:93
T componentsAs() const
Converts vector to another object.
Definition: vector2d.hpp:132
double x() const
Gets x-component.
Definition: vector2d.hpp:55
Vector2D rotate(const double ang) const
Rotates vector by a certain angle.
Definition: vector2d.hpp:106
Vector2D(const Vec2_ &other)
Copy constructor for base class.
Definition: vector2d.hpp:43
void y(const double &newY)
Sets y-component.
Definition: vector2d.hpp:82
Vector2D(const double x, const double y)
Initializes a vector given xy components.
Definition: vector2d.hpp:35
void x(const double &newX)
Sets x-component.
Definition: vector2d.hpp:64
double y() const
Gets y-component.
Definition: vector2d.hpp:73