]> git.localhorst.tv Git - space.git/blob - src/math/Vector.h
57159bff2e0e01193e4a8ea16952eace97e9e4b7
[space.git] / src / math / Vector.h
1 #ifndef SPACE_VECTOR_H_
2 #define SPACE_VECTOR_H_
3
4 #include <algorithm>
5
6
7 namespace space {
8
9 template<class Scalar>
10 class Vector {
11
12 public:
13         constexpr Vector() : x(0), y(0) { }
14         constexpr Vector(Scalar x, Scalar y) : x(x), y(y) { }
15
16         template<class Other>
17         constexpr Vector(Vector<Other> other) : x(other.x), y(other.y) { }
18
19 public:
20         Vector<Scalar> &operator +=(Vector<Scalar> other) {
21                 x += other.x;
22                 y += other.y;
23                 return *this;
24         }
25         Vector<Scalar> &operator -=(Vector<Scalar> other) {
26                 x -= other.x;
27                 y -= other.y;
28                 return *this;
29         }
30
31 public:
32         Scalar x;
33         Scalar y;
34
35 };
36
37
38 template<class Scalar>
39 constexpr Vector<Scalar> operator -(Vector<Scalar> v) {
40         return Vector<Scalar>(-v.x, -v.y);
41 }
42
43
44 template<class Scalar>
45 constexpr Vector<Scalar> operator +(Vector<Scalar> lhs, Vector<Scalar> rhs) {
46         return Vector<Scalar>(lhs.x + rhs.x, lhs.y + rhs.y);
47 }
48
49 template<class Scalar>
50 constexpr Vector<Scalar> operator -(Vector<Scalar> lhs, Vector<Scalar> rhs) {
51         return Vector<Scalar>(lhs.x - rhs.x, lhs.y - rhs.y);
52 }
53
54
55 template<class Scalar>
56 constexpr Vector<Scalar> operator *(Vector<Scalar> lhs, Scalar rhs) {
57         return Vector<Scalar>(lhs.x * rhs, lhs.y * rhs);
58 }
59
60 template<class Scalar>
61 constexpr Vector<Scalar> operator *(Scalar lhs, Vector<Scalar> rhs) {
62         return rhs * lhs;
63 }
64 template<class Scalar>
65 constexpr Vector<Scalar> operator *(Vector<Scalar> lhs, Vector<Scalar> rhs) {
66         return Vector<Scalar>(lhs.x * rhs.x, lhs.y * rhs.y);
67 }
68
69 }
70
71
72 namespace std {
73
74 template<class Scalar>
75 constexpr space::Vector<Scalar> min(
76                 space::Vector<Scalar> lhs,
77                 space::Vector<Scalar> rhs
78 ) {
79         return space::Vector<Scalar>(
80                 min(lhs.x, rhs.x),
81                 min(lhs.y, rhs.y)
82         );
83 }
84
85 template<class Scalar>
86 constexpr space::Vector<Scalar> max(
87                 space::Vector<Scalar> lhs,
88                 space::Vector<Scalar> rhs
89 ) {
90         return space::Vector<Scalar>(
91                 max(lhs.x, rhs.x),
92                 max(lhs.y, rhs.y)
93         );
94 }
95
96 }
97
98 #endif