-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath.hpp
More file actions
30 lines (23 loc) · 929 Bytes
/
math.hpp
File metadata and controls
30 lines (23 loc) · 929 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#pragma once
#include <cmath>
#include <numbers>
template <typename T>
struct vec2
{
T x{};
T y{};
};
using vec2i = vec2<int>;
using vec2f = vec2<float>;
constexpr vec2f operator+(const vec2f& v0, const vec2f& v1) { return {.x = v0.x + v1.x, .y = v0.y + v1.y}; }
constexpr vec2f operator-(const vec2f& v0, const vec2f& v1) { return {.x = v0.x - v1.x, .y = v0.y - v1.y}; }
constexpr vec2f operator*(const vec2f& v, const float x) { return {.x = v.x * x, .y = v.y * x}; }
constexpr vec2f rotate(const vec2f& v, const float radians)
{
const auto c = std::cos(radians);
const auto s = std::sin(radians);
return {.x = v.x * c - v.y * s, .y = v.x * s + v.y * c};
}
constexpr vec2i to_vec2i(const vec2f& v) { return {.x = static_cast<int>(v.x), .y = static_cast<int>(v.y)}; }
constexpr auto pi = std::numbers::pi_v<float>;
constexpr auto to_radians(const vec2f& dir) { return pi + std::atan2(dir.y, dir.x); }