pikmin2/include/Vector2.h

146 lines
2.6 KiB
C
Raw Normal View History

#ifndef _VECTOR2_H
#define _VECTOR2_H
#include "types.h"
#include "sqrt.h"
2022-12-13 14:42:42 +00:00
#include "JSystem/JGeometry.h"
#include "math.h"
2022-10-26 00:12:41 +00:00
template <typename T>
struct Vector2 {
Vector2() { }
Vector2(T _x, T _y)
{
x = _x;
y = _y;
}
Vector2(T val)
{
x = val;
y = val;
}
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;
}
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; }
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&);
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;
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); }
inline Vector2f operator-(const Vector2f& a, const Vector2f& b) { return Vector2f(a.x - b.x, a.y - b.y); }
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);
}
2024-01-13 03:17:55 +00:00
// should be
// inline f32 _lenVec2D(Vector2f& vec) { return _sqrtf(SQUARE(vec.x) + SQUARE(vec.y)); }
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);
return sqrtf2(sqrLen);
} else {
return 0.0f;
}
}
template <>
inline f32 Vector2f::normalise()
{
f32 len = length();
if (len != 0.0f) {
*this *= 1.0f / len;
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();
}
#endif