Merge pull request #10989 from mike-000/patch-4
Add log2 function to ol/math
This commit is contained in:
@@ -40,6 +40,31 @@ export const cosh = (function () {
|
|||||||
return cosh;
|
return cosh;
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the base 2 logarithm of a given number. The method will use the
|
||||||
|
* native `Math.log2` function if it is available, otherwise the base 2
|
||||||
|
* logarithm will be calculated via the reference implementation of the
|
||||||
|
* Mozilla developer network.
|
||||||
|
*
|
||||||
|
* @param {number} x X.
|
||||||
|
* @return {number} Base 2 logarithm of x.
|
||||||
|
*/
|
||||||
|
export const log2 = (function () {
|
||||||
|
// Wrapped in a iife, to save the overhead of checking for the native
|
||||||
|
// implementation on every invocation.
|
||||||
|
let log2;
|
||||||
|
if ('log2' in Math) {
|
||||||
|
// The environment supports the native Math.log2 function, use it…
|
||||||
|
log2 = Math.log2;
|
||||||
|
} else {
|
||||||
|
// … else, use the reference implementation of MDN:
|
||||||
|
log2 = function (x) {
|
||||||
|
return Math.log(x) * Math.LOG2E;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return log2;
|
||||||
|
})();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the square of the closest distance between the point (x, y) and the
|
* Returns the square of the closest distance between the point (x, y) and the
|
||||||
* line segment (x1, y1) to (x2, y2).
|
* line segment (x1, y1) to (x2, y2).
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import {
|
|||||||
intersects,
|
intersects,
|
||||||
} from '../extent.js';
|
} from '../extent.js';
|
||||||
import {getTransform} from '../proj.js';
|
import {getTransform} from '../proj.js';
|
||||||
import {modulo} from '../math.js';
|
import {log2, modulo} from '../math.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Single triangle; consists of 3 source points and 3 target points.
|
* Single triangle; consists of 3 source points and 3 target points.
|
||||||
@@ -169,7 +169,7 @@ class Triangulation {
|
|||||||
? Math.max(
|
? Math.max(
|
||||||
0,
|
0,
|
||||||
Math.ceil(
|
Math.ceil(
|
||||||
Math.log2(
|
log2(
|
||||||
getArea(targetExtent) /
|
getArea(targetExtent) /
|
||||||
(opt_destinationResolution *
|
(opt_destinationResolution *
|
||||||
opt_destinationResolution *
|
opt_destinationResolution *
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import {
|
|||||||
clamp,
|
clamp,
|
||||||
cosh,
|
cosh,
|
||||||
lerp,
|
lerp,
|
||||||
|
log2,
|
||||||
modulo,
|
modulo,
|
||||||
solveLinearSystem,
|
solveLinearSystem,
|
||||||
toDegrees,
|
toDegrees,
|
||||||
@@ -52,6 +53,32 @@ describe('ol.math.cosh', function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('ol.math.log2', function () {
|
||||||
|
it('returns the correct value at Infinity', function () {
|
||||||
|
expect(log2(Infinity)).to.eql(Infinity);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns the correct value at 3', function () {
|
||||||
|
expect(log2(3)).to.roughlyEqual(1.584962500721156, 1e-9);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns the correct value at 2', function () {
|
||||||
|
expect(log2(2)).to.eql(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns the correct value at 1', function () {
|
||||||
|
expect(log2(1)).to.eql(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns the correct value at 0', function () {
|
||||||
|
expect(log2(0)).to.eql(-Infinity);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns the correct value at -1', function () {
|
||||||
|
expect(log2(-1).toString()).to.eql('NaN');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('ol.math.solveLinearSystem', function () {
|
describe('ol.math.solveLinearSystem', function () {
|
||||||
it('calculates correctly', function () {
|
it('calculates correctly', function () {
|
||||||
const result = solveLinearSystem([
|
const result = solveLinearSystem([
|
||||||
|
|||||||
Reference in New Issue
Block a user