Rivet  3.1.3
Vector2.hh
1 #ifndef RIVET_MATH_VECTOR2
2 #define RIVET_MATH_VECTOR2
3 
4 #include "Rivet/Math/MathConstants.hh"
5 #include "Rivet/Math/MathUtils.hh"
6 #include "Rivet/Math/VectorN.hh"
7 
8 namespace Rivet {
9 
10 
11  class Vector2;
12  typedef Vector2 TwoVector;
13  //class Matrix2;
14 
15  Vector2 multiply(const double, const Vector2&);
16  Vector2 multiply(const Vector2&, const double);
17  Vector2 add(const Vector2&, const Vector2&);
18  Vector2 operator*(const double, const Vector2&);
19  Vector2 operator*(const Vector2&, const double);
20  Vector2 operator/(const Vector2&, const double);
21  Vector2 operator+(const Vector2&, const Vector2&);
22  Vector2 operator-(const Vector2&, const Vector2&);
23 
24 
26  class Vector2 : public Vector<2> {
27 
28  // friend class Matrix2;
29  friend Vector2 multiply(const double, const Vector2&);
30  friend Vector2 multiply(const Vector2&, const double);
31  friend Vector2 add(const Vector2&, const Vector2&);
32  friend Vector2 subtract(const Vector2&, const Vector2&);
33 
34  public:
35  Vector2() : Vector<2>() { }
36 
37  template<typename V2>
38  Vector2(const V2& other) {
39  this->setX(other.x());
40  this->setY(other.y());
41  }
42 
43  Vector2(const Vector<2>& other) {
44  this->setX(other.get(0));
45  this->setY(other.get(1));
46  }
47 
48  Vector2(double x, double y) {
49  this->setX(x);
50  this->setY(y);
51  }
52 
53  ~Vector2() { }
54 
55 
56  public:
57 
58  static Vector2 mkX() { return Vector2(1,0); }
59  static Vector2 mkY() { return Vector2(0,1); }
60 
61 
62  public:
63 
64  double x() const { return get(0); }
65  double y() const { return get(1); }
66  Vector2& setX(double x) { set(0, x); return *this; }
67  Vector2& setY(double y) { set(1, y); return *this; }
68 
69 
71  double dot(const Vector2& v) const {
72  return _vec.dot(v._vec);
73  }
74 
76  double angle(const Vector2& v) const {
77  const double localDotOther = unit().dot(v.unit());
78  if (localDotOther > 1.0) return 0.0;
79  if (localDotOther < -1.0) return M_PI;
80  return acos(localDotOther);
81  }
82 
83 
85  Vector2 unitVec() const {
86  double md = mod();
87  if ( md <= 0.0 ) return Vector2();
88  else return *this * 1.0/md;
89  }
90 
92  Vector2 unit() const {
93  return unitVec();
94  }
95 
96 
97  public:
98  Vector2& operator*=(const double a) {
99  _vec = multiply(a, *this)._vec;
100  return *this;
101  }
102 
103  Vector2& operator/=(const double a) {
104  _vec = multiply(1.0/a, *this)._vec;
105  return *this;
106  }
107 
108  Vector2& operator+=(const Vector2& v) {
109  _vec = add(*this, v)._vec;
110  return *this;
111  }
112 
113  Vector2& operator-=(const Vector2& v) {
114  _vec = subtract(*this, v)._vec;
115  return *this;
116  }
117 
118  Vector2 operator-() const {
119  Vector2 rtn;
120  rtn._vec = -_vec;
121  return rtn;
122  }
123 
124  };
125 
126 
127 
128  inline double dot(const Vector2& a, const Vector2& b) {
129  return a.dot(b);
130  }
131 
132  inline Vector2 multiply(const double a, const Vector2& v) {
133  Vector2 result;
134  result._vec = a * v._vec;
135  return result;
136  }
137 
138  inline Vector2 multiply(const Vector2& v, const double a) {
139  return multiply(a, v);
140  }
141 
142  inline Vector2 operator*(const double a, const Vector2& v) {
143  return multiply(a, v);
144  }
145 
146  inline Vector2 operator*(const Vector2& v, const double a) {
147  return multiply(a, v);
148  }
149 
150  inline Vector2 operator/(const Vector2& v, const double a) {
151  return multiply(1.0/a, v);
152  }
153 
154  inline Vector2 add(const Vector2& a, const Vector2& b) {
155  Vector2 result;
156  result._vec = a._vec + b._vec;
157  return result;
158  }
159 
160  inline Vector2 subtract(const Vector2& a, const Vector2& b) {
161  Vector2 result;
162  result._vec = a._vec - b._vec;
163  return result;
164  }
165 
166  inline Vector2 operator+(const Vector2& a, const Vector2& b) {
167  return add(a, b);
168  }
169 
170  inline Vector2 operator-(const Vector2& a, const Vector2& b) {
171  return subtract(a, b);
172  }
173 
175  inline double angle(const Vector2& a, const Vector2& b) {
176  return a.angle(b);
177  }
178 
179 
180 }
181 
182 #endif
Definition: MC_Cent_pPb.hh:10
double angle(const Vector2 &v) const
Angle in radians to another vector.
Definition: Vector2.hh:76
string operator/(const string &a, const string &b)
Operator for joining strings a and b with filesystem separators.
Definition: Utils.hh:260
Vector2 unit() const
Synonym for unitVec.
Definition: Vector2.hh:92
Vector2 unitVec() const
Unit-normalized version of this vector.
Definition: Vector2.hh:85
Two-dimensional specialisation of Vector.
Definition: Vector2.hh:26
A minimal base class for -dimensional vectors.
Definition: VectorN.hh:13
double dot(const Vector2 &v) const
Dot-product with another vector.
Definition: Vector2.hh:71
double mod() const
Calculate the modulus of a vector. .
Definition: VectorN.hh:95