void Shape::fill(FrameBuffer &fb){ std::queue <Point> qpoints; Point newFP; newFP.setX((firePoint->getX()-centrePoint->getX())*k+centrePoint->getX()); newFP.setY((firePoint->getY()-centrePoint->getX())*k+centrePoint->getX()); qpoints.push(newFP); while (qpoints.size() > 0){ Point current = qpoints.front(); fb.plot(current.getX(), current.getY(), r,g,b); qpoints.pop(); // TO-DO !! //std::cout << current.getY() << " " << current.getX() << std::endl; if (isPointValid(fb, current.getX()+1, current.getY())) { if (fb.zbuffer[current.getY()][current.getX()+1] == 0){ qpoints.push(Point(current.getX()+1, current.getY())); fb.zbuffer[current.getY()][current.getX()+1] = 1; } } if (isPointValid(fb, current.getX(), current.getY()+1)) { if (fb.zbuffer[current.getY()+1][current.getX()] == 0){ qpoints.push(Point(current.getX(), current.getY()+1)); fb.zbuffer[current.getY()+1][current.getX()] = 1; } } if (isPointValid(fb, current.getX(), current.getY()-1)) { if (fb.zbuffer[current.getY()-1][current.getX()] == 0){ qpoints.push(Point(current.getX(), current.getY()-1)); fb.zbuffer[current.getY()-1][current.getX()] = 1; } } if (isPointValid(fb, current.getX()-1, current.getY())) { if (fb.zbuffer[current.getY()][current.getX()-1] == 0){ qpoints.push(Point(current.getX()-1, current.getY())); fb.zbuffer[current.getY()][current.getX()-1] = 1; } } } }
int Chessboard::GetIdOfPoint(const ChessPoint& cp)const { if (isPointValid(cp)) { return cb[cp.y*width+cp.x]; } return -1; }
int HaarFeature::slantToRect(const CB_SlantT &slant, CB_RectangleT &rect, const CB_PointT &image_size) { CB_PointT left = {slant.x-slant.L, slant.y+slant.L}; CB_PointT right = {slant.x+slant.R, slant.y+slant.R}; CB_PointT top = {slant.x, slant.y}; CB_PointT bottom = {slant.x-slant.L+slant.R, slant.y+slant.L+slant.R}; if ( isPointValid(left, image_size) == 0 || isPointValid(right, image_size) == 0 || isPointValid(bottom, image_size) == 0 ) { return 0; } rect.left = left; rect.right = right; rect.top = top; rect.bottom = bottom; return 1; }
std::vector<double> RandomGlobalPoll::computeFamilyStepPoint ( const int size ) { std::vector<double> newPoint; do { // create family index set set<RandomGlobalPoll::PopulationType::const_iterator> idxFamily = createFamilySet ( size ); // find max idx in family RandomGlobalPoll::PopulationType::const_iterator idxmaxFam= *max_element ( idxFamily.begin(), idxFamily.end(), PopulationIteratorLess() ); // compute centroid family // cout << "deltaInit: " << deltaInit << " delta: " << delta << endl; double phi = bounds.size()*delta*delta/deltaInit; std::vector<double> eta; transform ( idxFamily.begin(), idxFamily.end(),back_inserter ( eta ), EtaGenerator ( idxmin->value(),phi ) ); double sumEta = accumulate ( eta.begin(),eta.end(),0. ); // compute weights std::vector<double> weight; transform ( eta.begin(),eta.end(),back_inserter ( weight ),WeightGenerator ( sumEta ) ); // compute centroid std::vector<double> centroid = createCentroid ( idxFamily,weight ); // compute new point (reflex max idx in family with centroid family) // compute stepsize double sumFWeights = computeSumFWeights ( idxFamily,weight ); double alpha = 1 - ( idxmaxFam->value() - sumFWeights ) / ( delta+phi ); // compute new point newPoint = computeNewPoint ( centroid,alpha,idxmaxFam->point() ); } while ( !isPointValid ( newPoint ) ); return newPoint; }
bool Chessboard::SetChessPoint(const ChessPoint& cp,int id) { if(!isPointValid(cp)) { return false; } //if (GetIdOfPoint(cp)!=0) //{ // return false; //} if (GetIdOfPoint(cp)==0) { sum++; } cb[cp.y*width+cp.x]=id; return true; }
void pathFinder::findPath( p2DArray maze, const vector2 &start, const vector2 &end ) { // 起点或者终点无效,直接返回 if( !isPointValid( start, maze ) || !isPointValid( end, maze ) ) { return; } else { clearPath(); char mazeCopy[ROW][CEL]; memcpy( dirMap, maze, sizeof(char)*ROW*CEL ); memcpy( mazeCopy, maze, sizeof(char)*ROW*CEL ); MyQueue openList; openList.push_back( start ); vector2 curPos; vector2 temp; while ( 1 ) { openList.pop_front( curPos ); // 碰到了终点,可以停止了 if( curPos == end ) { dirMap[start.x][start.y] = MS_START; // readPathFromDirMap( end ); return; } // 当前点有效才需要扩展 if( isPointValid( curPos, mazeCopy ) ) { mazeCopy[curPos.x][curPos.y] = MS_WALKED; int curDir = 0; while ( curDir < 4 ) { temp.x = curPos.x + dirArray[curDir].x; temp.y = curPos.y + dirArray[curDir].y; if( isPointValid( temp, mazeCopy ) ) { openList.push_back( temp ); // 标记当前节点的上一节点 dirMap[temp.x][temp.y] = getOppositeDir( curDir ); } ++curDir; } } // 扩展列表为空,只能退出了 if( openList.isEmpty() ) { return; } } } }
bool aStarPathFinder::findPathOneStep( char (*mapdata)[COL], int startX, int startY, int endX, int endY, bool isFirst ) { if( isFirst ) { m_OpenList.setEmpty(); m_CloseList.setEmpty(); m_Start.x = startX; m_Start.y = startY; m_End.x = endX; m_End.y = endY; // 初始化地图数据 for ( int i=0; i<ROW; ++i ) { for ( int j=0; j<COL; ++j ) { m_Map[i][j].pos.x = j; m_Map[i][j].pos.y = i; m_Map[i][j].mapFlag = mapdata[i][j]; _calcH( m_Map[i][j] ); m_Map[i][j].G = 0; } } node = &m_Map[m_Start.y][m_Start.x]; node->H = INT_MAX; mapdata[m_Start.y][m_Start.x]= AF_INOPENLIST; m_OpenList.add( node ); return false; } if( !m_OpenList.isEmpty() ) { m_OpenList.remove_Top( &node ); m_CloseList.push( node ); mapdata[node->pos.y][node->pos.x] = AF_INCLOSELIST; // 探索周围的八个方向 m_Dir = 0; while ( m_Dir < DIR_NUM ) { tempPos.x = node->pos.x + dirArray[m_Dir].x; tempPos.y = node->pos.y + dirArray[m_Dir].y; if ( isPointValid( mapdata, tempPos ) ) { temp= &m_Map[tempPos.y][tempPos.x]; // 完成寻路 if( isPointEqual( temp->pos, m_End ) ) { temp->pParent = node; return true; } if ( mapdata[temp->pos.y][temp->pos.x] == SPACE ) { m_OpenList.add( temp ); mapdata[temp->pos.y][temp->pos.x] = AF_INOPENLIST; // 由于这里使用了一个方向的数组,其中斜线方向的下标 // 刚好是一个奇数,所以判断当前的方向是否为奇数即可 // 判断是否为斜线方向 if( _calcF( *temp, *node, (m_Dir%2 != 0) ) ) { temp->pParent = node; } } else { if( mapdata[temp->pos.y][temp->pos.x] == AF_INOPENLIST && _calcF( *temp, *node, (m_Dir%2 != 0) ) ) { temp->pParent = node; } else if( mapdata[temp->pos.y][temp->pos.x] == AF_INCLOSELIST && _calcF( *temp, *node, (m_Dir%2 != 0) ) ) { m_OpenList.add( temp ); m_CloseList.remove( temp ); mapdata[temp->pos.y][temp->pos.x] = AF_INOPENLIST; temp->pParent = node; } } } ++m_Dir; } } return false; }