]> git.localhorst.tv Git - l2e.git/blob - src/geometry/Vector.h
added mixed-type operators to Vector template
[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 template<class T, class U>
41 inline Vector<T> operator +(const Vector<T> &lhs, const Vector<U> &rhs) {
42         return Vector<T>(lhs.X() + rhs.X(), lhs.Y() + rhs.Y());
43 }
44
45 template<class T>
46 inline Vector<T> &operator +=(Vector<T> &lhs, const Vector<T> &rhs) {
47         return lhs = lhs + rhs;
48 }
49 template<class T, class U>
50 inline Vector<T> &operator +=(Vector<T> &lhs, const Vector<U> &rhs) {
51         return lhs = lhs + rhs;
52 }
53
54 template<class T>
55 inline Vector<T> operator -(const Vector<T> &lhs, const Vector<T> &rhs) {
56         return Vector<T>(lhs.X() - rhs.X(), lhs.Y() - rhs.Y());
57 }
58 template<class T, class U>
59 inline Vector<T> operator -(const Vector<T> &lhs, const Vector<U> &rhs) {
60         return Vector<T>(lhs.X() - rhs.X(), lhs.Y() - rhs.Y());
61 }
62
63 template<class T>
64 inline Vector<T> &operator -=(Vector<T> &lhs, const Vector<T> &rhs) {
65         return lhs = lhs - rhs;
66 }
67 template<class T, class U>
68 inline Vector<T> &operator -=(Vector<T> &lhs, const Vector<U> &rhs) {
69         return lhs = lhs - rhs;
70 }
71
72 template<class T>
73 inline Vector<T> operator -(const Vector<T> &v) {
74         return Vector<T>(-v.X(), -v.Y());
75 }
76
77 template<class T>
78 inline Vector<T> operator *(const Vector<T> &v, T s) {
79         return Vector<T>(v.X() * s, v.Y() * s);
80 }
81 template<class T>
82 inline Vector<T> operator *(T s, const Vector<T> &v) {
83         return Vector<T>(s * v.X(), s * v.Y());
84 }
85
86 template<class T>
87 inline Vector<T> operator /(const Vector<T> &v, T s) {
88         return Vector<T>(v.X() / s, v.Y() / s);
89 }
90
91 template<class T>
92 inline std::ostream &operator <<(std::ostream &out, const Vector<T> &v) {
93         out << '<' << v.X() << ", " << v.Y() << '>';
94         return out;
95 }
96
97 }
98
99 #endif /* GEOMETRY_VECTOR_H_ */