diff --git a/src/ol/attribution.js b/src/ol/attribution.js index 9ed407153f..ffd96e29ac 100644 --- a/src/ol/attribution.js +++ b/src/ol/attribution.js @@ -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; } diff --git a/src/ol/coordinate.js b/src/ol/coordinate.js index 30730145dd..d43bddeed6 100644 --- a/src/ol/coordinate.js +++ b/src/ol/coordinate.js @@ -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 ' + diff --git a/src/ol/geom/flat/closestflatgeom.js b/src/ol/geom/flat/closestflatgeom.js index 2acdfdac8e..1c09a5b97b 100644 --- a/src/ol/geom/flat/closestflatgeom.js +++ b/src/ol/geom/flat/closestflatgeom.js @@ -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; diff --git a/src/ol/geom/flat/interpolateflatgeom.js b/src/ol/geom/flat/interpolateflatgeom.js index ef76bbcef0..392642dd90 100644 --- a/src/ol/geom/flat/interpolateflatgeom.js +++ b/src/ol/geom/flat/interpolateflatgeom.js @@ -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); diff --git a/src/ol/geom/polygon.js b/src/ol/geom/polygon.js index 741589ac58..e27c51e08f 100644 --- a/src/ol/geom/polygon.js +++ b/src/ol/geom/polygon.js @@ -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)); } diff --git a/src/ol/math.js b/src/ol/math.js index 135f3dc1b8..0e4e8150d7 100644 --- a/src/ol/math.js +++ b/src/ol/math.js @@ -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); +}; diff --git a/src/ol/reproj/triangulation.js b/src/ol/reproj/triangulation.js index 9c60063ded..e893c134da 100644 --- a/src/ol/reproj/triangulation.js +++ b/src/ol/reproj/triangulation.js @@ -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]; } diff --git a/src/ol/source/tilearcgisrestsource.js b/src/ol/source/tilearcgisrestsource.js index d72f00eafb..c0ed7fe440 100644 --- a/src/ol/source/tilearcgisrestsource.js +++ b/src/ol/source/tilearcgisrestsource.js @@ -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]; } diff --git a/src/ol/source/tilewmssource.js b/src/ol/source/tilewmssource.js index f6d57cdf7e..e1c7321567 100644 --- a/src/ol/source/tilewmssource.js +++ b/src/ol/source/tilewmssource.js @@ -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); diff --git a/src/ol/tileurlfunction.js b/src/ol/tileurlfunction.js index 49533af14a..3216d0cee3 100644 --- a/src/ol/tileurlfunction.js +++ b/src/ol/tileurlfunction.js @@ -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); } }); diff --git a/test/spec/ol/math.test.js b/test/spec/ol/math.test.js index fbff4e80bb..e3e3b78d77 100644 --- a/test/spec/ol/math.test.js +++ b/test/spec/ol/math.test.js @@ -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');