1 /******************************************************************************
2 * Copyright (c) 2014, Hobu Inc. (howard@hobu.co)
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following
8 * conditions are met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided
15 * with the distribution.
16 * * Neither the name of the Howard Butler or Hobu, Inc.
17 * the names of its contributors may be
18 * used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 * OF SUCH DAMAGE.
33 ****************************************************************************/
34
35
36 #include "Segment.hpp"
37 #include "HexGrid.hpp"
38
39 namespace hexer
40 {
41
42 // The segment is normalized if necessary.
possibleRoot(HexGrid * grid)43 bool Segment::possibleRoot(HexGrid *grid)
44 {
45 if (m_side == 3)
46 {
47 m_side = 0;
48 m_hex = grid->getHexagon(m_hex->x(), m_hex->y() + 1);
49 }
50 return m_hex->possibleRoot() && (m_side == 0);
51 }
52
53 // When we're traversing a hexagon counter-clockwise, determine
54 // the next segment we'll traverse assume we're taking a right turn.
rightAntiClockwise(HexGrid * grid)55 Segment Segment::rightAntiClockwise(HexGrid *grid)
56 {
57 Segment next;
58 static int nextside[] = { 5, 0, 1, 2, 3, 4 };
59 static int neighborside[] = { 1, 2, 3, 4, 5, 0 };
60
61 Coord coord = m_hex->neighborCoord(neighborside[m_side]);
62 next.m_side = nextside[m_side];
63 next.m_hex = grid->getHexagon(coord.m_x, coord.m_y);
64 return next;
65 }
66
67 // When we're traversing a hexagon counter-clockwise, determine the
68 // next segment we'll traverse assuming we're taking a right turn.
leftAntiClockwise(HexGrid * grid)69 Segment Segment::leftAntiClockwise(HexGrid *grid)
70 {
71 static int nextside[] = { 1, 2, 3, 4, 5, 0 };
72
73 (void)grid; //unused
74 Segment next(*this);
75 next.m_side = nextside[next.m_side];
76 return next;
77 }
78
79 // When we're travsersing a hexagon clockwise, determine the
80 // next segment we'll traverse assume we're taking a right turn.
rightClockwise(HexGrid * grid)81 Segment Segment::rightClockwise(HexGrid *grid)
82 {
83 static int nextside[] = { 5, 0, 1, 2, 3, 4 };
84
85 (void)grid; //unused
86 Segment next(*this);
87 next.m_side = nextside[next.m_side];
88 return next;
89 }
90
91 // When we're traversing a hexagon clockwise, determine the next segment
92 // we'll traverse assume we're taking a left turn.
leftClockwise(HexGrid * grid)93 Segment Segment::leftClockwise(HexGrid *grid)
94 {
95 Segment next;
96 static int nextside[] = { 1, 2, 3, 4, 5, 0 };
97 static int neighborside[] = { 5, 0, 1, 2, 3, 4 };
98
99 Coord coord = m_hex->neighborCoord(neighborside[m_side]);
100 next.m_side = nextside[m_side];
101 next.m_hex = grid->getHexagon(coord.m_x, coord.m_y);
102 return next;
103 }
104
105 // Change the edge so that it has side 0-2 if necessary.
normalize(HexGrid * grid)106 void Segment::normalize(HexGrid *grid)
107 {
108 if (m_side >= 3)
109 {
110 Coord coord = m_hex->neighborCoord(m_side);
111 m_side -= 3;
112 m_hex = grid->getHexagon(coord.m_x, coord.m_y);
113 }
114 }
115
startPos(HexGrid * grid) const116 Point Segment::startPos(HexGrid *grid) const
117 {
118 int side = m_side - 1;
119 side = side < 0 ? 5 : side;
120 return pos(grid, grid->offset(side));
121 }
122
endPos(HexGrid * grid) const123 Point Segment::endPos(HexGrid *grid) const
124 {
125 return pos(grid, grid->offset(m_side));
126 }
127
pos(HexGrid * grid,const Point & offset) const128 Point Segment::pos(HexGrid *grid, const Point& offset) const
129 {
130 Point pos;
131 pos.m_x = m_hex->x() * grid->width();
132 pos.m_y = m_hex->y() * grid->height();
133 if (m_hex->xodd())
134 {
135 pos.m_y += (grid->height() / 2);
136 }
137 return pos + offset + grid->origin();
138 }
139
operator ==(const Segment & s1,const Segment & s2)140 bool operator == (const Segment& s1, const Segment &s2)
141 {
142 static int sharedside[] = { 3, 4, 5, 0, 1, 2 };
143 static int evenx[] = { 0, -1, -1, 0, 1, 1 };
144 static int eveny[] = { -1, -1, 0, 1, 0, -1 };
145 static int oddx[] = { 0, -1, -1, 0, 1, 1, };
146 static int oddy[] = { -1, 0, 1, 1, 1, 0 };
147 if (s1.m_hex == s2.m_hex && s1.m_side == s2.m_side)
148 return true;
149 if (s2.m_side == sharedside[s1.m_side])
150 {
151 int xinc;
152 int yinc;
153 if (s1.m_hex->xeven())
154 {
155 xinc = evenx[s1.m_side];
156 yinc = eveny[s1.m_side];
157 }
158 else
159 {
160 xinc = oddx[s1.m_side];
161 yinc = oddy[s1.m_side];
162 }
163 if ((s2.m_hex->x() == s1.m_hex->x() + xinc) &&
164 (s2.m_hex->y() == s1.m_hex->y() + yinc))
165 {
166 return true;
167 }
168 }
169 return false;
170 }
171
operator !=(const Segment & s1,const Segment & s2)172 bool operator != (const Segment& s1, const Segment &s2)
173 {
174 return !(s1 == s2);
175 }
176
operator <<(std::ostream & os,const Segment & s)177 std::ostream& operator << (std::ostream& os, const Segment &s)
178 {
179 os << s.m_hex->x() << "/" << s.m_hex->y() << " - " << s.m_side;
180 return os;
181 };
182
183 } // namespace hexer
184
185