1 #ifndef SPACE_VECTOR_H_
2 #define SPACE_VECTOR_H_
12 template<class Scalar>
16 constexpr Vector() : x(0), y(0) { }
17 constexpr Vector(Scalar x, Scalar y) : x(x), y(y) { }
20 constexpr Vector(Vector<Other> other) : x(other.x), y(other.y) { }
22 static Vector<Scalar> FromPolar(Scalar rad, Scalar az) {
23 return Vector<Scalar>(rad * std::cos(az), rad * std::sin(az));
26 static constexpr Vector<Scalar> unit45 = Vector<Scalar>(-0.7071, 0.7071);
29 Vector<Scalar> &operator +=(Vector<Scalar> other) {
34 Vector<Scalar> &operator -=(Vector<Scalar> other) {
40 SDL_Point ToPoint() const {
54 constexpr Vector<T> Vector<T>::unit45;
56 /// specialization with same layout as SDL_Point
62 constexpr Vector() : SDL_Point({0, 0}) { }
63 constexpr Vector(int x, int y) : SDL_Point({x, y}) { }
66 constexpr Vector(Vector<Other> other)
67 : SDL_Point({int(other.x), int(other.y)}) { }
70 Vector<int> &operator +=(Vector<int> other) {
75 Vector<int> &operator -=(Vector<int> other) {
81 SDL_Point ToPoint() const {
88 template<class Scalar>
89 constexpr Vector<Scalar> operator -(Vector<Scalar> v) {
90 return Vector<Scalar>(-v.x, -v.y);
94 template<class Scalar>
95 constexpr Vector<Scalar> operator +(Vector<Scalar> lhs, Vector<Scalar> rhs) {
96 return Vector<Scalar>(lhs.x + rhs.x, lhs.y + rhs.y);
99 template<class Scalar>
100 constexpr Vector<Scalar> operator -(Vector<Scalar> lhs, Vector<Scalar> rhs) {
101 return Vector<Scalar>(lhs.x - rhs.x, lhs.y - rhs.y);
105 template<class Scalar>
106 constexpr Vector<Scalar> operator *(Vector<Scalar> lhs, Scalar rhs) {
107 return Vector<Scalar>(lhs.x * rhs, lhs.y * rhs);
110 template<class Scalar>
111 constexpr Vector<Scalar> operator *(Scalar lhs, Vector<Scalar> rhs) {
114 template<class Scalar>
115 constexpr Vector<Scalar> operator *(Vector<Scalar> lhs, Vector<Scalar> rhs) {
116 return Vector<Scalar>(lhs.x * rhs.x, lhs.y * rhs.y);
120 template<class Scalar>
121 constexpr Vector<Scalar> operator /(Vector<Scalar> lhs, Scalar rhs) {
122 return Vector<Scalar>(lhs.x / rhs, lhs.y / rhs);
125 template<class Scalar>
126 constexpr Vector<Scalar> operator /(Scalar lhs, Vector<Scalar> rhs) {
129 template<class Scalar>
130 constexpr Vector<Scalar> operator /(Vector<Scalar> lhs, Vector<Scalar> rhs) {
131 return Vector<Scalar>(lhs.x / rhs.x, lhs.y / rhs.y);
135 template<class Scalar>
136 constexpr bool operator ==(Vector<Scalar> lhs, Vector<Scalar> rhs) {
137 return lhs.x == rhs.x && lhs.y == rhs.y;
139 template<class Scalar>
140 constexpr bool operator !=(Vector<Scalar> lhs, Vector<Scalar> rhs) {
141 return lhs.x != rhs.x && lhs.y != rhs.y;
145 template<class Scalar>
146 constexpr Scalar Cross2D(Vector<Scalar> lhs, Vector<Scalar> rhs) {
147 return (lhs.x * rhs.y) - (lhs.y * rhs.x);
149 template<class Scalar>
150 constexpr Scalar Dot(Vector<Scalar> lhs, Vector<Scalar> rhs) {
151 return (lhs.x * rhs.x) + (lhs.y * rhs.y);
153 template<class Scalar>
154 constexpr Scalar Length(Vector<Scalar> v) {
155 return std::sqrt(Dot(v, v));
158 template<class Scalar>
159 constexpr Vector<Scalar> Rotate90(Vector<Scalar> v) {
160 return Vector<Scalar>(-v.y, v.x);
162 template<class Scalar>
163 constexpr Vector<Scalar> Rotate180(Vector<Scalar> v) {
166 template<class Scalar>
167 constexpr Vector<Scalar> Rotate270(Vector<Scalar> v) {
168 return Vector<Scalar>(v.y, -v.x);
172 template<class Scalar>
173 inline std::ostream &operator <<(std::ostream &out, Vector<Scalar> v) {
174 return out << '<' << v.x << ',' << v.y << '>';
182 template<class Scalar>
183 constexpr space::Vector<Scalar> min(
184 space::Vector<Scalar> lhs,
185 space::Vector<Scalar> rhs
187 return space::Vector<Scalar>(
193 template<class Scalar>
194 constexpr space::Vector<Scalar> max(
195 space::Vector<Scalar> lhs,
196 space::Vector<Scalar> rhs
198 return space::Vector<Scalar>(