diff options
author | zmiao <miao.zhao@mapbox.com> | 2020-02-28 17:18:05 +0200 |
---|---|---|
committer | zmiao <miao.zhao@mapbox.com> | 2020-03-05 20:53:16 +0200 |
commit | c69d3b46b12655533aaee5740eba25f5bf7b11de (patch) | |
tree | 0ad4cbeca0d2bcf59441434cef41399f189ef1d8 /src | |
parent | 9fbcb45c89ee3cec2b94253515a23df5986a4c7b (diff) | |
download | qtlocation-mapboxgl-c69d3b46b12655533aaee5740eba25f5bf7b11de.tar.gz |
[core] Fix within case when point is on polygon boundary
Diffstat (limited to 'src')
-rw-r--r-- | src/mbgl/util/geometry_within.cpp | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/src/mbgl/util/geometry_within.cpp b/src/mbgl/util/geometry_within.cpp index c79952b8f2..d6340fc14e 100644 --- a/src/mbgl/util/geometry_within.cpp +++ b/src/mbgl/util/geometry_within.cpp @@ -9,6 +9,14 @@ bool rayIntersect(const Point<double>& p, const Point<double>& p1, const Point<d return ((p1.y > p.y) != (p2.y > p.y)) && (p.x < (p2.x - p1.x) * (p.y - p1.y) / (p2.y - p1.y) + p1.x); } +bool onBoundary(const Point<double>& p, const Point<double>& p1, const Point<double>& p2) { + const auto x1 = p.x - p1.x; + const auto y1 = p.y - p1.y; + const auto x2 = p.x - p2.x; + const auto y2 = p.y - p2.y; + return (x1 * y2 - x2 * y1 == 0) && (x1 * x2 <= 0) && (y1 * y2 <= 0); +} + // a, b are end points for line segment1, c and d are end points for line segment2 bool lineIntersectLine(const Point<double>& a, const Point<double>& b, const Point<double>& c, const Point<double>& d) { const auto perp = [](const Point<double>& v1, const Point<double>& v2) { return (v1.x * v2.y - v1.y * v2.x); }; @@ -134,6 +142,7 @@ bool pointWithinPolygon(const Point<double>& point, const Polygon<double>& polyg const auto length = ring.size(); // loop through every edge of the ring for (std::size_t i = 0; i < length - 1; ++i) { + if (onBoundary(point, ring[i], ring[i + 1])) return false; if (rayIntersect(point, ring[i], ring[i + 1])) { within = !within; } |