Something went wrong. Try again.
OpenMW is an open implementation of the Morrowind engine
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586#include "cellselection.hpp"
#include "cellcoordinates.hpp"
#include <cmath>#include <limits>#include <stdexcept>#include <utility>
CSMWorld::CellSelection::Iterator CSMWorld::CellSelection::begin() const{ return mCells.begin();}
CSMWorld::CellSelection::Iterator CSMWorld::CellSelection::end() const{ return mCells.end();}
bool CSMWorld::CellSelection::add(const CellCoordinates& coordinates){ return mCells.insert(coordinates).second;}
void CSMWorld::CellSelection::remove(const CellCoordinates& coordinates){ mCells.erase(coordinates);}
bool CSMWorld::CellSelection::has(const CellCoordinates& coordinates) const{ return mCells.find(coordinates) != end();}
int CSMWorld::CellSelection::getSize() const{ return static_cast<int>(mCells.size());}
CSMWorld::CellCoordinates CSMWorld::CellSelection::getCentre() const{ if (mCells.empty()) throw std::logic_error("call of getCentre on empty cell selection");
double x = 0; double y = 0;
for (Iterator iter = begin(); iter != end(); ++iter) { x += iter->getX(); y += iter->getY(); }
x /= mCells.size(); y /= mCells.size();
Iterator closest = begin(); double distance = std::numeric_limits<double>::max();
for (Iterator iter(begin()); iter != end(); ++iter) { double deltaX = x - iter->getX(); double deltaY = y - iter->getY();
double delta = std::sqrt(deltaX * deltaX + deltaY * deltaY);
if (delta < distance) { distance = delta; closest = iter; } }
return *closest;}
void CSMWorld::CellSelection::move(int x, int y){ Container moved;
for (Iterator iter = begin(); iter != end(); ++iter) moved.insert(iter->move(x, y));
mCells.swap(moved);}