From 656af726155c7f7f74dc7334c5d866040046e1d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Harrtell?= Date: Sat, 5 Nov 2016 14:34:09 +0100 Subject: [PATCH] Use winding number algorithm for linearRingContainsXY --- src/ol/geom/flat/contains.js | 22 +++++++++++++++------- test/spec/ol/geom/flat/contains.test.js | 2 +- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/ol/geom/flat/contains.js b/src/ol/geom/flat/contains.js index 3e7cfea7a0..e771be89eb 100644 --- a/src/ol/geom/flat/contains.js +++ b/src/ol/geom/flat/contains.js @@ -36,22 +36,30 @@ ol.geom.flat.contains.linearRingContainsExtent = function(flatCoordinates, offse * @return {boolean} Contains (x, y). */ ol.geom.flat.contains.linearRingContainsXY = function(flatCoordinates, offset, end, stride, x, y) { - // http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html - var contains = false; + // http://geomalgorithms.com/a03-_inclusion.html + // Copyright 2000 softSurfer, 2012 Dan Sunday + // This code may be freely used and modified for any purpose + // providing that this copyright notice is included with it. + // SoftSurfer makes no warranty for this code, and cannot be held + // liable for any real or imagined damage resulting from its use. + // Users of this code must verify correctness for their application. + var wn = 0; var x1 = flatCoordinates[end - stride]; var y1 = flatCoordinates[end - stride + 1]; for (; offset < end; offset += stride) { var x2 = flatCoordinates[offset]; var y2 = flatCoordinates[offset + 1]; - var intersect = ((y1 > y) != (y2 > y)) && - (x < (x2 - x1) * (y - y1) / (y2 - y1) + x1); - if (intersect) { - contains = !contains; + if (y1 <= y) { + if (y2 > y && ((x2 - x1) * (y - y1)) - ((x - x1) * (y2 - y1)) > 0) { + wn++; + } + } else if (y2 <= y && ((x2 - x1) * (y - y1)) - ((x - x1) * (y2 - y1)) < 0) { + wn--; } x1 = x2; y1 = y2; } - return contains; + return wn !== 0; }; diff --git a/test/spec/ol/geom/flat/contains.test.js b/test/spec/ol/geom/flat/contains.test.js index 68f4e1b0dc..3a35a8d5e8 100644 --- a/test/spec/ol/geom/flat/contains.test.js +++ b/test/spec/ol/geom/flat/contains.test.js @@ -10,7 +10,7 @@ describe('ol.geom.flat.contains', function() { var flatCoordinatesSimple = [0, 0, 1, 0, 1, 1, 0, 1]; var flatCoordinatesNonSimple = [0, 0, 4, 0, 4, 3, 1, 3, 1, 2, 3, 2, 3, 1, 2, 1, 2, 4, 0, 4]; - describe('ol.geom.flat.closest.getMaxSquaredDelta', function() { + describe('ol.geom.flat.contains.linearRingContainsXY', function() { it('returns true for point inside a simple polygon', function() { expect(ol.geom.flat.contains.linearRingContainsXY(