2019-05-15 10:52:37 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
class CVector2D
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
float x, y;
|
|
|
|
CVector2D(void) {}
|
|
|
|
CVector2D(float x, float y) : x(x), y(y) {}
|
|
|
|
CVector2D(const CVector &v) : x(v.x), y(v.y) {}
|
2019-07-25 11:06:24 -04:00
|
|
|
float Heading(void) const { return Atan2(-x, y); }
|
2019-07-10 11:18:26 -04:00
|
|
|
float Magnitude(void) const { return Sqrt(x*x + y*y); }
|
2019-05-15 10:52:37 -04:00
|
|
|
float MagnitudeSqr(void) const { return x*x + y*y; }
|
|
|
|
|
|
|
|
void Normalise(void){
|
|
|
|
float sq = MagnitudeSqr();
|
2020-04-15 01:03:53 -04:00
|
|
|
//if(sq > 0.0f){
|
2019-07-28 07:14:08 -04:00
|
|
|
float invsqrt = RecipSqrt(sq);
|
2019-05-15 10:52:37 -04:00
|
|
|
x *= invsqrt;
|
|
|
|
y *= invsqrt;
|
2020-04-15 01:03:53 -04:00
|
|
|
//}else
|
|
|
|
// x = 1.0f;
|
2019-05-15 10:52:37 -04:00
|
|
|
}
|
2020-04-15 01:03:53 -04:00
|
|
|
|
2019-08-03 18:31:00 -04:00
|
|
|
const CVector2D &operator+=(CVector2D const &right) {
|
|
|
|
x += right.x;
|
|
|
|
y += right.y;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CVector2D &operator-=(CVector2D const &right) {
|
|
|
|
x -= right.x;
|
|
|
|
y -= right.y;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CVector2D &operator*=(float right) {
|
|
|
|
x *= right;
|
|
|
|
y *= right;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CVector2D &operator/=(float right) {
|
|
|
|
x /= right;
|
|
|
|
y /= right;
|
|
|
|
return *this;
|
|
|
|
}
|
2019-05-15 10:52:37 -04:00
|
|
|
CVector2D operator-(const CVector2D &rhs) const {
|
|
|
|
return CVector2D(x-rhs.x, y-rhs.y);
|
|
|
|
}
|
|
|
|
CVector2D operator+(const CVector2D &rhs) const {
|
|
|
|
return CVector2D(x+rhs.x, y+rhs.y);
|
|
|
|
}
|
2019-08-09 13:42:18 -04:00
|
|
|
CVector2D operator/(float t) const {
|
|
|
|
return CVector2D(x/t, y/t);
|
|
|
|
}
|
2019-05-15 10:52:37 -04:00
|
|
|
};
|
|
|
|
|
2019-06-19 08:06:13 -04:00
|
|
|
inline float
|
|
|
|
DotProduct2D(const CVector2D &v1, const CVector2D &v2)
|
|
|
|
{
|
|
|
|
return v1.x*v2.x + v1.y*v2.y;
|
|
|
|
}
|
|
|
|
|
2019-05-15 10:52:37 -04:00
|
|
|
inline float
|
|
|
|
CrossProduct2D(const CVector2D &v1, const CVector2D &v2)
|
|
|
|
{
|
|
|
|
return v1.x*v2.y - v1.y*v2.x;
|
|
|
|
}
|
2019-08-09 13:42:18 -04:00
|
|
|
|
|
|
|
inline float
|
|
|
|
Distance2D(const CVector2D &v, float x, float y)
|
|
|
|
{
|
|
|
|
return Sqrt((v.x-x)*(v.x-x) + (v.y-y)*(v.y-y));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline float
|
|
|
|
DistanceSqr2D(const CVector2D &v, float x, float y)
|
|
|
|
{
|
|
|
|
return (v.x-x)*(v.x-x) + (v.y-y)*(v.y-y);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
NormalizeXY(float &x, float &y)
|
|
|
|
{
|
|
|
|
float l = Sqrt(x*x + y*y);
|
|
|
|
if(l != 0.0f){
|
|
|
|
x /= l;
|
|
|
|
y /= l;
|
|
|
|
}else
|
|
|
|
x = 1.0f;
|
|
|
|
}
|
2019-11-04 18:04:26 -05:00
|
|
|
|
|
|
|
inline CVector2D operator*(const CVector2D &left, float right)
|
|
|
|
{
|
|
|
|
return CVector2D(left.x * right, left.y * right);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline CVector2D operator*(float left, const CVector2D &right)
|
|
|
|
{
|
|
|
|
return CVector2D(left * right.x, left * right.y);
|
|
|
|
}
|