MyGAL
Vector2.h
1 /* MyGAL
2  * Copyright (C) 2019 Pierre Vigier
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #pragma once
19 
20 // STL
21 #include <iosfwd>
22 #include <cmath>
23 
27 namespace mygal
28 {
29 
30 // Declarations
31 
32 template<typename T>
33 class Vector2;
34 template<typename T>
35 Vector2<T> operator-(Vector2<T> lhs, const Vector2<T>& rhs);
36 
37 // Implementations
38 
46 template<typename T>
47 class Vector2
48 {
49 public:
50  T x;
51  T y;
59  Vector2<T>(T x = 0.0, T y = 0.0) : x(x), y(y)
60  {
61 
62  }
63 
64  // Unary operators
65 
73  {
74  return Vector2<T>(-x, -y);
75  }
76 
85  {
86  x += other.x;
87  y += other.y;
88  return *this;
89  }
90 
99  {
100  x -= other.x;
101  y -= other.y;
102  return *this;
103  }
104 
113  {
114  x *= t;
115  y *= t;
116  return *this;
117  }
118 
119  // Other operations
120 
127  {
128  return Vector2<T>(-y, x);
129  }
130 
136  T getNorm() const
137  {
138  return std::sqrt(x * x + y * y);
139  }
140 
150  T getDistance(const Vector2<T>& other) const
151  {
152  return (*this - other).getNorm();
153  }
154 
162  T getDet(const Vector2<T>& other) const
163  {
164  return x * other.y - y * other.x;
165  }
166 };
167 
168 // Binary operators
169 
179 template<typename T>
181 {
182  lhs += rhs;
183  return lhs;
184 }
185 
195 template<typename T>
197 {
198  lhs -= rhs;
199  return lhs;
200 }
201 
211 template<typename T>
213 {
214  vec *= t;
215  return vec;
216 }
217 
227 template<typename T>
229 {
230  return t * vec;
231 }
232 
242 template<typename T>
243 std::ostream& operator<<(std::ostream& os, const Vector2<T>& vec)
244 {
245  os << "(" << vec.x << ", " << vec.y << ")";
246  return os;
247 }
248 
249 }
T getNorm() const
Compute the euclidean norm of the vector.
Definition: Vector2.h:136
Vector2< T > operator*(T t, Vector2< T > vec)
Compute the product of a scalar by a vector.
Definition: Vector2.h:212
Vector2< T > getOrthogonal() const
Get the orthogonal vector.
Definition: Vector2.h:126
Class representing a 2D vector.
Definition: Vector2.h:33
Namespace of MyGAL.
Definition: Box.h:29
Vector2< T > operator*(Vector2< T > vec, T t)
Compute the product of a vector by a scalar.
Definition: Vector2.h:228
Vector2< T > operator+(Vector2< T > lhs, const Vector2< T > &rhs)
Compute the sum of two vectors.
Definition: Vector2.h:180
Vector2< T > operator-(Vector2< T > lhs, const Vector2< T > &rhs)
Compute the difference of two vectors.
Definition: Vector2.h:196
Vector2< T > & operator-=(const Vector2< T > &other)
Substract a vector.
Definition: Vector2.h:98
Vector2< T > & operator*=(T t)
Multiply by a scalar.
Definition: Vector2.h:112
T getDistance(const Vector2< T > &other) const
Compute the euclidean distance of this point to another point.
Definition: Vector2.h:150
Vector2< T > operator-() const
Compute the opposite vector.
Definition: Vector2.h:72
T getDet(const Vector2< T > &other) const
Compute the determinant with another vector.
Definition: Vector2.h:162
Vector2< T > & operator+=(const Vector2< T > &other)
Add a vector.
Definition: Vector2.h:84