void BeeHive::MakeCross (int radius, const Hexagon& center, vector<Hexagon>& cross, ccColor4F color) { cross.push_back (center); for (int i = -radius; i <= radius; ++i) { if (i == 0)continue; int x = i; int z = -i; int y = -x - z; cross.push_back (Hexagon (x, y, color) += center); cross.push_back (Hexagon (y, z, color) += center); cross.push_back (Hexagon (x, z, color) += center); } }
int main() { char line[100]; int points[6], row[6], col[6]; int i, n, isvalid; while (gets(line), strlen(line)) { n = sscanf(line, "%d %d %d %d %d %d", points, points+1, points+2, points+3, points+4, points+5); for (i=0; i<n; i++) printf("%d ", points[i]); qsort(points, n, sizeof(int), Cmp); for (i=0; i<n; i++) { RowCol(points[i], row+i, col+i); } isvalid = (n==3) ? Triangle(row, col) : (n==4) ? Parallelogram(row, col) : (n==6) ? Hexagon(row, col) : 0; printf("are %sthe vertices of %s\n", isvalid ? "" : "not ", !isvalid ? "an acceptable figure" : n==3 ? "a triangle" : n==4 ? "a parallelogram" : "a hexagon"); } return 0; }
void BeeHive::MakeSolidHex (int radius, const Hexagon& center, vector<Hexagon>& hexagons, ccColor4F color) { for (int q = radius; q >= -radius; --q) { for (int r = -radius; r <= radius; ++r) { if (abs (r) <= radius && abs (q) <= radius && abs (-q - r) <= radius) hexagons.push_back (Hexagon (q, r, color) += center); } } }
// on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if (!CCLayer::init()) { return false; } CCSize s = CCDirector::sharedDirector()->getWinSize(); field.setZeroPoint (s.width / 6, s.height / 4); line.CopyCoordinate (field); intersection.CopyCoordinate (field); intersection.setMask (&field.hexagones); HexCoordinate h; BeeHive::MakeRingHex (1, HexZero, h.hexagones, ccc4f (0, 1, 1, 1)); //test direction // field.MakeLine(Hexagon(-15, 10), Hexagon(8, -15), ccc4f(1, 0, 0, 1)); // field.MakeCross(3, Hexagon(10, 10), ccc4f(1, 1, 0, 1)); // field.MakeSolidHex(3, Hexagon(-10, 10), ccc4f(1, 0, 1, 1)); // field.MakeRingHex(3, Hexagon(-10, -10), ccc4f(0, 1, 1, 1)); // field.MakeRingHexes(3, 4, Hexagon(10, -10), ccc4f(0, 0, 1, 1)); field.MakeRect2(9, 7, Hexagon(0, 0), ccc4f(1, 0, 0.5, 1)); //behind // field.MakeRect2 (11, 7, Hexagon (5, -10), ccc4f (1, 0, 0.5, 1)); field.hexagones.push_back (Hexagon (20, 10, ccc4f (1, 1, 0.5, 1))); field.hexagones.push_back(field.hexagones.rbegin()->Mirror(HexZero)); HexCoordinate for_intersect; for_intersect.MakeRect (11, 7, Hexagon (5, -10), ccc4f (1, 0, 0.5, 1)); field.InterSect (for_intersect.hexagones); field.hexagones.push_back (Hexagon (15, 15, ccc4f (0.5, 1, 0, 1))); setTouchMode (kCCTouchesOneByOne); return true; }
// Get the hexagon at position x, y. If it doesn't exist, create it. // Never returns NULL. Hexagon *HexGrid::getHexagon(int x, int y) { // Stick a hexagon into the map if necessary. HexMap::value_type hexpair(Hexagon::key(x, y), Hexagon(x, y)); std::pair<HexMap::iterator,bool> retval; retval = m_hexes.insert(hexpair); HexMap::iterator it = retval.first; Hexagon *hex_p = &(it->second); // Return a pointer to the located hexagon. return hex_p; }
void BeeHive::MakeRingHex (int radius, const Hexagon& center, vector<Hexagon>& ring, ccColor4F color) { for (int q = -radius; q <= radius; ++q) { for (int r = -radius; r <= radius; ++r) { if (max (max (abs (q), abs (r)), abs (-q - r)) == radius) { ring.push_back (Hexagon (q, r, color) += center); } } } }
// first point (origin) and start of column 0 // | // | // | ------- column 0 // | | // | | end of column 0 - start of column 1 // | | | // | v | _____ // v____ v |\ | // / \ | \ | Here's an expansion of what // / 0,0 \____ | \ | I'm calling a mini-column, // \ / \ | \ | showing two half rows. // \____/ 1,0 \ |____\| // / \ / | /| The top rectangle is the // / 0,1 \____/ | / | negative slope case and // \ / \ | / | the lower rectangle is the // \____/ \ | / | positive slope case. // |/____| // ** <-- The area above these // asterisks are the "mini-column" // The mini-column is 1/3 the total width of the column. // // We are creating a tesselated plane of hexagons where one side of each // hexagon is parallel with the X axis. We think of the columns of // hexagons as all having the same X value. Hexagons lower than their // neighbor have successive Y values. // // The hexagon in the second column but just below the hexagon in the first // column as the same Y value as the hexagon above and to the left. The third // column's Y values are the one less than the hexagon below and to the left // as the second column. // // The first point, whatever it's X/Y location, is made the origin, and is // placed at the top-left edge of hexagon 0,0. // Hexagon *HexGrid::findHexagon(Point p) { int x, y; if (m_hexes.empty()) { m_origin = p; // Make a hex at the origin and insert it. Return a pointer // to the hexagon in the map. HexMap::value_type hexpair(Hexagon::key(0, 0), Hexagon(0, 0)); HexMap::iterator it = m_hexes.insert(hexpair).first; return &it->second; } // Offset by the origin. p -= m_origin; double col = p.m_x / m_width; // First calculate X and Y as if we had a bunch of offset rectangles. // This works for 2/3 of the width of the hexagons. x = (int)floor(col); if (x % 2 == 0) y = static_cast<int>(floor(p.m_y / m_height)); else y = static_cast<int>(floor((p.m_y - (m_height / 2)) / m_height)); // Compute the column remainder to determine if we are in a strip where // the hexagons overlap (the mini-column). double xcolOffset = col - floor(col); if (xcolOffset > 2.0/3.0) { // Calculate the xvalue as a fraction of the width of the column-piece // containing multiple hex columns. These overlap columns are 1/3 // the total width of any column. // Subtract the 2/3 of the value not relevant to the mini-column. xcolOffset -= 2.0/3.0; // Scale the value to the width of the mini-column. xcolOffset *= 3.0; // Each halfrow contains a single sloping edge of a hexagon. // The slope of the edge is either sqrt(3) or -sqrt(3). The edge // extends from top left to lower right or from bottom left to top // right. What we do here is compute the horizontal fraction of // the box (xcolOffset) and the vertical fraction of the box // (yrowOffset) and then compare them. double halfrow = p.m_y / (m_height / 2); int halfy = (int)halfrow; double yrowOffset = halfrow - floor(halfrow); // Negative slope case. if ((halfy % 2 == 0 && x % 2 == 0) || (x % 2 && halfy % 2)) { if (xcolOffset > yrowOffset) { if (x % 2 == 0) y--; x++; } } // Positive slope case. else { if (yrowOffset > xcolOffset) { if (x % 2) y++; x++; } } } return getHexagon(x, y); }