Use winding number algorithm for linearRingContainsXY
This commit is contained in:
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user