]> git.localhorst.tv Git - space.git/blob - src/math/Vector.h
grid view
[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
65 }
66
67
68 namespace std {
69
70 template<class Scalar>
71 constexpr space::Vector<Scalar> min(
72                 space::Vector<Scalar> lhs,
73                 space::Vector<Scalar> rhs
74 ) {
75         return space::Vector<Scalar>(
76                 min(lhs.x, rhs.x),
77                 min(lhs.y, rhs.y)
78         );
79 }
80
81 template<class Scalar>
82 constexpr space::Vector<Scalar> max(
83                 space::Vector<Scalar> lhs,
84                 space::Vector<Scalar> rhs
85 ) {
86         return space::Vector<Scalar>(
87                 max(lhs.x, rhs.x),
88                 max(lhs.y, rhs.y)
89         );
90 }
91
92 }
93
94 #endif