-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoord.cpp
More file actions
75 lines (59 loc) · 1.42 KB
/
coord.cpp
File metadata and controls
75 lines (59 loc) · 1.42 KB
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
#include "coord.hpp"
#include <stdexcept>
using namespace std;
Coord::Coord(): lin{0}, col{0} {}
Coord::Coord(int a, int b): lin{a}, col{b}
{
if(a >= TAILLEGRILLE or b > TAILLEGRILLE or a < 0 or b < 0)
{
throw invalid_argument("out of grille bound");
}
}
Coord::Coord(int a) {
if(a >= TAILLEGRILLE*TAILLEGRILLE) {
throw invalid_argument("out of grille bound");
}else {
lin = a / TAILLEGRILLE;
col = a % TAILLEGRILLE;
}
}
int Coord::getLin() const
{
return lin;
}
int Coord::getCol() const
{
return col;
}
void Coord::setLin(int i) {
lin = i;
}
void Coord::setCol(int i) {
col = i;
}
int Coord::toInt() const {
return lin * TAILLEGRILLE + col;
}
vector<Coord> Coord::voisins() const {
vector<Coord> helper;
for(int l = lin -1 ; l <= lin +1 ; l++) {
for(int c = col - 1 ; c <= col +1; c++) {
if(c > TAILLEGRILLE-1 or l > TAILLEGRILLE-1 or c <0 or l <0 or (c == col and l == lin)) {
continue;
}else {
helper.push_back(*(new Coord{l, c}));
}
}
}
return helper;
}
ostream &operator<<(ostream &out, Coord c) {
out << "("<<c.getLin() << "," << c.getCol()<< ")"<< " ";
return out;
}
bool operator==(Coord c1, Coord c2) {
return c1.getLin() == c2.getLin() and c1.getCol() == c2.getCol();
}
bool operator!=(Coord c1, Coord c2) {
return not(c2 == c1);
}