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