Remove goog.math.modulo and goog.math.lerp

This commit is contained in:
Nicholas Latham
2016-04-04 19:43:24 +12:00
committed by Frédéric Junod
parent 19fd3d6987
commit 2ee1969de7
11 changed files with 82 additions and 23 deletions

View File

@@ -1,7 +1,7 @@
goog.provide('ol.Attribution');
goog.require('goog.math');
goog.require('ol.TileRange');
goog.require('ol.math');
/**
@@ -80,8 +80,8 @@ ol.Attribution.prototype.intersectsAnyTileRange = function(tileRanges, tileGrid,
if (tileRange.minX < extentTileRange.minX ||
tileRange.maxX > extentTileRange.maxX) {
if (testTileRange.intersects(new ol.TileRange(
goog.math.modulo(tileRange.minX, width),
goog.math.modulo(tileRange.maxX, width),
ol.math.modulo(tileRange.minX, width),
ol.math.modulo(tileRange.maxX, width),
tileRange.minY, tileRange.maxY))) {
return true;
}

View File

@@ -2,8 +2,8 @@ goog.provide('ol.Coordinate');
goog.provide('ol.CoordinateFormatType');
goog.provide('ol.coordinate');
goog.require('goog.math');
goog.require('goog.string');
goog.require('ol.math');
/**
@@ -129,7 +129,7 @@ ol.coordinate.createStringXY = function(opt_fractionDigits) {
* @return {string} String.
*/
ol.coordinate.degreesToStringHDMS_ = function(degrees, hemispheres, opt_fractionDigits) {
var normalizedDegrees = goog.math.modulo(degrees + 180, 360) - 180;
var normalizedDegrees = ol.math.modulo(degrees + 180, 360) - 180;
var x = Math.abs(3600 * normalizedDegrees);
var dflPrecision = opt_fractionDigits || 0;
return Math.floor(x / 3600) + '\u00b0 ' +

View File

@@ -1,7 +1,6 @@
goog.provide('ol.geom.flat.closest');
goog.require('goog.asserts');
goog.require('goog.math');
goog.require('ol.math');
@@ -31,7 +30,7 @@ ol.geom.flat.closest.point = function(flatCoordinates, offset1, offset2, stride,
offset = offset2;
} else if (t > 0) {
for (i = 0; i < stride; ++i) {
closestPoint[i] = goog.math.lerp(flatCoordinates[offset1 + i],
closestPoint[i] = ol.math.lerp(flatCoordinates[offset1 + i],
flatCoordinates[offset2 + i], t);
}
closestPoint.length = stride;

View File

@@ -1,8 +1,8 @@
goog.provide('ol.geom.flat.interpolate');
goog.require('goog.asserts');
goog.require('goog.math');
goog.require('ol.array');
goog.require('ol.math');
/**
@@ -52,9 +52,9 @@ ol.geom.flat.interpolate.lineString = function(flatCoordinates, offset, end, str
var t = (target - cumulativeLengths[-index - 2]) /
(cumulativeLengths[-index - 1] - cumulativeLengths[-index - 2]);
var o = offset + (-index - 2) * stride;
pointX = goog.math.lerp(
pointX = ol.math.lerp(
flatCoordinates[o], flatCoordinates[o + stride], t);
pointY = goog.math.lerp(
pointY = ol.math.lerp(
flatCoordinates[o + 1], flatCoordinates[o + stride + 1], t);
} else {
pointX = flatCoordinates[offset + index * stride];
@@ -127,7 +127,7 @@ ol.geom.flat.lineStringCoordinateAtM = function(flatCoordinates, offset, end, st
coordinate = [];
var i;
for (i = 0; i < stride - 1; ++i) {
coordinate.push(goog.math.lerp(flatCoordinates[(lo - 1) * stride + i],
coordinate.push(ol.math.lerp(flatCoordinates[(lo - 1) * stride + i],
flatCoordinates[lo * stride + i], t));
}
coordinate.push(m);

View File

@@ -1,7 +1,6 @@
goog.provide('ol.geom.Polygon');
goog.require('goog.asserts');
goog.require('goog.math');
goog.require('ol');
goog.require('ol.array');
goog.require('ol.extent');
@@ -19,6 +18,7 @@ goog.require('ol.geom.flat.interiorpoint');
goog.require('ol.geom.flat.intersectsextent');
goog.require('ol.geom.flat.orient');
goog.require('ol.geom.flat.simplify');
goog.require('ol.math');
/**
@@ -467,7 +467,7 @@ ol.geom.Polygon.makeRegular = function(polygon, center, radius, opt_angle) {
var angle, offset;
for (var i = 0; i <= sides; ++i) {
offset = i * stride;
angle = startAngle + (goog.math.modulo(i, sides) * 2 * Math.PI / sides);
angle = startAngle + (ol.math.modulo(i, sides) * 2 * Math.PI / sides);
flatCoordinates[offset] = center[0] + (radius * Math.cos(angle));
flatCoordinates[offset + 1] = center[1] + (radius * Math.sin(angle));
}

View File

@@ -179,3 +179,27 @@ ol.math.toDegrees = function(angleInRadians) {
ol.math.toRadians = function(angleInDegrees) {
return angleInDegrees * Math.PI / 180;
};
/**
* Returns the modulo of a / b, depending on the sign of b.
*
* @param {number} a Dividend.
* @param {number} b Divisor.
* @return {number} Modulo.
*/
ol.math.modulo = function(a, b) {
var r = a % b;
return r * b < 0 ? r + b : r;
};
/**
* Calculates the linearly interpolated value of x between a and b.
*
* @param {number} a Number
* @param {number} b Number
* @param {number} x Value to be interpolated.
* @return {number} Interpolated value.
*/
ol.math.lerp = function(a, b, x) {
return a + x * (b - a);
};

View File

@@ -1,8 +1,8 @@
goog.provide('ol.reproj.Triangulation');
goog.require('goog.asserts');
goog.require('goog.math');
goog.require('ol.extent');
goog.require('ol.math');
goog.require('ol.proj');
@@ -262,10 +262,10 @@ ol.reproj.Triangulation.prototype.addQuad_ = function(a, b, c, d,
if (wrapsX) {
goog.asserts.assert(this.sourceWorldWidth_);
var centerSrcEstimX =
(goog.math.modulo(aSrc[0], this.sourceWorldWidth_) +
goog.math.modulo(cSrc[0], this.sourceWorldWidth_)) / 2;
(ol.math.modulo(aSrc[0], this.sourceWorldWidth_) +
ol.math.modulo(cSrc[0], this.sourceWorldWidth_)) / 2;
dx = centerSrcEstimX -
goog.math.modulo(centerSrc[0], this.sourceWorldWidth_);
ol.math.modulo(centerSrc[0], this.sourceWorldWidth_);
} else {
dx = (aSrc[0] + cSrc[0]) / 2 - centerSrc[0];
}

View File

@@ -1,12 +1,12 @@
goog.provide('ol.source.TileArcGISRest');
goog.require('goog.asserts');
goog.require('goog.math');
goog.require('goog.uri.utils');
goog.require('ol');
goog.require('ol.TileCoord');
goog.require('ol.extent');
goog.require('ol.object');
goog.require('ol.math');
goog.require('ol.proj');
goog.require('ol.size');
goog.require('ol.source.TileImage');
@@ -105,7 +105,7 @@ ol.source.TileArcGISRest.prototype.getRequestUrl_ = function(tileCoord, tileSize
if (urls.length == 1) {
url = urls[0];
} else {
var index = goog.math.modulo(ol.tilecoord.hash(tileCoord), urls.length);
var index = ol.math.modulo(ol.tilecoord.hash(tileCoord), urls.length);
url = urls[index];
}

View File

@@ -5,13 +5,13 @@
goog.provide('ol.source.TileWMS');
goog.require('goog.asserts');
goog.require('goog.math');
goog.require('goog.string');
goog.require('goog.uri.utils');
goog.require('ol');
goog.require('ol.TileCoord');
goog.require('ol.extent');
goog.require('ol.object');
goog.require('ol.math');
goog.require('ol.proj');
goog.require('ol.size');
goog.require('ol.source.TileImage');
@@ -277,7 +277,7 @@ ol.source.TileWMS.prototype.getRequestUrl_ = function(tileCoord, tileSize, tileE
if (urls.length == 1) {
url = urls[0];
} else {
var index = goog.math.modulo(ol.tilecoord.hash(tileCoord), urls.length);
var index = ol.math.modulo(ol.tilecoord.hash(tileCoord), urls.length);
url = urls[index];
}
return goog.uri.utils.appendParamsFromMap(url, params);

View File

@@ -2,8 +2,8 @@ goog.provide('ol.TileUrlFunction');
goog.provide('ol.TileUrlFunctionType');
goog.require('goog.asserts');
goog.require('goog.math');
goog.require('ol.TileCoord');
goog.require('ol.math');
goog.require('ol.tilecoord');
@@ -109,7 +109,7 @@ ol.TileUrlFunction.createFromTileUrlFunctions = function(tileUrlFunctions) {
return undefined;
} else {
var h = ol.tilecoord.hash(tileCoord);
var index = goog.math.modulo(h, tileUrlFunctions.length);
var index = ol.math.modulo(h, tileUrlFunctions.length);
return tileUrlFunctions[index](tileCoord, pixelRatio, projection);
}
});

View File

@@ -155,5 +155,41 @@ describe('ol.math.toRadians', function() {
});
});
describe('ol.math.modulo', function() {
it('256 / 8 returns 0', function() {
expect(ol.math.modulo(256, 8)).to.be(0);
});
it('positive and positive returns a positive ', function() {
expect(ol.math.modulo(7, 8)).to.be(7);
});
it('same Dividend and Divisor returns 0', function() {
expect(ol.math.modulo(4, 4)).to.be(0);
});
it('negative and positive returns positive', function() {
expect(ol.math.modulo(-3, 4)).to.be(1);
});
it('negative and negative returns negative', function() {
expect(ol.math.modulo(-4, -5)).to.be(-4);
expect(ol.math.modulo(-3, -4)).to.be(-3);
});
it('positive and negative returns negative', function() {
expect(ol.math.modulo(3, -4)).to.be(-1);
expect(ol.math.modulo(1, -5)).to.be(-4);
expect(ol.math.modulo(6, -5)).to.be(-4);
});
describe('ol.math.lerp', function() {
it('correctly interpolated numbers', function() {
expect(ol.math.lerp(0, 0, 0)).to.be(0);
expect(ol.math.lerp(0, 1, 0)).to.be(0);
expect(ol.math.lerp(1, 11, 5)).to.be(51);
});
it('correctly interpolates floats', function() {
expect(ol.math.lerp(0, 1, 0.5)).to.be(0.5);
expect(ol.math.lerp(0.25, 0.75, 0.5)).to.be(0.5);
});
});
});
goog.require('ol.math');