Merge pull request #8910 from fredj/containsXY
Implements containsXY for all geometries
This commit is contained in:
@@ -92,7 +92,8 @@ class Geometry extends BaseObject {
|
||||
* @return {boolean} Contains (x, y).
|
||||
*/
|
||||
containsXY(x, y) {
|
||||
return false;
|
||||
const coord = this.getClosestPoint([x, y]);
|
||||
return coord[0] === x && coord[1] === y;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -443,4 +443,37 @@ describe('ol.geom.LineString', function() {
|
||||
|
||||
});
|
||||
|
||||
describe('#containsXY()', function() {
|
||||
|
||||
let lineString;
|
||||
beforeEach(function() {
|
||||
lineString = new LineString([
|
||||
[0, 0, 0, 0],
|
||||
[1, -1, 2, 1],
|
||||
[2, -2, 4, 2],
|
||||
[4, -4, 8, 4],
|
||||
[8, -8, 16, 8],
|
||||
[12, -12, 24, 12],
|
||||
[14, -14, 28, 14],
|
||||
[15, -15, 30, 15],
|
||||
[16, -16, 32, 16],
|
||||
[18, -18, 36, 18],
|
||||
[22, -22, 44, 22]
|
||||
]);
|
||||
});
|
||||
|
||||
it('does contain XY', function() {
|
||||
expect(lineString.containsXY(1, -1)).to.be(true);
|
||||
expect(lineString.containsXY(16, -16)).to.be(true);
|
||||
expect(lineString.containsXY(3, -3)).to.be(true);
|
||||
});
|
||||
|
||||
it('does not contain XY', function() {
|
||||
expect(lineString.containsXY(1, 3)).to.be(false);
|
||||
expect(lineString.containsXY(2, 2)).to.be(false);
|
||||
expect(lineString.containsXY(2, 3)).to.be(false);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@@ -355,4 +355,30 @@ describe('ol.geom.MultiLineString', function() {
|
||||
});
|
||||
});
|
||||
|
||||
describe('#containsXY()', function() {
|
||||
|
||||
let multiLineString;
|
||||
beforeEach(function() {
|
||||
multiLineString = new MultiLineString(
|
||||
[[[1, 2, 3], [4, 5, 6]], [[-1, -1, 9], [2, 2, 12]]]);
|
||||
});
|
||||
|
||||
it('does contain XY', function() {
|
||||
expect(multiLineString.containsXY(1, 2)).to.be(true);
|
||||
expect(multiLineString.containsXY(4, 5)).to.be(true);
|
||||
expect(multiLineString.containsXY(3, 4)).to.be(true);
|
||||
|
||||
expect(multiLineString.containsXY(-1, -1)).to.be(true);
|
||||
expect(multiLineString.containsXY(2, 2)).to.be(true);
|
||||
expect(multiLineString.containsXY(0, 0)).to.be(true);
|
||||
});
|
||||
|
||||
it('does not contain XY', function() {
|
||||
expect(multiLineString.containsXY(1, 3)).to.be(false);
|
||||
expect(multiLineString.containsXY(2, 11)).to.be(false);
|
||||
expect(multiLineString.containsXY(-2, 3)).to.be(false);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@@ -287,4 +287,27 @@ describe('ol.geom.MultiPoint', function() {
|
||||
|
||||
});
|
||||
|
||||
describe('#containsXY()', function() {
|
||||
|
||||
it('does contain XY', function() {
|
||||
const multi = new MultiPoint([[1, 2], [10, 20]]);
|
||||
|
||||
expect(multi.containsXY(1, 2)).to.be(true);
|
||||
expect(multi.containsXY(10, 20)).to.be(true);
|
||||
});
|
||||
|
||||
it('does not contain XY', function() {
|
||||
const multi = new MultiPoint([[1, 2], [10, 20]]);
|
||||
|
||||
expect(multi.containsXY(1, 3)).to.be(false);
|
||||
expect(multi.containsXY(2, 2)).to.be(false);
|
||||
expect(multi.containsXY(2, 3)).to.be(false);
|
||||
|
||||
expect(multi.containsXY(10, 30)).to.be(false);
|
||||
expect(multi.containsXY(20, 20)).to.be(false);
|
||||
expect(multi.containsXY(20, 30)).to.be(false);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@@ -215,4 +215,22 @@ describe('ol.geom.Point', function() {
|
||||
|
||||
});
|
||||
|
||||
describe('#containsXY()', function() {
|
||||
|
||||
it('does contain XY', function() {
|
||||
const point = new Point([1, 2]);
|
||||
|
||||
expect(point.containsXY(1, 2)).to.be(true);
|
||||
});
|
||||
|
||||
it('does not contain XY', function() {
|
||||
const point = new Point([1, 2]);
|
||||
|
||||
expect(point.containsXY(1, 3)).to.be(false);
|
||||
expect(point.containsXY(2, 2)).to.be(false);
|
||||
expect(point.containsXY(2, 3)).to.be(false);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user