Я предлагаю что-то вроде этого в качестве отправной точки:
- посмотреть вживую: http://ideone.com/LQk8U
обратите внимание, что я могу угадать направление вашей осинеправильно (в этом случае вам может понадобиться поменять местами мин / макс) Редактировать исправлено в соответствии с комментарием «Север положительный, а Восток положительный»
для вектора, просто выполните std::acummulate(v.begin(), v.end(), v[0]....)
Если ваши сценарии усложняются, см. Библиотека ускоренной геометрии
.
#include <iostream>
#include <numeric>
struct GEOGRAPHIC_REGION
{
float fNorthMost;
float fSouthMost;
float fWestMost;
float fEastMost;
};
GEOGRAPHIC_REGION combine(const GEOGRAPHIC_REGION& accum, const GEOGRAPHIC_REGION& tocombine)
{
GEOGRAPHIC_REGION combined = {
std::max(accum.fNorthMost, tocombine.fNorthMost),
std::min(accum.fSouthMost, tocombine.fSouthMost),
std::min(accum.fWestMost, tocombine.fWestMost),
std::max(accum.fEastMost, tocombine.fEastMost)
};
return combined;
}
int main()
{
const GEOGRAPHIC_REGION regions[] =
{
{ 2,-1,-1,1 },
{ 1,-2,-1,1 },
{ 1,-1,-2,1 },
{ 1,-1,-1,2 },
};
GEOGRAPHIC_REGION super = std::accumulate(regions, regions+4, regions[0], combine);
std::cout << "{ " << super.fNorthMost << ", "
<< super.fSouthMost << ", "
<< super.fWestMost << ", "
<< super.fEastMost << " }" << std::endl;
}