-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtility.cpp
192 lines (174 loc) · 3.74 KB
/
Utility.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include "Utility.h"
#include <iostream>
#include <cmath>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
using namespace std;
Utility::Utility()
{
// Empty constructor
}
pair<int, int> Utility::arrayToPolar(pair<int, int> coord, int boardSize)
{
// Compute centre of the board
pair<int, int> centre = make_pair(boardSize / 2, boardSize / 2);
// Shift the Origin to the centre of the board
int X = coord.first - centre.first;
int absX = abs(X);
int Y = coord.second - centre.second;
int absY = abs(Y);
// cout << X << " " << Y << " " << absX << " " << absY << endl;
if (X == 0 && Y == 0)
{
// At the centre itself
return make_pair(0, 0);
}
int hex, pos; // the polar coordinates
if (X * Y < 0)
{
// Opposite sign
hex = absX + absY;
}
else
{
// Either 0 or same sign
hex = max(absX, absY);
}
// For pos find the hexrand in which the point is
if (X == 0)
{
if (Y > 0)
{
pos = 0;
}
else
{
pos = 3 * hex;
}
}
else if (X * Y <= 0)
{
// 3rd or 6th
if (X > 0)
{
// 3rd
pos = 2 * hex + absY;
}
else
{
// 6th
pos = 5 * hex + absY;
}
}
else if (X == Y)
{
// On the third axis
if (X > 0)
{
pos = hex;
}
else
{
pos = 4 * hex;
}
}
else if (absY > absX)
{
// 1st or 4th
if (X > 0)
{
// 1st
pos = min(absX, absY);
}
else
{
// 4th
pos = 3 * hex + min(absX, absY);
}
}
else
{
// 2nd or 5th
if (X > 0)
{
// 2nd
pos = hex + max(absX, absY) - min(absX, absY);
}
else
{
// 5th
pos = 4 * hex + max(absX, absY) - min(absX, absY);
}
}
return make_pair(hex, pos);
}
pair<int, int> Utility::polarToArray(pair<int, int> coord, int boardSize)
{
int hex = coord.first, pos = coord.second;
int X, Y;
if (hex == 0)
{
// centre point
return make_pair(boardSize / 2, boardSize / 2);
}
// Determining which hexrand
int quad = pos / hex + 1;
// cout << "quad: " << quad << endl;
switch (quad)
{
case 1:
X = pos;
Y = hex;
break;
case 2:
X = hex;
Y = 2 * hex - pos;
break;
case 3:
X = 3 * hex - pos;
Y = 2 * hex - pos;
break;
case 4:
X = 3 * hex - pos;
Y = -hex;
break;
case 5:
X = -hex;
Y = pos - 5 * hex;
break;
case 6:
X = pos - 6 * hex;
Y = pos - 5 * hex;
break;
}
// Shift the origin back
X += boardSize / 2;
Y += boardSize / 2;
return make_pair(X, Y);
}
vector<string> Utility::splitString(string sentence)
{
// int i = 0, pos = 0, length = 0, temp;
// vector<string> result;
// cout << message << "$" << delimiter << "$";
// temp = message.find ( delimiter.c_str ( ), pos );
// while ( temp != -1 )
// {
// length = temp - pos;
// result[i] = message.substr ( pos, length );
// pos = temp + delimiter.size ( );
// temp = message.find ( delimiter.c_str ( ), pos );
// i++;
// }
// result[i] = message.substr ( pos );
// i++;
// return result;
istringstream iss(sentence);
vector<string> tokens;
copy(istream_iterator<string>(iss),
istream_iterator<string>(),
back_inserter(tokens));
return tokens;
}