-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhex.cc
More file actions
123 lines (102 loc) · 2.53 KB
/
hex.cc
File metadata and controls
123 lines (102 loc) · 2.53 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
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
/* Package : libhex
* hex.cc Created : 2007/10/11
* Author : Alex Tingle
*
* Copyright (C) 2007-2008, Alex Tingle.
*
* This file is part of the libhex application.
*
* libhex is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* libhex is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "hex.h"
#include <sstream>
namespace hex {
Edge*
Hex::edge(const Direction& d)
{
switch(d)
{
case A: return &_edgeA;
case B: return &_edgeB;
case C: return &_edgeC;
case D: return &_edgeD;
case E: return &_edgeE;
default: return &_edgeF;
}
}
const Edge*
Hex::edge(const Direction& d) const
{
return const_cast<Hex*>(this)->edge( d );
}
Hex*
Hex::go(const Direction& d, int distance) const
{
int i_ =this->i;
int j_ =this->j;
hex::go(i_,j_,d,distance); // in/out: i_,j_
try
{
return _grid.hex(i_,j_);
}
catch(hex::out_of_range)
{
return NULL;
}
}
Hex*
Hex::go(const std::string& steps) const
{
int i_ =this->i;
int j_ =this->j;
hex::go(i_,j_,steps); // in/out: i_,j_
try
{
return _grid.hex(i_,j_);
}
catch(hex::out_of_range)
{
return NULL;
}
}
Point
Hex::centre(void) const
{
Point result;
if(this->j % 2)
result.x = I * (1 + this->i); // odd rows
else
result.x = I/2.0 + I * this->i; // even rows
result.y = K + J * this->j;
return result;
}
std::string
Hex::str(void) const
{
std::ostringstream ss;
ss<< this->i <<"_"<< this->j;
return ss.str();
}
Hex::Hex(const Grid& grid, int i_, int j_):
_edgeA(this,A), _edgeB(this,B), _edgeC(this,C),
_edgeD(this,D), _edgeE(this,E), _edgeF(this,F),
_grid(grid), i(i_), j(j_)
{}
Hex::Hex(const Grid& grid, const Hex& h):
_edgeA(this,A), _edgeB(this,B), _edgeC(this,C),
_edgeD(this,D), _edgeE(this,E), _edgeF(this,F),
_grid(grid), i(h.i), j(h.j)
{}
} // end namespace hex