]> git.localhorst.tv Git - l2e.git/blob - src/geometry/Vector.h
merged Point into Vector
[l2e.git] / src / geometry / Vector.h
1 /*
2  * Vector.h
3  *
4  *  Created on: Aug 6, 2012
5  *      Author: holy
6  */
7
8 #ifndef GEOMETRY_VECTOR_H_
9 #define GEOMETRY_VECTOR_H_
10
11 #include <ostream>
12
13 namespace geometry {
14
15 template<class Scalar>
16 class Vector {
17
18 public:
19         Vector() : x(0), y(0) { }
20         Vector(Scalar x, Scalar y) : x(x), y(y) { }
21         template<class T>
22         Vector(const Vector<T> &other) : x(other.X()), y(other.Y()) { };
23         template<class T>
24         Vector(T x, T y) : x(x), y(y) { }
25
26 public:
27         Scalar X() const { return x; }
28         Scalar Y() const { return y; }
29
30 private:
31         Scalar x, y;
32
33 };
34
35
36 template<class T>
37 inline Vector<T> operator +(const Vector<T> &lhs, const Vector<T> &rhs) {
38         return Vector<T>(lhs.X() + rhs.X(), lhs.Y() + rhs.Y());
39 }
40
41 template<class T>
42 inline Vector<T> &operator +=(Vector<T> &lhs, const Vector<T> &rhs) {
43         return lhs = lhs + rhs;
44 }
45
46 template<class T>
47 inline Vector<T> operator -(const Vector<T> &lhs, const Vector<T> &rhs) {
48         return Vector<T>(lhs.X() - rhs.X(), lhs.Y() - rhs.Y());
49 }
50
51 template<class T>
52 inline Vector<T> &operator -=(Vector<T> &lhs, const Vector<T> &rhs) {
53         return lhs = lhs - rhs;
54 }
55
56 template<class T>
57 inline Vector<T> operator -(const Vector<T> &v) {
58         return Vector<T>(-v.X(), -v.Y());
59 }
60
61 template<class T>
62 inline Vector<T> operator *(const Vector<T> &v, T s) {
63         return Vector<T>(v.X() * s, v.Y() * s);
64 }
65 template<class T>
66 inline Vector<T> operator *(T s, const Vector<T> &v) {
67         return Vector<T>(s * v.X(), s * v.Y());
68 }
69
70 template<class T>
71 inline Vector<T> operator /(const Vector<T> &v, T s) {
72         return Vector<T>(v.X() / s, v.Y() / s);
73 }
74
75 template<class T>
76 inline std::ostream &operator <<(std::ostream &out, const Vector<T> &v) {
77         out << '<' << v.X() << ", " << v.Y() << '>';
78         return out;
79 }
80
81 }
82
83 #endif /* GEOMETRY_VECTOR_H_ */