13 template<class Scalar>
17 constexpr Vector() : x(0), y(0) { }
18 constexpr Vector(Scalar x, Scalar y) : x(x), y(y) { }
21 constexpr Vector(Vector<Other> other) : x(other.x), y(other.y) { }
23 static Vector<Scalar> FromPolar(Scalar rad, Scalar az) {
24 return Vector<Scalar>(rad * std::cos(az), rad * std::sin(az));
27 static constexpr Vector<Scalar> unit45 = Vector<Scalar>(-0.7071, 0.7071);
30 Vector<Scalar> &operator +=(Vector<Scalar> other) {
35 Vector<Scalar> &operator -=(Vector<Scalar> other) {
40 Vector<Scalar> &operator *=(Scalar factor) {
45 Vector<Scalar> &operator /=(Scalar factor) {
51 SDL_Point ToPoint() const {
65 constexpr Vector<T> Vector<T>::unit45;
67 /// specialization with same layout as SDL_Point
73 constexpr Vector() : SDL_Point({0, 0}) { }
74 constexpr Vector(int x, int y) : SDL_Point({x, y}) { }
77 constexpr Vector(Vector<Other> other)
78 : SDL_Point({int(other.x), int(other.y)}) { }
81 Vector<int> &operator +=(Vector<int> other) {
86 Vector<int> &operator -=(Vector<int> other) {
92 SDL_Point ToPoint() const {
99 template<class Scalar>
100 constexpr Vector<Scalar> operator -(Vector<Scalar> v) {
101 return Vector<Scalar>(-v.x, -v.y);
105 template<class Scalar>
106 constexpr Vector<Scalar> operator +(Vector<Scalar> lhs, Vector<Scalar> rhs) {
107 return Vector<Scalar>(lhs.x + rhs.x, lhs.y + rhs.y);
110 template<class Scalar>
111 constexpr Vector<Scalar> operator -(Vector<Scalar> lhs, Vector<Scalar> rhs) {
112 return Vector<Scalar>(lhs.x - rhs.x, lhs.y - rhs.y);
116 template<class Scalar>
117 constexpr Vector<Scalar> operator *(Vector<Scalar> lhs, Scalar rhs) {
118 return Vector<Scalar>(lhs.x * rhs, lhs.y * rhs);
121 template<class Scalar>
122 constexpr Vector<Scalar> operator *(Scalar lhs, Vector<Scalar> rhs) {
125 template<class Scalar>
126 constexpr Vector<Scalar> operator *(Vector<Scalar> lhs, Vector<Scalar> rhs) {
127 return Vector<Scalar>(lhs.x * rhs.x, lhs.y * rhs.y);
131 template<class Scalar>
132 constexpr Vector<Scalar> operator /(Vector<Scalar> lhs, Scalar rhs) {
133 return Vector<Scalar>(lhs.x / rhs, lhs.y / rhs);
136 template<class Scalar>
137 constexpr Vector<Scalar> operator /(Scalar lhs, Vector<Scalar> rhs) {
140 template<class Scalar>
141 constexpr Vector<Scalar> operator /(Vector<Scalar> lhs, Vector<Scalar> rhs) {
142 return Vector<Scalar>(lhs.x / rhs.x, lhs.y / rhs.y);
146 template<class Scalar>
147 constexpr bool operator ==(Vector<Scalar> lhs, Vector<Scalar> rhs) {
148 return lhs.x == rhs.x && lhs.y == rhs.y;
150 template<class Scalar>
151 constexpr bool operator !=(Vector<Scalar> lhs, Vector<Scalar> rhs) {
152 return lhs.x != rhs.x && lhs.y != rhs.y;
156 template<class Scalar>
157 constexpr bool IsZero(Vector<Scalar> v) {
158 return std::abs(v.x) < std::numeric_limits<Scalar>::epsilon()
159 && std::abs(v.y) < std::numeric_limits<Scalar>::epsilon();
162 template<class Scalar>
163 constexpr Vector<Scalar> abs(Vector<Scalar> v) {
164 return Vector<Scalar>(std::abs(v.x), std::abs(v.y));
166 template<class Scalar>
167 constexpr Vector<Scalar> min(Vector<Scalar> lhs, Vector<Scalar> rhs) {
168 return Vector<Scalar>(std::min(lhs.x, rhs.x), std::min(lhs.y, rhs.y));
170 template<class Scalar>
171 constexpr Vector<Scalar> max(Vector<Scalar> lhs, Vector<Scalar> rhs) {
172 return Vector<Scalar>(std::max(lhs.x, rhs.x), std::max(lhs.y, rhs.y));
176 template<class Scalar>
177 constexpr Scalar Cross2D(Vector<Scalar> lhs, Vector<Scalar> rhs) {
178 return (lhs.x * rhs.y) - (lhs.y * rhs.x);
180 template<class Scalar>
181 constexpr Scalar Dot(Vector<Scalar> lhs, Vector<Scalar> rhs) {
182 return (lhs.x * rhs.x) + (lhs.y * rhs.y);
184 template<class Scalar>
185 constexpr Scalar Length(Vector<Scalar> v) {
186 return std::sqrt(Dot(v, v));
188 template<class Scalar>
189 constexpr Vector<Scalar> Norm(Vector<Scalar> v) {
190 return v / Length(v);
193 template<class Scalar>
194 constexpr Vector<Scalar> Rotate90(Vector<Scalar> v) {
195 return Vector<Scalar>(-v.y, v.x);
197 template<class Scalar>
198 constexpr Vector<Scalar> Rotate180(Vector<Scalar> v) {
201 template<class Scalar>
202 constexpr Vector<Scalar> Rotate270(Vector<Scalar> v) {
203 return Vector<Scalar>(v.y, -v.x);
205 template<class Scalar, class Float>
206 inline Vector<Scalar> Rotate(Vector<Scalar> v, Float by) {
207 Float sine(std::sin(by));
208 Float cosine(std::cos(by));
209 return Vector<Scalar>(v.x * cosine - v.y * sine, v.x * sine + v.y * cosine);
212 /// reflect v along normalized n
213 template<class Scalar>
214 inline Vector<Scalar> Reflect(Vector<Scalar> v, Vector<Scalar> n) {
215 const Scalar dd = Scalar(2) * Dot(v, n);
216 return Vector<Scalar>(v.x - (dd * n.x), v.y - (dd * n.y));
218 /// project v onto normalized n
219 template<class Scalar>
220 inline Vector<Scalar> Project(Vector<Scalar> v, Vector<Scalar> n) {
221 return Dot(v, n) * n;
225 template<class Scalar>
226 inline std::ostream &operator <<(std::ostream &out, Vector<Scalar> v) {
227 return out << '<' << v.x << ',' << v.y << '>';
235 template<class Scalar>
236 constexpr orbi::Vector<Scalar> min(
237 orbi::Vector<Scalar> lhs,
238 orbi::Vector<Scalar> rhs
240 return orbi::Vector<Scalar>(
246 template<class Scalar>
247 constexpr orbi::Vector<Scalar> max(
248 orbi::Vector<Scalar> lhs,
249 orbi::Vector<Scalar> rhs
251 return orbi::Vector<Scalar>(