26#include <tqvaluelist.h>
27#include <tqvaluevector.h>
39 typedef TQPair<int, int> Coord;
45 typedef TQValueList<Coord> CoordList;
49operator +(
const KGrid2D::Coord &c1,
const KGrid2D::Coord &c2) {
50 return KGrid2D::Coord(c1.first + c2.first, c1.second + c2.second);
54operator -(
const KGrid2D::Coord &c1,
const KGrid2D::Coord &c2) {
55 return KGrid2D::Coord(c1.first - c2.first, c1.second - c2.second);
63maximum(
const KGrid2D::Coord &c1,
const KGrid2D::Coord &c2) {
64 return KGrid2D::Coord(kMax(c1.first, c2.first), kMax(c1.second, c2.second));
71minimum(
const KGrid2D::Coord &c1,
const KGrid2D::Coord &c2) {
72 return KGrid2D::Coord(kMin(c1.first, c2.first), kMin(c1.second, c2.second));
75inline TQTextStream &operator <<(TQTextStream &s,
const KGrid2D::Coord &c) {
76 return s <<
'(' << c.second <<
", " << c.first <<
')';
79inline TQTextStream &operator <<(TQTextStream &s,
const KGrid2D::CoordList &list)
81 for(KGrid2D::CoordList::const_iterator i=list.begin(); i!=list.end(); ++i)
121 for (uint i=0; i<_vector.count(); i++) _vector[i] = value;
127 uint
width()
const {
return _width; }
135 uint
size()
const {
return _width*_height; }
141 return c.first + c.second*_width;
154 const Type &
at(
const Coord &c)
const {
return _vector[
index(c)]; }
158 Type &
at(
const Coord &c) {
return _vector[
index(c)]; }
189 return ( c.first>=0 && c.first<(
int)_width
190 && c.second>=0 && c.second<(
int)_height );
197 c.first = kMax(kMin(c.first, (
int)_width-1), 0);
198 c.second = kMax(kMin(c.second, (
int)_height-1), 0);
202 uint _width, _height;
203 TQValueVector<Type> _vector;
209 s << (TQ_UINT32)m.
width() << (TQ_UINT32)m.
height();
210 for (uint i=0; i<m.
size(); i++) s << m[i];
219 for (uint i=0; i<m.
size(); i++) s >> m[i];
240 enum Neighbour { Left=0, Right, Up, Down, LeftUp, LeftDown,
241 RightUp, RightDown, Nb_Neighbour };
248 case Left:
return M_PI;
249 case Right:
return 0;
250 case Up:
return M_PI_2;
251 case Down:
return -M_PI_2;
252 case LeftUp:
return 3.0*M_PI_4;
253 case LeftDown:
return -3.0*M_PI_4;
254 case RightUp:
return M_PI_4;
255 case RightDown:
return -M_PI_4;
256 case Nb_Neighbour: Q_ASSERT(
false);
266 case Left:
return Right;
267 case Right:
return Left;
268 case Up:
return Down;
269 case Down:
return Up;
270 case LeftUp:
return RightDown;
271 case LeftDown:
return RightUp;
272 case RightUp:
return LeftDown;
273 case RightDown:
return LeftUp;
274 case Nb_Neighbour: Q_ASSERT(
false);
290 case Left:
return c + Coord(-1, 0);
291 case Right:
return c + Coord( 1, 0);
292 case Up:
return c + Coord( 0, -1);
293 case Down:
return c + Coord( 0, 1);
294 case LeftUp:
return c + Coord(-1, -1);
295 case LeftDown:
return c + Coord(-1, 1);
296 case RightUp:
return c + Coord( 1, -1);
297 case RightDown:
return c + Coord( 1, 1);
298 case Nb_Neighbour: Q_ASSERT(
false);
328 bool directOnly =
false)
const {
330 for (uint i=0; i<(directOnly ? LeftUp : Nb_Neighbour); i++) {
346 case Left:
return Coord(0, c.second);
348 case Up:
return Coord(c.first, 0);
350 case LeftUp:
return Coord(0, 0);
354 case Nb_Neighbour: Q_ASSERT(
false);
379 RightUp, RightDown, Nb_Neighbour };
386 case Left:
return M_PI;
387 case Right:
return 0;
388 case LeftUp:
return 2.0*M_PI/3;
389 case LeftDown:
return -2.0*M_PI/3;
390 case RightUp:
return M_PI/3;
391 case RightDown:
return -M_PI/3;
392 case Nb_Neighbour: Q_ASSERT(
false);
402 case Left:
return Right;
403 case Right:
return Left;
404 case LeftUp:
return RightDown;
405 case LeftDown:
return RightUp;
406 case RightUp:
return LeftDown;
407 case RightDown:
return LeftUp;
408 case Nb_Neighbour: Q_ASSERT(
false);
417 bool oddRow = c.second%2;
419 case Left:
return c + Coord(-1, 0);
420 case Right:
return c + Coord( 1, 0);
421 case LeftUp:
return c + (oddRow ? Coord( 0, -1) : Coord(-1, -1));
422 case LeftDown:
return c + (oddRow ? Coord( 0, 1) : Coord(-1, 1));
423 case RightUp:
return c + (oddRow ? Coord( 1, -1) : Coord( 0, -1));
424 case RightDown:
return c + (oddRow ? Coord( 1, 1) : Coord( 0, 1));
425 case Nb_Neighbour: Q_ASSERT(
false);
433 static uint
distance(
const Coord &c1,
const Coord &c2) {
434 return kAbs(c1.first - c2.first) + kAbs(c1.second - c2.second)
435 + (c1.first==c2.first || c1.second==c2.second ? 0 : -1);
466 CoordList
neighbours(
const Coord &c,
bool insideOnly =
true)
const {
468 for (uint i=0; i<Nb_Neighbour; i++) {
486 bool insideOnly =
true)
const {
496 CoordList::const_iterator it;
497 for (it=ring.begin(); it!=ring.end(); ++it) {
499 CoordList::const_iterator it2;
500 for (it2=n.begin(); it2!=n.end(); ++it2)
501 if ( center.find(*it2)==center.end()
502 && ring.find(*it2)==ring.end()
503 && newRing.find(*it2)==newRing.end() )
504 newRing.append(*it2);
509 if ( !all )
return ring;
510 CoordList::const_iterator it;
511 for (it=ring.begin(); it!=ring.end(); ++it)
This template class represents a generic bidimensionnal grid.
Generic(uint width=0, uint height=0)
Constructor.
bool inside(const Coord &c) const
void fill(const Type &value)
Fill the nodes with the given value.
uint index(const Coord &c) const
const Type & operator[](const Coord &c) const
void resize(uint width, uint height)
Resize the grid.
Coord coord(uint index) const
void bound(Coord &c) const
Bound the given coordinate with the grid dimensions.
const Type & at(uint index) const
Type & at(const Coord &c)
const Type & at(const Coord &c) const
This class contains static methods to manipulate coordinates on an hexagonal grid where hexagons form...
static Neighbour opposed(Neighbour n)
Neighbour
Identify the six neighbours.
static double angle(Neighbour n)
static uint distance(const Coord &c1, const Coord &c2)
static Coord neighbour(const Coord &c, Neighbour n)
This template implements a hexagonal grid where hexagons form horizontal lines:
CoordList neighbours(const Coord &c, uint distance, bool all, bool insideOnly=true) const
CoordList neighbours(const Coord &c, bool insideOnly=true) const
Hexagonal(uint width=0, uint height=0)
Constructor.
This class contains static methods to manipulate coordinates for a square bidimensionnal grid.
static bool isDirect(Neighbour n)
static Neighbour opposed(Neighbour n)
Neighbour
Identify the eight neighbours.
static Coord neighbour(const Coord &c, Neighbour n)
static double angle(Neighbour n)
This template is a Generic implementation for a square bidimensionnal grid (SquareBase).
Square(uint width=0, uint height=0)
Constructor.
CoordList neighbours(const Coord &c, bool insideOnly=true, bool directOnly=false) const
Coord toEdge(const Coord &c, Neighbour n) const