Path Map::pathFind(const Point2D& source, const Point2D& destination) const{
  //Return empty Path-container, if path not found.
  if (!(inRange(destination) && inRange(source)))
    throw out_of_range("Source or destination is out of map.");
  if (!(isPassable(destination) && isPassable(source)))
    return Path();
  Wavefront wavefront = { destination };
  auto field = fieldConstruct();
  int waveDistance = 0;
  const auto wavefrontEnd = wavefront.end();
  auto currentPoint = wavefront.begin();
  while (!wavefront.empty())
  {
    while (currentPoint != wavefrontEnd)
    {
      if (stepIsNotFolly(field, waveDistance, *currentPoint)){
        field[*currentPoint] = waveDistance;
        if (source == *currentPoint)
          return fillPath(field, source, destination);
        const auto nextPoints = this->nextPoints(*currentPoint);
        insert(wavefront, currentPoint, nextPoints);
      }
      wavefront.erase(currentPoint++);
    }
    currentPoint = wavefront.begin();
    ++waveDistance;
  }
  return Path();
}
Example #2
0
File: main.cpp Project: CCJY/coliru
 Path pathFind(const Point2D& source, const Point2D& destination) const{
   //Return empty Path-container, if path not found or call with invalid argument.
   if (!(isPassable(destination) && isPassable(source)))
     return Path();
   Wavefront wavefront = { destination };
   auto field = fieldConstruct();
   int waveDistance = 0;
   const auto wavefrontEnd = wavefront.end();
   auto currentPoint = wavefront.begin();
   while (!wavefront.empty())
   {
     while (currentPoint != wavefrontEnd)
     {
       field[*currentPoint] = waveDistance;
       if (source == *currentPoint)
         return fillPath(field, source, destination);
       insert(wavefront, currentPoint, nextPoints(field, waveDistance, *currentPoint));
       wavefront.erase(currentPoint++);
     }
     currentPoint = wavefront.begin();
     ++waveDistance;
   }
   return Path();
 }