hex.cc

Go to the documentation of this file.
00001 /*                            Package   : libhex
00002  * hex.cc                     Created   : 2007/10/11
00003  *                            Author    : Alex Tingle
00004  *
00005  *    Copyright (C) 2007-2008, Alex Tingle.
00006  *
00007  *    This file is part of the libhex application.
00008  *
00009  *    libhex is free software; you can redistribute it and/or
00010  *    modify it under the terms of the GNU Lesser General Public
00011  *    License as published by the Free Software Foundation; either
00012  *    version 2.1 of the License, or (at your option) any later version.
00013  *
00014  *    libhex is distributed in the hope that it will be useful,
00015  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  *    Lesser General Public License for more details.
00018  *
00019  *    You should have received a copy of the GNU Lesser General Public
00020  *    License along with this library; if not, write to the Free Software
00021  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022  */
00023 
00024 #include "hex.h"
00025 
00026 #include <sstream>
00027 
00028 namespace hex {
00029 
00030 
00031 Edge*
00032 Hex::edge(const Direction& d)
00033 {
00034   switch(d)
00035   {
00036     case A:  return &_edgeA;
00037     case B:  return &_edgeB;
00038     case C:  return &_edgeC;
00039     case D:  return &_edgeD;
00040     case E:  return &_edgeE;
00041     default: return &_edgeF;
00042   }
00043 }
00044 
00045 
00046 const Edge*
00047 Hex::edge(const Direction& d) const
00048 {
00049   return const_cast<Edge*>( const_cast<Hex*>(this)->edge( d ) );
00050 }
00051 
00052 
00053 Hex*
00054 Hex::go(const Direction& d, int distance) const
00055 {
00056   int i_ =this->i;
00057   int j_ =this->j;
00058   hex::go(i_,j_,d,distance); // in/out: i_,j_ 
00059   try
00060   {
00061     return _grid.hex(i_,j_);
00062   }
00063   catch(hex::out_of_range)
00064   {
00065     return NULL;
00066   }
00067 }
00068 
00069 
00070 Hex*
00071 Hex::go(const std::string& steps) const
00072 {
00073   int i_ =this->i;
00074   int j_ =this->j;
00075   hex::go(i_,j_,steps); // in/out: i_,j_ 
00076   try
00077   {
00078     return _grid.hex(i_,j_);
00079   }
00080   catch(hex::out_of_range)
00081   {
00082     return NULL;
00083   }
00084 }
00085 
00086 
00087 Point
00088 Hex::centre(void) const
00089 {
00090   Point result;
00091   if(this->j % 2)
00092       result.x = I * (1 + this->i); // odd rows
00093   else 
00094       result.x = I/2.0 + I * this->i; // even rows
00095   result.y = K + J * this->j;
00096   return result;
00097 }
00098 
00099 
00100 std::string
00101 Hex::str(void) const
00102 {
00103   std::ostringstream ss;
00104   ss<< this->i <<"_"<< this->j;
00105   return ss.str();
00106 }
00107 
00108 
00109 Hex::Hex(const Grid& grid, int i_, int j_):
00110   _edgeA(this,A), _edgeB(this,B), _edgeC(this,C),
00111   _edgeD(this,D), _edgeE(this,E), _edgeF(this,F),
00112   _grid(grid), i(i_), j(j_)
00113 {}
00114 
00115 
00116 Hex::Hex(const Grid& grid, const Hex& h):
00117   _edgeA(this,A), _edgeB(this,B), _edgeC(this,C),
00118   _edgeD(this,D), _edgeE(this,E), _edgeF(this,F),
00119  _grid(grid), i(h.i), j(h.j)
00120 {}
00121 
00122 
00123 } // end namespace hex

Generated on Thu Feb 21 00:00:54 2008 for libhex by  doxygen 1.5.1