-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper.h
74 lines (63 loc) · 1.69 KB
/
helper.h
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
#ifndef HELPER_H
#define HELPER_H
#include <algorithm>
#include <cassert>
#include <cmath>
#include <string>
#include <vector>
/// Calculate the angle in degrees
/// dx | dy | o'clock | degrees
/// ---|----|---------|--------
/// 1 | 0 | 3 | 0
/// 0 | 1 | 6 | 270
/// -1 | 0 | 9 | 180
/// 0 | -1 | 12 | 90
double calc_angle_degrees(const double dx, const double dy);
/// Calculate the angle in radians
/// dx | dy | o'clock | radians
/// ---|----|---------|---------
/// 1 | 0 | 3 | 0.0 * pi
/// 0 | 1 | 6 | 1.5 * pi
/// -1 | 0 | 9 | 1.0 * pi
/// 0 | -1 | 12 | 0.5 * pi
double calc_angle_radians(const double dx, const double dy);
/// Calculate the Euclidean distance between two points
double calc_distance(const double dx, const double dy) noexcept;
template <class T> bool is_close(const T& lhs, const T& rhs, const T& max)
{
return std::abs(lhs - rhs) < max;
}
template <class T> bool is_present_in(
const T& element,
const std::vector<T>& collection
)
{
return std::find(
std::begin(collection),
std::end(collection),
element
) != std::end(collection);
}
/// Make a sequence in an incluse way:
/// the first element will be 'from',
/// the last element will be 'to'
/// Assumes 'from' and 'to' are different
std::vector<int> make_sequence(
const int from,
const int to,
const int increment = 1
);
template <class T>
void remove_first(T& v)
{
assert(!v.empty());
v.erase(v.begin());
}
/// Split a string, from https://www.richelbilderbeek.nl/CppSeperateString.htm
std::vector<std::string> split_str(
const std::string& s,
const char seperator = ' '
);
/// Test the help functions
void test_helper();
#endif // HELPER_H