General purpose getCorner function for extents
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
goog.provide('ol.Extent');
|
||||
goog.provide('ol.extent');
|
||||
goog.provide('ol.extent.Corner');
|
||||
goog.provide('ol.extent.Relationship');
|
||||
|
||||
goog.require('goog.asserts');
|
||||
@@ -17,6 +18,18 @@ goog.require('ol.TransformFunction');
|
||||
ol.Extent;
|
||||
|
||||
|
||||
/**
|
||||
* Extent corner.
|
||||
* @enum {string}
|
||||
*/
|
||||
ol.extent.Corner = {
|
||||
BOTTOM_LEFT: 'bottom-left',
|
||||
BOTTOM_RIGHT: 'bottom-right',
|
||||
TOP_LEFT: 'top-left',
|
||||
TOP_RIGHT: 'top-right'
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Relationship to an extent.
|
||||
* @enum {number}
|
||||
@@ -456,6 +469,30 @@ ol.extent.getCenter = function(extent) {
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Get a corner coordinate of an extent.
|
||||
* @param {ol.Extent} extent Extent.
|
||||
* @param {ol.extent.Corner} corner Corner.
|
||||
* @return {ol.Coordinate} Corner coordinate.
|
||||
*/
|
||||
ol.extent.getCorner = function(extent, corner) {
|
||||
var coordinate;
|
||||
if (corner === ol.extent.Corner.BOTTOM_LEFT) {
|
||||
coordinate = ol.extent.getBottomLeft(extent);
|
||||
} else if (corner === ol.extent.Corner.BOTTOM_RIGHT) {
|
||||
coordinate = ol.extent.getBottomRight(extent);
|
||||
} else if (corner === ol.extent.Corner.TOP_LEFT) {
|
||||
coordinate = ol.extent.getTopLeft(extent);
|
||||
} else if (corner === ol.extent.Corner.TOP_RIGHT) {
|
||||
coordinate = ol.extent.getTopRight(extent);
|
||||
} else {
|
||||
goog.asserts.fail('Invalid corner: %s', corner);
|
||||
}
|
||||
goog.asserts.assert(goog.isDef(coordinate));
|
||||
return coordinate;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.Extent} extent1 Extent 1.
|
||||
* @param {ol.Extent} extent2 Extent 2.
|
||||
|
||||
@@ -177,6 +177,31 @@ describe('ol.extent', function() {
|
||||
});
|
||||
});
|
||||
|
||||
describe('getCorner', function() {
|
||||
var extent = [1, 2, 3, 4];
|
||||
|
||||
it('gets the bottom left', function() {
|
||||
var corner = ol.extent.Corner.BOTTOM_LEFT;
|
||||
expect(ol.extent.getCorner(extent, corner)).to.eql([1, 2]);
|
||||
});
|
||||
|
||||
it('gets the bottom right', function() {
|
||||
var corner = ol.extent.Corner.BOTTOM_RIGHT;
|
||||
expect(ol.extent.getCorner(extent, corner)).to.eql([3, 2]);
|
||||
});
|
||||
|
||||
it('gets the top left', function() {
|
||||
var corner = ol.extent.Corner.TOP_LEFT;
|
||||
expect(ol.extent.getCorner(extent, corner)).to.eql([1, 4]);
|
||||
});
|
||||
|
||||
it('gets the top right', function() {
|
||||
var corner = ol.extent.Corner.TOP_RIGHT;
|
||||
expect(ol.extent.getCorner(extent, corner)).to.eql([3, 4]);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('getForViewAndSize', function() {
|
||||
|
||||
it('works for a unit square', function() {
|
||||
@@ -538,5 +563,6 @@ describe('ol.extent', function() {
|
||||
|
||||
goog.require('goog.vec.Mat4');
|
||||
goog.require('ol.extent');
|
||||
goog.require('ol.extent.Corner');
|
||||
goog.require('ol.extent.Relationship');
|
||||
goog.require('ol.proj');
|
||||
|
||||
Reference in New Issue
Block a user