26 #include <tqvaluelist.h> 27 #include <tqvaluevector.h> 29 #include <tdeglobal.h> 39 typedef TQPair<int, int> Coord;
45 typedef TQValueList<Coord> CoordList;
49 operator +(
const KGrid2D::Coord &c1,
const KGrid2D::Coord &c2) {
50 return KGrid2D::Coord(c1.first + c2.first, c1.second + c2.second);
54 operator -(
const KGrid2D::Coord &c1,
const KGrid2D::Coord &c2) {
55 return KGrid2D::Coord(c1.first - c2.first, c1.second - c2.second);
63 maximum(
const KGrid2D::Coord &c1,
const KGrid2D::Coord &c2) {
64 return KGrid2D::Coord(kMax(c1.first, c2.first), kMax(c1.second, c2.second));
71 minimum(
const KGrid2D::Coord &c1,
const KGrid2D::Coord &c2) {
72 return KGrid2D::Coord(kMin(c1.first, c2.first), kMin(c1.second, c2.second));
75 inline TQTextStream &operator <<(TQTextStream &s,
const KGrid2D::Coord &c) {
76 return s <<
'(' << c.second <<
", " << c.first <<
')';
79 inline TQTextStream &operator <<(TQTextStream &s,
const KGrid2D::CoordList &list)
81 for(KGrid2D::CoordList::const_iterator i=list.begin(); i!=list.end(); ++i)
114 _vector.resize(width*height);
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;
148 return Coord(index % _width, index / _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;
207 template <
class Type>
208 TQDataStream &operator <<(TQDataStream &s, const KGrid2D::Generic<Type> &m) {
209 s << (TQ_UINT32)m.width() << (TQ_UINT32)m.height();
210 for (uint i=0; i<m.size(); i++) s << m[i];
214 template <
class Type>
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 {
329 CoordList neighbours;
330 for (uint i=0; i<(directOnly ? LeftUp : Nb_Neighbour); i++) {
333 neighbours.append(n);
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);
450 template <
class Type>
466 CoordList
neighbours(
const Coord &c,
bool insideOnly =
true)
const {
467 CoordList neighbours;
468 for (uint i=0; i<Nb_Neighbour; i++) {
471 neighbours.append(n);
485 CoordList
neighbours(
const Coord &c, uint distance,
bool all,
486 bool insideOnly =
true)
const {
489 if ( distance==0 )
return ring;
490 ring = neighbours(c, insideOnly);
491 if ( distance==1 )
return ring;
494 for (uint i=1; i<distance; i++) {
496 CoordList::const_iterator it;
497 for (it=ring.begin(); it!=ring.end(); ++it) {
498 CoordList n = neighbours(*it, insideOnly);
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 is a Generic implementation for a square bidimensionnal grid (SquareBase).
This template class represents a generic bidimensionnal grid.
CoordList neighbours(const Coord &c, bool insideOnly=true) const
CoordList neighbours(const Coord &c, bool insideOnly=true, bool directOnly=false) const
This class contains static methods to manipulate coordinates on an hexagonal grid where hexagons form...
static Coord neighbour(const Coord &c, Neighbour n)
static Neighbour opposed(Neighbour n)
Generic(uint width=0, uint height=0)
Constructor.
const Type & at(uint index) const
CoordList neighbours(const Coord &c, uint distance, bool all, bool insideOnly=true) const
Type & at(const Coord &c)
const Type & at(const Coord &c) const
static bool isDirect(Neighbour n)
Coord toEdge(const Coord &c, Neighbour n) const
uint index(const Coord &c) const
static double angle(Neighbour n)
void fill(const Type &value)
Fill the nodes with the given value.
Coord coord(uint index) const
void resize(uint width, uint height)
Resize the grid.
Hexagonal(uint width=0, uint height=0)
Constructor.
const Type & operator[](const Coord &c) const
This template implements a hexagonal grid where hexagons form horizontal lines:
This class contains static methods to manipulate coordinates for a square bidimensionnal grid...
bool inside(const Coord &c) const
static double angle(Neighbour n)
static Neighbour opposed(Neighbour n)
Square(uint width=0, uint height=0)
Constructor.
void bound(Coord &c) const
Bound the given coordinate with the grid dimensions.
static uint distance(const Coord &c1, const Coord &c2)
Neighbour
Identify the eight neighbours.
Neighbour
Identify the six neighbours.
static Coord neighbour(const Coord &c, Neighbour n)