-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvec.hpp
103 lines (82 loc) · 2.51 KB
/
vec.hpp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#pragma once
#include <array>
#include "hash.hpp"
namespace aocutil
{
template<typename T>
struct Vec2
{
T x, y;
Vec2 operator+(const Vec2& v) const
{
return Vec2{.x = x + v.x, .y = y + v.y};
}
Vec2 operator-(const Vec2& v) const
{
return Vec2{.x = x - v.x, .y = y - v.y};
}
bool operator==(const Vec2& v) const = default;
};
enum class Direction {Up, Right, Down, Left};
template <typename T, bool up_is_positive = false>
constexpr std::array<Vec2<T>, 4> all_dirs_vec2()
{
constexpr Vec2<T> dir_right = {.x = 1, .y = 0};
constexpr Vec2<T> dir_left = {.x = -1, .y = 0};
constexpr Vec2<T> dir_up = up_is_positive ? Vec2<T>{.x = 0, .y = 1} : Vec2<T>{.x = 0, .y = -1};
constexpr Vec2<T> dir_down = up_is_positive ? Vec2<T>{.x = 0, .y = -1} : Vec2<T>{.x = 0, .y = 1};
return {dir_right, dir_left, dir_up, dir_down};
}
template <typename T, bool up_is_positive = false>
constexpr Vec2<T> dir_to_vec2(Direction dir)
{
constexpr Vec2<T> dir_right = {.x = 1, .y = 0};
constexpr Vec2<T> dir_left = {.x = -1, .y = 0};
constexpr Vec2<T> dir_up = up_is_positive ? Vec2<T>{.x = 0, .y = 1} : Vec2<T>{.x = 0, .y = -1};
constexpr Vec2<T> dir_down = up_is_positive ? Vec2<T>{.x = 0, .y = -1} : Vec2<T>{.x = 0, .y = 1};
switch (dir)
{
case Direction::Up:
return dir_up;
case Direction::Right:
return dir_right;
case Direction::Down:
return dir_down;
case Direction::Left:
return dir_left;
default:
throw std::invalid_argument("dir_to_vec2: Invalid direction");
break;
}
}
constexpr std::pair<Direction, Direction> dir_get_left_right(Direction dir)
{
switch (dir)
{
case Direction::Right:
return {Direction::Up, Direction::Down};
case Direction::Left:
return {Direction::Down, Direction::Up};
case Direction::Up:
return {Direction::Left, Direction::Right};
case Direction::Down:
return {Direction::Right, Direction::Left};
default:
throw std::invalid_argument("dir_get_left_right: Invalid direction");
}
}
}
template<typename T>
struct std::hash<aocutil::Vec2<T>>
{
std::size_t operator()(const aocutil::Vec2<T>& v) const noexcept
{
std::size_t h = 0;
aocutil::hash_combine(h, v.x, v.y);
return h;
}
};
template<typename T>
inline std::ostream& operator<<(std::ostream&os, const aocutil::Vec2<T>& v) {
return os << "(x: " << v.x << ", y: " << v.y << ")";
}