Add ol.geom.flat.flipXY

This commit is contained in:
Tom Payne
2013-12-02 09:07:55 +01:00
parent dd1e0c616a
commit b3473c3cba
2 changed files with 65 additions and 0 deletions

View File

@@ -74,6 +74,40 @@ ol.geom.flat.deflateCoordinatesss =
};
/**
* @param {Array.<number>} flatCoordinates Flat coordinates.
* @param {number} offset Offset.
* @param {number} end End.
* @param {number} stride Stride.
* @param {Array.<number>=} opt_dest Destination.
* @param {number=} opt_destOffset Destination offset.
* @return {Array.<number>} Flat coordinates.
*/
ol.geom.flat.flipXY =
function(flatCoordinates, offset, end, stride, opt_dest, opt_destOffset) {
var dest, destOffset;
if (goog.isDef(opt_dest)) {
dest = opt_dest;
destOffset = goog.isDef(opt_destOffset) ? opt_destOffset : 0;
} else {
goog.asserts.assert(!goog.isDef(opt_destOffset));
dest = [];
destOffset = 0;
}
var j, k;
for (j = offset; j < end; ) {
var x = flatCoordinates[j++];
dest[destOffset++] = flatCoordinates[j++];
dest[destOffset++] = x;
for (k = 2; k < stride; ++k) {
dest[destOffset++] = flatCoordinates[j++];
}
}
dest.length = destOffset;
return dest;
};
/**
* @param {Array.<number>} flatCoordinates Flat coordinates.
* @param {number} offset Offset.

View File

@@ -44,6 +44,37 @@ describe('ol.geom.flat', function() {
});
describe('ol.geom.flat.flipXY', function() {
it('can flip XY coordinates', function() {
var flatCoordinates = ol.geom.flat.flipXY([1, 2, 3, 4], 0, 4, 2);
expect(flatCoordinates).to.eql([2, 1, 4, 3]);
});
it('can flip XY coordinates while preserving other dimensions', function() {
var flatCoordinates = ol.geom.flat.flipXY(
[1, 2, 3, 4, 5, 6, 7, 8], 0, 8, 4);
expect(flatCoordinates).to.eql([2, 1, 3, 4, 6, 5, 7, 8]);
});
it('can flip XY coordinates in place', function() {
var flatCoordinates = [1, 2, 3, 4];
expect(ol.geom.flat.flipXY(flatCoordinates, 0, 4, 2, flatCoordinates)).
to.be(flatCoordinates);
expect(flatCoordinates).to.eql([2, 1, 4, 3]);
});
it('can flip XY coordinates in place while preserving other dimensions',
function() {
var flatCoordinates = [1, 2, 3, 4, 5, 6, 7, 8, 9];
expect(ol.geom.flat.flipXY(
flatCoordinates, 0, 9, 3, flatCoordinates)).
to.be(flatCoordinates);
expect(flatCoordinates).to.eql([2, 1, 3, 5, 4, 6, 8, 7, 9]);
});
});
describe('ol.geom.flat.inflateCoordinatess', function() {
it('inflates arrays of coordinates', function() {