2021-10-18 04:06:07 +00:00
|
|
|
#ifndef _VECTOR2_H
|
|
|
|
#define _VECTOR2_H
|
|
|
|
|
2022-11-25 14:51:40 +00:00
|
|
|
#include "types.h"
|
|
|
|
#include "sqrt.h"
|
2022-12-13 14:42:42 +00:00
|
|
|
#include "JSystem/JGeometry.h"
|
2023-12-23 01:51:48 +00:00
|
|
|
#include "math.h"
|
2022-11-25 14:51:40 +00:00
|
|
|
|
2022-10-26 00:12:41 +00:00
|
|
|
template <typename T>
|
|
|
|
struct Vector2 {
|
2022-09-18 09:31:04 +00:00
|
|
|
Vector2() { }
|
|
|
|
Vector2(T _x, T _y)
|
|
|
|
{
|
|
|
|
x = _x;
|
|
|
|
y = _y;
|
|
|
|
}
|
2022-08-26 03:40:39 +00:00
|
|
|
|
2022-12-13 08:10:29 +00:00
|
|
|
Vector2(T val)
|
|
|
|
{
|
|
|
|
x = val;
|
|
|
|
y = val;
|
|
|
|
}
|
|
|
|
|
2024-01-13 02:29:52 +00:00
|
|
|
inline void add(const f32& x, const f32& y)
|
|
|
|
{
|
|
|
|
this->x += x;
|
|
|
|
this->y += y;
|
|
|
|
}
|
|
|
|
|
2024-01-22 14:52:46 +00:00
|
|
|
inline void set(f32 x, f32 y)
|
|
|
|
{
|
|
|
|
this->x = x;
|
|
|
|
this->y = y;
|
|
|
|
}
|
|
|
|
|
2024-01-13 03:17:55 +00:00
|
|
|
inline f32 angleBetween(Vector2& other)
|
|
|
|
{
|
|
|
|
f32 angle = JMath::atanTable_.atan2_((this->y - other.y), -(this->x - other.x));
|
|
|
|
return angle;
|
|
|
|
}
|
|
|
|
|
2023-03-06 02:28:34 +00:00
|
|
|
Vector2& operator+=(const Vector2& other)
|
|
|
|
{
|
|
|
|
x += other.x;
|
|
|
|
y += other.y;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector2& operator-=(const Vector2& other)
|
|
|
|
{
|
|
|
|
x -= other.x;
|
|
|
|
y -= other.y;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2023-09-19 07:04:09 +00:00
|
|
|
inline void operator*=(const f32 other)
|
|
|
|
{
|
|
|
|
this->x *= other;
|
|
|
|
this->y *= other;
|
|
|
|
}
|
|
|
|
|
2023-09-25 21:42:05 +00:00
|
|
|
inline void operator/=(const f32 other)
|
|
|
|
{
|
|
|
|
this->x /= other;
|
|
|
|
this->y /= other;
|
|
|
|
}
|
|
|
|
|
2022-12-13 14:42:42 +00:00
|
|
|
operator JGeometry::TVec2f() { return JGeometry::TVec2f(x, y); }
|
|
|
|
|
2023-09-19 15:18:10 +00:00
|
|
|
inline f32 sqrMagnitude() const { return x * x + y * y; }
|
|
|
|
|
2023-02-06 22:25:01 +00:00
|
|
|
inline f32 length() const;
|
|
|
|
inline f32 normalise();
|
2023-09-19 15:18:10 +00:00
|
|
|
inline f32 distance(Vector2&);
|
2023-10-30 02:55:27 +00:00
|
|
|
inline f32 distance(const JGeometry::TVec2f&);
|
2023-02-06 22:25:01 +00:00
|
|
|
|
2021-10-18 04:06:07 +00:00
|
|
|
T x, y;
|
|
|
|
};
|
|
|
|
|
2023-12-22 02:30:38 +00:00
|
|
|
// Use instead of Vector2<f32>
|
|
|
|
typedef Vector2<f32> Vector2f;
|
2021-11-04 02:15:55 +00:00
|
|
|
typedef Vector2<int> Vector2i;
|
2021-10-18 04:06:07 +00:00
|
|
|
|
2022-12-29 23:04:33 +00:00
|
|
|
inline Vector2f operator+(const Vector2f& a, f32 b) { return Vector2f(a.x + b, a.y + b); }
|
|
|
|
|
|
|
|
inline Vector2f operator*(const Vector2f& a, f32 b) { return Vector2f(a.x * b, a.y * b); }
|
|
|
|
|
2022-11-11 14:37:07 +00:00
|
|
|
inline Vector2f operator+(const Vector2f& a, const Vector2f& b) { return Vector2f(a.x + b.x, a.y + b.y); }
|
2023-02-09 05:07:45 +00:00
|
|
|
|
2022-12-13 08:10:29 +00:00
|
|
|
inline Vector2f operator-(const Vector2f& a, const Vector2f& b) { return Vector2f(a.x - b.x, a.y - b.y); }
|
|
|
|
|
2022-11-25 14:51:40 +00:00
|
|
|
inline f32 _lenVec2D(Vector2f& vec)
|
|
|
|
{
|
2022-12-05 23:43:39 +00:00
|
|
|
Vector2f a = vec;
|
|
|
|
a.y *= a.y;
|
|
|
|
f32 length = a.y + vec.x * vec.x;
|
2022-12-07 20:44:30 +00:00
|
|
|
return _sqrtf(a.y + vec.x * vec.x);
|
2022-11-25 14:51:40 +00:00
|
|
|
}
|
|
|
|
|
2024-01-13 03:17:55 +00:00
|
|
|
// should be
|
|
|
|
// inline f32 _lenVec2D(Vector2f& vec) { return _sqrtf(SQUARE(vec.x) + SQUARE(vec.y)); }
|
|
|
|
|
2023-02-06 22:25:01 +00:00
|
|
|
template <>
|
|
|
|
inline f32 Vector2f::length() const
|
|
|
|
{
|
2023-09-19 15:18:10 +00:00
|
|
|
if (sqrMagnitude() > 0.0f) {
|
|
|
|
Vector2f vec = Vector2f(x, y);
|
|
|
|
f32 sqrLen = SQUARE(vec.x) + SQUARE(y);
|
2023-09-19 15:31:12 +00:00
|
|
|
return sqrtf2(sqrLen);
|
2023-02-06 22:25:01 +00:00
|
|
|
} else {
|
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline f32 Vector2f::normalise()
|
|
|
|
{
|
|
|
|
f32 len = length();
|
|
|
|
|
|
|
|
if (len != 0.0f) {
|
2023-09-19 15:31:12 +00:00
|
|
|
*this *= 1.0f / len;
|
2023-02-06 22:25:01 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
|
2023-09-19 15:18:10 +00:00
|
|
|
template <>
|
|
|
|
inline f32 Vector2f::distance(Vector2f& them)
|
|
|
|
{
|
|
|
|
f32 diffX = this->x - them.x;
|
|
|
|
f32 diffY = this->y - them.y;
|
|
|
|
|
|
|
|
return Vector2f(diffX, diffY).length();
|
|
|
|
}
|
|
|
|
|
2023-10-30 02:55:27 +00:00
|
|
|
template <>
|
|
|
|
inline f32 Vector2f::distance(const JGeometry::TVec2f& them)
|
|
|
|
{
|
|
|
|
f32 diffX = this->x - them.x;
|
|
|
|
f32 diffY = this->y - them.y;
|
|
|
|
|
|
|
|
return Vector2f(diffX, diffY).length();
|
|
|
|
}
|
|
|
|
|
2021-10-18 04:06:07 +00:00
|
|
|
#endif
|