From d01a8186d8df6e78eaa0849f13ee27ce5b67f7b7 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Thu, 15 Dec 2022 20:10:57 +0300 Subject: [PATCH] Add geom::polygon_contains --- libs/geom/include/psemek/geom/contains.hpp | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) 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); + } + }