int GameBoard::NumNeighbours(const Cell& cell) { BoundingBox searchBox; if (cell.x == 0) { searchBox._x = 0; searchBox._width = 1; } else { searchBox._x = cell.x - 1; searchBox._width = 2; } if (cell.y == 0) { searchBox._y = 0; searchBox._height = 1; } else { searchBox._y = cell.y - 1; searchBox._height = 2; } CellSet neighbours; _quadTree.FindPoints(searchBox, neighbours); // The original cell is in the search box, so should // be at least one. If a dead cell, we only look at // dead cells next to alive ones so there should be at least one. assert(neighbours.size() > 0); return cell.isAlive ? neighbours.size() - 1 : neighbours.size(); }
Cell GameBoard::FindNearest(const Cell& cell) const { if (_liveCells.count(cell)) { return cell; } // Select random cell, find all cells in the minimal bounding // box such that the random cell is on the edge. // Repeat until there is only one cell in the bounding box. CellSet candidateSet = _liveCells; srand(time(0)); while (candidateSet.size() > 1) { int index = rand() % candidateSet.size(); CellSet::iterator it = candidateSet.begin(); for (int i = 0; i < index; ++i) { ++it; assert(it != candidateSet.end()); } Cell nextCandidate = *it; unsigned long boxWidth = (nextCandidate.x > cell.x) ? nextCandidate.x - cell.x : cell.x - nextCandidate.x; unsigned long boxHeight = (nextCandidate.y > cell.y) ? nextCandidate.y - cell.y : cell.y - nextCandidate.y; unsigned long boxX = cell.x >= (0 + boxWidth) ? cell.x - boxWidth : 0; unsigned long boxY = cell.y >= (0 + boxHeight) ? cell.y - boxHeight : 0; candidateSet.clear(); _quadTree.FindPoints(BoundingBox(boxX, boxY, boxWidth * 2, boxHeight * 2), candidateSet); } assert(candidateSet.size() == 1); return *candidateSet.begin(); }
// Insert the new cell just below the lowest boundary cell void ZOrderedCells::insertCell(Cell * cell) { // Get boundary cells CellSet boundary = cell->boundary(); // Insert at appropriate position if(boundary.size() == 0) { // Insert last insertLast(cell); } else { // Insert before boundary Iterator it = begin(); while(it!=end() && !boundary.contains(*it)) ++it; list_.insert(it,cell); } }