8 #include <steeriously/Utilities.hpp>
28 Vector2(
double a,
double b):x(a),y(b){}
42 bool isZero()
const {
return (x*x + y*y) < MinDouble;}
49 inline double Length()
const;
87 inline void Truncate(
double max);
90 inline double Distance(
const Vector2 &v2)
const;
93 inline double DistanceSq(
const Vector2 &v2)
const;
95 inline void Reflect(
const Vector2& norm);
98 inline Vector2 GetReverse()
const;
118 const Vector2& operator*=(
const double& rhs)
126 const Vector2& operator/=(
const double& rhs)
134 bool operator==(
const Vector2& rhs)
const
139 bool operator!=(
const Vector2& rhs)
const
141 return (x != rhs.x) || (y != rhs.y);
147 inline Vector2 operator*(
const Vector2 &lhs,
double rhs);
148 inline Vector2 operator*(
double lhs,
const Vector2 &rhs);
149 inline Vector2 operator-(
const Vector2 &lhs,
const Vector2 &rhs);
150 inline Vector2 operator+(
const Vector2 &lhs,
const Vector2 &rhs);
151 inline Vector2 operator/(
const Vector2 &lhs,
double val);
161 return sqrt(x * x + y * y);
171 return (x * x + y * y);
181 return x*v2.x + y*v2.y;
189 enum {clockwise = 1, anticlockwise = -1};
195 return anticlockwise;
216 inline double Vector2::Distance(
const Vector2 &v2)
const
218 double ySeparation = v2.y - y;
219 double xSeparation = v2.x - x;
221 return sqrt(ySeparation*ySeparation + xSeparation*xSeparation);
229 inline double Vector2::DistanceSq(
const Vector2 &v2)
const
231 double ySeparation = v2.y - y;
232 double xSeparation = v2.x - x;
234 return ySeparation*ySeparation + xSeparation*xSeparation;
241 inline void Vector2::Truncate(
double max)
256 inline void Vector2::Reflect(
const Vector2& norm)
258 *
this += 2.0 * this->
Dot(norm) * norm.GetReverse();
265 inline Vector2 Vector2::GetReverse()
const
267 return Vector2(-this->x, -this->y);
277 double vector_length = this->
Length();
279 if (vector_length > std::numeric_limits<double>::epsilon())
281 this->x /= vector_length;
282 this->y /= vector_length;
293 double vector_length = vec.
Length();
295 if (vector_length > std::numeric_limits<double>::epsilon())
297 vec.x /= vector_length;
298 vec.y /= vector_length;
305 inline double Vec2DDistance(
const Vector2 &v1,
const Vector2 &v2)
308 double ySeparation = v2.y - v1.y;
309 double xSeparation = v2.x - v1.x;
311 return sqrt(ySeparation*ySeparation + xSeparation*xSeparation);
314 inline double Vec2DDistanceSq(
const Vector2 &v1,
const Vector2 &v2)
317 double ySeparation = v2.y - v1.y;
318 double xSeparation = v2.x - v1.x;
320 return ySeparation*ySeparation + xSeparation*xSeparation;
323 inline double Vec2DLength(
const Vector2& v)
325 return sqrt(v.x*v.x + v.y*v.y);
328 inline double Vec2DLengthSq(
const Vector2& v)
330 return (v.x*v.x + v.y*v.y);
335 inline Vector2 operator*(
const Vector2 &lhs,
double rhs)
342 inline Vector2 operator*(
double lhs,
const Vector2 &rhs)
350 inline Vector2 operator-(
const Vector2 &lhs,
const Vector2 &rhs)
360 inline Vector2 operator+(
const Vector2 &lhs,
const Vector2 &rhs)
370 inline Vector2 operator/(
const Vector2 &lhs,
double val)
385 if (pos.x > MaxX) {pos.x = 0.0;}
387 if (pos.x < 0) {pos.x = (double)MaxX;}
389 if (pos.y < 0) {pos.y = (double)MaxY;}
391 if (pos.y > MaxY) {pos.y = 0.0;}
396 inline bool NotInsideRegion(Vector2 p,
400 return (p.x < top_left.x) || (p.x > bot_rgt.x) ||
401 (p.y < top_left.y) || (p.y > bot_rgt.y);
404 inline bool InsideRegion(Vector2 p,
408 return !((p.x < top_left.x) || (p.x > bot_rgt.x) ||
409 (p.y < top_left.y) || (p.y > bot_rgt.y));
412 inline bool InsideRegion(Vector2 p,
int left,
int top,
int right,
int bottom)
414 return !( (p.x < left) || (p.x > right) || (p.y < top) || (p.y > bottom) );
422 inline bool isSecondInFOVOfFirst(Vector2 posFirst,
427 Vector2 toTarget = Vec2DNormalize(posSecond - posFirst);
429 return facingFirst.Dot(toTarget) >= cos(fov/2.0);