diff --git a/libs/geom/include/psemek/geom/contains.hpp b/libs/geom/include/psemek/geom/contains.hpp index 064f48f2..8467382d 100644 --- a/libs/geom/include/psemek/geom/contains.hpp +++ b/libs/geom/include/psemek/geom/contains.hpp @@ -5,6 +5,7 @@ #include #include #include +#include namespace psemek::geom { @@ -55,4 +56,31 @@ namespace psemek::geom return contains(default_robust_tag, t, p); } + template + bool polygon_contains(Iterator begin, Iterator end, point const & p) + { + int count = 0; + for (auto it = begin, prev = std::prev(end); it != end; prev = it++) + { + auto p0 = *prev; + auto p1 = *it; + if (p0[1] > p1[1]) + std::swap(p0, p1); + + if (p0[1] <= p[1] && p[1] < p1[1]) + { + if (orientation(p0, p1, p) == sign_t::positive) + ++count; + } + } + + return (count % 2) == 1; + } + + template + bool polygon_contains(Polygon const & polygon, point const & p) + { + return polygon_contains(util::begin(polygon), util::end(polygon), p); + } + }