Add point-box distance
This commit is contained in:
parent
9a47bddb73
commit
98ff134d3a
1 changed files with 24 additions and 0 deletions
24
libs/geom/include/psemek/geom/distance.hpp
Normal file
24
libs/geom/include/psemek/geom/distance.hpp
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#pragma once
|
||||
|
||||
#include <psemek/geom/point.hpp>
|
||||
#include <psemek/geom/box.hpp>
|
||||
#include <psemek/geom/math.hpp>
|
||||
|
||||
namespace psemek::geom
|
||||
{
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
T distance(point<T, N> const & p, box<T, N> const & b)
|
||||
{
|
||||
T s = T{};
|
||||
for (std::size_t i = 0; i < N; ++i)
|
||||
{
|
||||
if (p[i] < b[i].min)
|
||||
s += sqr(b[i].min - p[i]);
|
||||
else if (p[i] > b[i].max)
|
||||
s += sqr(p[i] - b[i].max);
|
||||
}
|
||||
return std::sqrt(s);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue