Make ol.geom.Circle support #intersectsExtent, with tests
This commit is contained in:
@@ -137,6 +137,29 @@ ol.geom.Circle.prototype.getType = function() {
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @api stable
|
||||
*/
|
||||
ol.geom.Circle.prototype.intersectsExtent = function(extent) {
|
||||
var circleExtent = this.getExtent();
|
||||
if (ol.extent.intersects(extent, circleExtent)) {
|
||||
var center = this.getCenter();
|
||||
|
||||
if (extent[0] <= center[0] && extent[2] >= center[0]) {
|
||||
return true;
|
||||
}
|
||||
if (extent[1] <= center[1] && extent[3] >= center[1]) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return ol.extent.forEachCorner(extent, this.containsCoordinate, this);
|
||||
}
|
||||
return false;
|
||||
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Set the center of the circle as {@link ol.Coordinate coordinate}.
|
||||
* @param {ol.Coordinate} center Center.
|
||||
|
||||
@@ -203,6 +203,30 @@ describe('ol.geom.Circle', function() {
|
||||
|
||||
});
|
||||
|
||||
describe('#intersectsExtent', function() {
|
||||
|
||||
it('return false for non matching extent', function() {
|
||||
expect(circle.intersectsExtent([0.9, 0.9, 10, 10])).to.be(false);
|
||||
});
|
||||
|
||||
it('return true for extent within circle', function() {
|
||||
expect(circle.intersectsExtent([0.5, 0.5, 0.5, 0.5])).to.be(true);
|
||||
});
|
||||
|
||||
it('return true for extent overlapping circle', function() {
|
||||
expect(circle.intersectsExtent([0.5, 0.5, 10, 10])).to.be(true);
|
||||
});
|
||||
|
||||
it('return true for non-overlapping but intersecting extent', function() {
|
||||
expect(circle.intersectsExtent([0, 1, 1, 2])).to.be(true);
|
||||
});
|
||||
|
||||
it('returns true for the geom\'s own extent', function() {
|
||||
expect(circle.intersectsExtent(circle.getExtent())).to.be(true);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user