31 lines
1.1 KiB
C++
31 lines
1.1 KiB
C++
#include "evo/simulation/spatial.hpp"
|
|
|
|
#include <algorithm>
|
|
|
|
namespace evo {
|
|
namespace {
|
|
std::uint64_t axis_distance(Millimetres first, Millimetres second) noexcept {
|
|
const auto direct =
|
|
first.value > second.value ? first.value - second.value : second.value - first.value;
|
|
return std::min(direct, domain_extent_mm - direct);
|
|
}
|
|
} // namespace
|
|
|
|
std::expected<std::vector<EntityId>, Error> query_neighbors(const SimulationWorld& world,
|
|
Position2 center, Millimetres radius) {
|
|
if (center.x.value >= domain_extent_mm || center.y.value >= domain_extent_mm) {
|
|
return std::unexpected{Error::invalid_position};
|
|
}
|
|
const auto squared_radius = static_cast<std::uint64_t>(radius.value) * radius.value;
|
|
std::vector<EntityId> matches;
|
|
for (const auto& entity : world.entities()) {
|
|
const auto dx = axis_distance(center.x, entity.position.x);
|
|
const auto dy = axis_distance(center.y, entity.position.y);
|
|
if (dx * dx + dy * dy <= squared_radius) {
|
|
matches.push_back(entity.id);
|
|
}
|
|
}
|
|
return matches;
|
|
}
|
|
} // namespace evo
|