Add geom::polygon_contains
This commit is contained in:
parent
9f045c18a8
commit
d01a8186d8
1 changed files with 28 additions and 0 deletions
|
|
@ -5,6 +5,7 @@
|
||||||
#include <psemek/geom/point.hpp>
|
#include <psemek/geom/point.hpp>
|
||||||
#include <psemek/geom/simplex.hpp>
|
#include <psemek/geom/simplex.hpp>
|
||||||
#include <psemek/geom/orientation.hpp>
|
#include <psemek/geom/orientation.hpp>
|
||||||
|
#include <psemek/util/range.hpp>
|
||||||
|
|
||||||
namespace psemek::geom
|
namespace psemek::geom
|
||||||
{
|
{
|
||||||
|
|
@ -55,4 +56,31 @@ namespace psemek::geom
|
||||||
return contains(default_robust_tag, t, p);
|
return contains(default_robust_tag, t, p);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename Iterator, typename T>
|
||||||
|
bool polygon_contains(Iterator begin, Iterator end, point<T, 2> 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 <typename Polygon, typename T>
|
||||||
|
bool polygon_contains(Polygon const & polygon, point<T, 2> const & p)
|
||||||
|
{
|
||||||
|
return polygon_contains(util::begin(polygon), util::end(polygon), p);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue