1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-22 10:53:54 +00:00
openmw/apps/opencs/model/world/cellselection.cpp

86 lines
1.8 KiB
C++
Raw Normal View History

#include "cellselection.hpp"
2022-08-04 22:00:49 +00:00
#include "cellcoordinates.hpp"
#include <cmath>
#include <limits>
2022-09-22 18:26:05 +00:00
#include <stdexcept>
2022-10-19 17:02:00 +00:00
#include <utility>
CSMWorld::CellSelection::Iterator CSMWorld::CellSelection::begin() const
{
return mCells.begin();
}
CSMWorld::CellSelection::Iterator CSMWorld::CellSelection::end() const
{
return mCells.end();
}
2022-09-22 18:26:05 +00:00
bool CSMWorld::CellSelection::add(const CellCoordinates& coordinates)
{
2022-09-22 18:26:05 +00:00
return mCells.insert(coordinates).second;
}
2022-09-22 18:26:05 +00:00
void CSMWorld::CellSelection::remove(const CellCoordinates& coordinates)
{
2022-09-22 18:26:05 +00:00
mCells.erase(coordinates);
}
2022-09-22 18:26:05 +00:00
bool CSMWorld::CellSelection::has(const CellCoordinates& coordinates) const
{
2022-09-22 18:26:05 +00:00
return mCells.find(coordinates) != end();
}
int CSMWorld::CellSelection::getSize() const
{
return mCells.size();
}
CSMWorld::CellCoordinates CSMWorld::CellSelection::getCentre() const
{
if (mCells.empty())
2022-09-22 18:26:05 +00:00
throw std::logic_error("call of getCentre on empty cell selection");
double x = 0;
double y = 0;
2022-09-22 18:26:05 +00:00
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();
2022-09-22 18:26:05 +00:00
for (Iterator iter(begin()); iter != end(); ++iter)
{
double deltaX = x - iter->getX();
double deltaY = y - iter->getY();
2022-09-22 18:26:05 +00:00
double delta = std::sqrt(deltaX * deltaX + deltaY * deltaY);
2022-09-22 18:26:05 +00:00
if (delta < distance)
{
distance = delta;
closest = iter;
}
}
return *closest;
}
2022-09-22 18:26:05 +00:00
void CSMWorld::CellSelection::move(int x, int y)
{
Container moved;
2022-09-22 18:26:05 +00:00
for (Iterator iter = begin(); iter != end(); ++iter)
moved.insert(iter->move(x, y));
2022-09-22 18:26:05 +00:00
mCells.swap(moved);
}