point: add convenience centroid function

This commit is contained in:
Danny Robson 2018-04-26 16:30:16 +10:00
parent c5d142ec9a
commit d011c60359

View File

@ -203,6 +203,26 @@ namespace util {
util::point<S,T> util::point<S,T>
> >
furthest (util::view<const util::point<S,T>*>); furthest (util::view<const util::point<S,T>*>);
///////////////////////////////////////////////////////////////////////////
/// computes the mean point across a view of points
template <typename InputT>
auto
center (util::view<InputT> points)
{
CHECK_NEZ (points.size ());
using point_type = typename std::iterator_traits<InputT>::value_type;
using value_type = typename point_type::value_type;
util::vector<point_type::elements,value_type> accum = 0;
for (auto const &i: points)
accum += i.template as<util::vector> ();
return (accum / points.size ()).template as<util::point> ();
}
} }
#endif // __UTIL_POINT_HPP #endif // __UTIL_POINT_HPP