00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
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);
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
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 }