int16_t SolvingSudokuBoard::getSelectionAdditiveMask(vector<iPoint2D> *selection) { int16_t mask = 0; int i; for (i = 0; i < (int)selection->size(); i++) mask = mask | getSquareValue((*selection)[i]); //change (*selection)[n] to selection->at(n)? (would that be cleaner?) return mask; }
std::vector<cocos2d::Vec2> AutoPolygon::marchSquare(const Rect& rect, const Vec2& start, const float& threshold) { int stepx = 0; int stepy = 0; int prevx = 0; int prevy = 0; int startx = start.x; int starty = start.y; int curx = startx; int cury = starty; unsigned int count = 0; unsigned int totalPixel = _width*_height; bool problem = false; std::vector<int> case9s; std::vector<int> case6s; int i; std::vector<int>::iterator it; std::vector<cocos2d::Vec2> _points; do{ int sv = getSquareValue(curx, cury, rect, threshold); switch(sv){ case 1: case 5: case 13: /* going UP with these cases: 1 5 13 +---+---+ +---+---+ +---+---+ | 1 | | | 1 | | | 1 | | +---+---+ +---+---+ +---+---+ | | | | 4 | | | 4 | 8 | +---+---+ +---+---+ +---+---+ */ stepx = 0; stepy = -1; break; case 8: case 10: case 11: /* going DOWN with these cases: 8 10 11 +---+---+ +---+---+ +---+---+ | | | | | 2 | | 1 | 2 | +---+---+ +---+---+ +---+---+ | | 8 | | | 8 | | | 8 | +---+---+ +---+---+ +---+---+ */ stepx = 0; stepy = 1; break; case 4: case 12: case 14: /* going LEFT with these cases: 4 12 14 +---+---+ +---+---+ +---+---+ | | | | | | | | 2 | +---+---+ +---+---+ +---+---+ | 4 | | | 4 | 8 | | 4 | 8 | +---+---+ +---+---+ +---+---+ */ stepx = -1; stepy = 0; break; case 2 : case 3 : case 7 : /* going RIGHT with these cases: 2 3 7 +---+---+ +---+---+ +---+---+ | | 2 | | 1 | 2 | | 1 | 2 | +---+---+ +---+---+ +---+---+ | | | | | | | 4 | | +---+---+ +---+---+ +---+---+ */ stepx=1; stepy=0; break; case 9 : /* +---+---+ | 1 | | +---+---+ | | 8 | +---+---+ this should normally go UP, but if we already been here, we go down */ //find index from xy; i = getIndexFromPos(curx, cury); it = find (case9s.begin(), case9s.end(), i); if (it != case9s.end()) { //found, so we go down, and delete from case9s; stepx = 0; stepy = 1; case9s.erase(it); problem = true; } else { //not found, we go up, and add to case9s; stepx = 0; stepy = -1; case9s.push_back(i); } break; case 6 : /* 6 +---+---+ | | 2 | +---+---+ | 4 | | +---+---+ this normally go RIGHT, but if its coming from UP, it should go LEFT */ i = getIndexFromPos(curx, cury); it = find (case6s.begin(), case6s.end(), i); if (it != case6s.end()) { //found, so we go down, and delete from case9s; stepx = -1; stepy = 0; case6s.erase(it); problem = true; } else{ //not found, we go up, and add to case9s; stepx = 1; stepy = 0; case6s.push_back(i); } break; default: CCLOG("this shouldn't happen."); } //little optimization // if previous direction is same as current direction, // then we should modify the last vec to current curx += stepx; cury += stepy; if(stepx == prevx && stepy == prevy) { _points.back().x = (float)(curx-rect.origin.x) / _scaleFactor; _points.back().y = (float)(rect.size.height - cury + rect.origin.y) / _scaleFactor; } else if(problem) { //TODO: we triangulation cannot work collinear points, so we need to modify same point a little //TODO: maybe we can detect if we go into a hole and coming back the hole, we should extract those points and remove them _points.push_back(Vec2((float)(curx- rect.origin.x) / _scaleFactor, (float)(rect.size.height - cury + rect.origin.y) / _scaleFactor)); } else{ _points.push_back(Vec2((float)(curx-rect.origin.x) / _scaleFactor, (float)(rect.size.height - cury + rect.origin.y) / _scaleFactor)); } count++; prevx = stepx; prevy = stepy; problem = false; CCASSERT(count <= totalPixel, "oh no, marching square cannot find starting position"); } while(curx != startx || cury != starty); return _points; }