154 lines
2.6 KiB
C++
154 lines
2.6 KiB
C++
#include "dxd_math.hpp"
|
|
|
|
#include <SDL3/SDL.h>
|
|
|
|
vec2 operator+(vec2 const &v)
|
|
{
|
|
return { .x = v.x, .y = v.y };
|
|
}
|
|
|
|
vec2 operator-(vec2 const &v)
|
|
{
|
|
return { .x = -v.x, .y = -v.y };
|
|
}
|
|
|
|
vec2 operator+(vec2 const &a, vec2 const &b)
|
|
{
|
|
return { .x = a.x + b.x, .y = a.y + b.y };
|
|
}
|
|
|
|
vec2 operator-(vec2 const &a, vec2 const &b)
|
|
{
|
|
return { .x = a.x - b.x, .y = a.y - b.y };
|
|
}
|
|
|
|
vec2 operator*(vec2 const &a, float const &b)
|
|
{
|
|
return { .x = a.x * b, .y = a.y * b };
|
|
}
|
|
|
|
vec2 operator/(vec2 const &a, float const &b)
|
|
{
|
|
return { .x = a.x / b, .y = a.y / b };
|
|
}
|
|
|
|
bool operator<(vec2 const &a, vec2 const &b)
|
|
{
|
|
return a.x < b.x && a.y < b.y;
|
|
}
|
|
|
|
bool operator>(vec2 const &a, vec2 const &b)
|
|
{
|
|
return a.x > b.x && a.y > b.y;
|
|
}
|
|
|
|
bool operator<(vec2i const &a, vec2i const &b)
|
|
{
|
|
return a.x < b.x && a.y < b.y;
|
|
}
|
|
|
|
bool operator>(vec2i const &a, vec2i const &b)
|
|
{
|
|
return a.x > b.x && a.y > b.y;
|
|
}
|
|
|
|
float dot(vec2 const &a, vec2 const &b)
|
|
{
|
|
return a.x * b.x + a.y * b.y;
|
|
}
|
|
|
|
float norm2(vec2 const &v)
|
|
{
|
|
return v.x * v.x + v.y * v.y;
|
|
}
|
|
|
|
float norm(vec2 const &v)
|
|
{
|
|
return std::sqrtf(v.x * v.x + v.y * v.y);
|
|
}
|
|
|
|
vec2 normalize(vec2 const &v)
|
|
{
|
|
return v / std::sqrt(v.x * v.x + v.y * v.y);
|
|
}
|
|
|
|
vec2 min2(vec2 const &a, vec2 const &b)
|
|
{
|
|
return {
|
|
.x = a.x < b.x ? a.x : b.x,
|
|
.y = a.y < b.y ? a.y : b.y,
|
|
};
|
|
}
|
|
|
|
vec2 max2(vec2 const &a, vec2 const &b)
|
|
{
|
|
return {
|
|
.x = a.x > b.x ? a.x : b.x,
|
|
.y = a.y > b.y ? a.y : b.y,
|
|
};
|
|
}
|
|
|
|
vec2 polar_to_vec2(float const angle, float const len)
|
|
{
|
|
return {
|
|
.x = len * sinf(angle),
|
|
.y = len * cosf(angle)
|
|
};
|
|
}
|
|
|
|
float vec2_angle(vec2 const v)
|
|
{
|
|
return atan2f(v.x, v.y);
|
|
}
|
|
|
|
vec2 v2i_to_v2(vec2i const v)
|
|
{
|
|
return { (float)v.x, (float)v.y };
|
|
}
|
|
|
|
float normalize_angle(const float angle)
|
|
{
|
|
float a = angle;
|
|
while (a > 2*M_PIf) a -= 2*M_PIf;
|
|
while (a < 0) a += 2*M_PIf;
|
|
return a;
|
|
}
|
|
|
|
float normalize_angle_diff(const float diff)
|
|
{
|
|
float d = diff;
|
|
while (d > M_PIf) d -= 2*M_PIf;
|
|
while (d < -M_PIf) d += 2*M_PIf;
|
|
return d;
|
|
}
|
|
|
|
float angle_to_heading(float const angle)
|
|
{
|
|
return normalize_angle(angle) * 180.0f / M_PIf;
|
|
}
|
|
|
|
int rand_int(int const min, int const max)
|
|
{
|
|
return SDL_rand(max - min) + min;
|
|
}
|
|
|
|
float rand_float(float const min, float const max)
|
|
{
|
|
return SDL_randf() * (max - min) + min;
|
|
}
|
|
|
|
float rand_angle()
|
|
{
|
|
return rand_float(0, 2*M_PIf);
|
|
}
|
|
|
|
vec2i rand_v2i(vec2i const min, vec2i const max)
|
|
{
|
|
return { rand_int(min.x, max.x), rand_int(min.y, max.y) };
|
|
}
|
|
|
|
vec2 rand_v2(vec2 const min, vec2 const max)
|
|
{
|
|
return { rand_float(min.x, max.x), rand_float(min.y, max.y) };
|
|
}
|