edge.cc

Go to the documentation of this file.
00001 /*                            Package   : libhex
00002  * edge.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 <cassert>
00027 
00028 namespace hex {
00029 
00030 
00031 Edge*
00032 Edge::complement(void) const
00033 {
00034   Hex* adjacent_hex =_hex->go(_direction);
00035   if(adjacent_hex)
00036       return adjacent_hex->edge(_direction+3);
00037   else
00038       return NULL;
00039 }
00040 
00041 
00042 bool
00043 Edge::is_next(const Edge& v) const
00044 {
00045   // TRUE iff: &v == next_in(T) || next_in(F) || next_out(T) || next_out(F)
00046   if( _hex == v._hex )
00047       return( _direction+1 == v._direction || _direction-1 == v._direction );
00048   else
00049       return( &v == next_out(true) || &v == next_out(false) );
00050 }
00051 
00052 
00053 Edge*
00054 Edge::next_in(bool clockwise) const
00055 {
00056   int one =(clockwise? -1: 1);
00057   return _hex->edge( _direction + one );
00058 }
00059 
00060 
00061 Edge*
00062 Edge::next_out(bool clockwise) const
00063 {
00064   int one =(clockwise? -1: 1);
00065   Edge* c =_hex->edge( _direction + one )->complement();
00066   if(c)
00067       return c->_hex->edge( _direction - one );
00068   else
00069       return NULL;
00070 }
00071 
00072 
00074 Point corner_offset(Point p, Direction d, float bias)
00075 {
00076   Distance dx =0.0;
00077   Distance dy =0.0;
00078   switch(d)
00079   {
00080     case A: dx =  I/2.0; dy = -K/2.0; break;
00081     case B: dx =  I/2.0; dy =  K/2.0; break;
00082     case C: dx =    0.0; dy =  K    ; break;
00083     case D: dx = -I/2.0; dy =  K/2.0; break;
00084     case E: dx = -I/2.0; dy = -K/2.0; break;
00085     case F: dx =    0.0; dy = -K    ; break;
00086   }
00087   if(bias==0.0)
00088       return p.offset(dx,dy);
00089   else
00090       return p.offset(dx*(1.0+bias),dy*(1.0+bias));
00091 }
00092 
00093 
00094 Point
00095 Edge::start_point(float bias, bool clockwise) const
00096 {
00097   if(clockwise)
00098       return corner_offset( _hex->centre(), _direction + 1, bias );
00099   else
00100       return corner_offset( _hex->centre(), _direction    , bias );
00101 }
00102 
00103 
00104 Point
00105 Edge::end_point(float bias, bool clockwise) const
00106 {
00107   return start_point(bias,!clockwise);
00108 }
00109 
00110 
00111 Point
00112 Edge::join_point(const Edge* next, float bias) const
00113 {
00114   if(bias==0.0 || this->_hex==next->_hex)
00115   {
00116     if(this->_direction+1 == next->_direction)
00117         return end_point(bias);
00118     else
00119         return start_point(bias); // clockwise
00120   }
00121   else if(next == next_out())
00122   {
00123     Point p =corner_offset( _hex->centre(), _direction + 2, 0.0 );
00124     return corner_offset( p, _direction, bias );
00125   }
00126   else // clockwise
00127   {
00128 #ifdef HEX_PARANOID_CHECKS
00129     assert(next == next_out(true));
00130 #endif
00131     Point p =corner_offset( _hex->centre(), _direction - 1, 0.0 );
00132     return corner_offset( p, _direction + 1, bias );
00133   }
00134 }
00135 
00136 
00137 } // end namespace hex

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