1 #ifndef SPACE_VECTOR_H_
2 #define SPACE_VECTOR_H_
11 template<class Scalar>
15 constexpr Vector() : x(0), y(0) { }
16 constexpr Vector(Scalar x, Scalar y) : x(x), y(y) { }
19 constexpr Vector(Vector<Other> other) : x(other.x), y(other.y) { }
22 Vector<Scalar> &operator +=(Vector<Scalar> other) {
27 Vector<Scalar> &operator -=(Vector<Scalar> other) {
33 SDL_Point ToPoint() const {
46 /// specialization with same layout as SDL_Point
52 constexpr Vector() : SDL_Point({0, 0}) { }
53 constexpr Vector(int x, int y) : SDL_Point({x, y}) { }
56 constexpr Vector(Vector<Other> other)
57 : SDL_Point({int(other.x), int(other.y)}) { }
60 Vector<int> &operator +=(Vector<int> other) {
65 Vector<int> &operator -=(Vector<int> other) {
71 SDL_Point ToPoint() const {
78 template<class Scalar>
79 constexpr Vector<Scalar> operator -(Vector<Scalar> v) {
80 return Vector<Scalar>(-v.x, -v.y);
84 template<class Scalar>
85 constexpr Vector<Scalar> operator +(Vector<Scalar> lhs, Vector<Scalar> rhs) {
86 return Vector<Scalar>(lhs.x + rhs.x, lhs.y + rhs.y);
89 template<class Scalar>
90 constexpr Vector<Scalar> operator -(Vector<Scalar> lhs, Vector<Scalar> rhs) {
91 return Vector<Scalar>(lhs.x - rhs.x, lhs.y - rhs.y);
95 template<class Scalar>
96 constexpr Vector<Scalar> operator *(Vector<Scalar> lhs, Scalar rhs) {
97 return Vector<Scalar>(lhs.x * rhs, lhs.y * rhs);
100 template<class Scalar>
101 constexpr Vector<Scalar> operator *(Scalar lhs, Vector<Scalar> rhs) {
104 template<class Scalar>
105 constexpr Vector<Scalar> operator *(Vector<Scalar> lhs, Vector<Scalar> rhs) {
106 return Vector<Scalar>(lhs.x * rhs.x, lhs.y * rhs.y);
110 template<class Scalar>
111 constexpr Vector<Scalar> operator /(Vector<Scalar> lhs, Scalar rhs) {
112 return Vector<Scalar>(lhs.x / rhs, lhs.y / rhs);
115 template<class Scalar>
116 constexpr Vector<Scalar> operator /(Scalar lhs, Vector<Scalar> rhs) {
119 template<class Scalar>
120 constexpr Vector<Scalar> operator /(Vector<Scalar> lhs, Vector<Scalar> rhs) {
121 return Vector<Scalar>(lhs.x / rhs.x, lhs.y / rhs.y);
125 template<class Scalar>
126 constexpr bool operator ==(Vector<Scalar> lhs, Vector<Scalar> rhs) {
127 return lhs.x == rhs.x && lhs.y == rhs.y;
129 template<class Scalar>
130 constexpr bool operator !=(Vector<Scalar> lhs, Vector<Scalar> rhs) {
131 return lhs.x != rhs.x && lhs.y != rhs.y;
135 template<class Scalar>
136 inline std::ostream &operator <<(std::ostream &out, Vector<Scalar> v) {
137 return out << '<' << v.x << ',' << v.y << '>';
145 template<class Scalar>
146 constexpr space::Vector<Scalar> min(
147 space::Vector<Scalar> lhs,
148 space::Vector<Scalar> rhs
150 return space::Vector<Scalar>(
156 template<class Scalar>
157 constexpr space::Vector<Scalar> max(
158 space::Vector<Scalar> lhs,
159 space::Vector<Scalar> rhs
161 return space::Vector<Scalar>(