-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.cpp
150 lines (143 loc) · 3.33 KB
/
helper.cpp
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "helper.h"
#include <cassert>
#include <cmath>
#include <sstream>
#include <vector>
double calc_angle_degrees(const double dx, const double dy)
{
return calc_angle_radians(dx, dy) * 360.0 / (2.0 * M_PI);
}
double calc_angle_radians(const double dx, const double dy)
{
return std::atan2(dx,dy) - (0.5 * M_PI);
}
double calc_distance(const double dx, const double dy) noexcept
{
return std::sqrt((dx * dx) + (dy * dy));
}
std::vector<int> make_sequence(
const int from,
const int to,
const int increment
)
{
assert(increment >= 1);
assert((from - to) % increment == 0);
std::vector<int> v;
v.reserve((std::abs(to - from) / increment) + 1);
if (to > from)
{
for (int i{from}; i != to; i+=increment)
{
v.push_back(i);
}
}
else
{
for (int i{from}; i != to; i-=increment)
{
v.push_back(i);
}
}
v.push_back(to);
return v;
}
std::vector<std::string> split_str(
const std::string& s,
const char seperator
)
{
std::istringstream is(s);
std::vector<std::string> v;
for (
std::string sub;
std::getline(is, sub, seperator);
v.push_back(sub))
{
//Empty for loop
}
return v;
}
void test_helper()
{
#ifndef NDEBUG
// calc_angle_degreees
{
const double tolerance{0.1};
{
// 3 o'clock
const double expected{0.0};
const double created{calc_angle_degrees( 1.0, 0.0)};
assert(is_close(expected, created, tolerance));
}
{
// 6 o'clock
const double expected{-90.0};
const double created{calc_angle_degrees( 0.0, 1.0)};
assert(is_close(expected, created, tolerance));
}
{
// 9 o'clock
const double expected{-180.0};
const double created{calc_angle_degrees(-1.0, 0.0)};
assert(is_close(expected, created, tolerance));
}
{
// 12 o'clock
const double expected{90.0};
const double created{calc_angle_degrees( 0.0, -1.0)};
assert(is_close(expected, created, tolerance));
}
}
// calc_angle_radians
{
const double tolerance{0.1};
{
// 3 o'clock
const double expected{0.0 * M_PI};
const double created{calc_angle_radians( 1.0, 0.0)};
assert(is_close(expected, created, tolerance));
}
{
// 6 o'clock
const double expected{(1.5 * M_PI) - (2.0 * M_PI)};
const double created{calc_angle_radians( 0.0, 1.0)};
assert(is_close(expected, created, tolerance));
}
{
// 9 o'clock
const double expected{(1.0 * M_PI) - (2.0 * M_PI)};
const double created{calc_angle_radians(-1.0, 0.0)};
assert(is_close(expected, created, tolerance));
}
{
// 12 o'clock
const double expected{(0.5 * M_PI) + (0.0 * M_PI)};
const double created{calc_angle_radians( 0.0, -1.0)};
assert(is_close(expected, created, tolerance));
}
}
{
std::vector<int> v = {1, 2, 3};
remove_first(v);
assert(v == std::vector<int>( {2, 3} ) );
remove_first(v);
assert(v == std::vector<int>( {3} ) );
}
// make_sequence, no intermediates
{
const auto v{make_sequence(42, 43)};
assert(v.size() == 2);
assert(v[0] == 42);
assert(v[1] == 43);
}
// make_sequence, many intermediates
{
const auto v{make_sequence(314, 271)};
assert(v.size() > 2);
assert(v[0] == 314);
assert(v[1] == 313);
assert(v.back() == 271);
}
#endif
}