-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit bcb9309
Showing
8 changed files
with
370 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
### 1.0.1 (2015-10-27) | ||
|
||
- Fixed JSDoc annotation | ||
|
||
### 1.0.0 (2015-09-19) | ||
|
||
- Initial version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2016 mpk | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# math | ||
|
||
Math utilities. | ||
|
||
## Install | ||
|
||
npm install mpk/math | ||
|
||
## API | ||
|
||
#### .clamp( value, min, max ) | ||
|
||
Clamp the input value to the specified bounds. | ||
|
||
#### .fuzzyEquals( value1, value2, [tolerance] ) | ||
|
||
Test if the input values are equal within specified tolerance (default is 0.00001). | ||
|
||
#### .mapLinear( value, x1, x2, y1, y2 ) | ||
|
||
Map the input value from range [x1, x2] to range [y1, y2]. | ||
|
||
#### .mapLinearClamp( value, x1, x2, y1, y2 ) | ||
|
||
Map the input value from range [x1, x2] to range [y1, y2]. | ||
|
||
#### .modulo( dividend, divisor ) | ||
|
||
Perform modulo operation (result has the same sign as divisor). | ||
|
||
#### .randFloat( min, max ) | ||
|
||
Generate pseudo-random float from [min, max) interval. | ||
|
||
#### .randInt( min, max ) | ||
|
||
Generate pseudo-random integer from [min, max) interval. | ||
|
||
#### .roundToNearest( value, nearest ) | ||
|
||
Round the input value to the nearest multiple of the passed number. | ||
|
||
#### .toDegrees( radians ) | ||
|
||
Convert radians to degrees. | ||
|
||
#### .toRadians( degrees ) | ||
|
||
Convert degrees to radians. | ||
|
||
## License | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "math", | ||
"version": "1.0.1", | ||
"description": "Math utilities", | ||
"private": true, | ||
"main": "source/index.js", | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"jasmine-core": "2.3.4", | ||
"karma": "0.13.9", | ||
"karma-browserify": "4.3.0", | ||
"karma-jasmine": "0.3.6" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/mpk/math" | ||
}, | ||
"author": "mpk", | ||
"license": "MIT", | ||
"scripts": { | ||
"test": "karma start specs/support/options.js" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/** | ||
* Math utilities | ||
*/ | ||
module.exports = { | ||
|
||
/** | ||
* Clamp the input value to the specified bounds. | ||
* | ||
* @param {number} value | ||
* @param {number} min | ||
* @param {number} max | ||
* @return {number} | ||
*/ | ||
clamp: function( value, min, max ) { | ||
return (value < min) ? min : ((value > max) ? max : value); | ||
}, | ||
|
||
/** | ||
* Test if the input values are equal within specified tolerance (default is 0.00001). | ||
* | ||
* @param {number} value1 | ||
* @param {number} value2 | ||
* @param {number=} tolerance | ||
* @return {boolean} | ||
*/ | ||
fuzzyEquals: function( value1, value2, tolerance ) { | ||
return Math.abs(value1 - value2) <= (tolerance || 0.00001); | ||
}, | ||
|
||
/** | ||
* Map the input value from range [x1, x2] to range [y1, y2]. | ||
* | ||
* @param {number} value | ||
* @param {number} x1 | ||
* @param {number} x2 | ||
* @param {number} y1 | ||
* @param {number} y2 | ||
* @return {number} | ||
*/ | ||
mapLinear: function( value, x1, x2, y1, y2 ) { | ||
return y1 + (value - x1) * (y2 - y1) / (x2 - x1); | ||
}, | ||
|
||
/** | ||
* Map the input value from range [x1, x2] to range [y1, y2] and clamp the mapped value between y1 and y2. | ||
* | ||
* @param {number} value | ||
* @param {number} x1 | ||
* @param {number} x2 | ||
* @param {number} y1 | ||
* @param {number} y2 | ||
* @return {number} | ||
*/ | ||
mapLinearClamp: function( value, x1, x2, y1, y2 ) { | ||
return this.clamp(this.mapLinear(value, x1, x2, y1, y2), (y1 < y2) ? y1 : y2, (y1 < y2) ? y2 : y1); | ||
}, | ||
|
||
/** | ||
* Perform modulo operation (result has the same sign as divisor). | ||
* | ||
* @param {number} dividend | ||
* @param {number} divisor | ||
* @return {number} | ||
*/ | ||
modulo: function( dividend, divisor ) { | ||
var result = dividend % divisor; | ||
|
||
return (dividend * divisor < 0) ? result + divisor : result; | ||
}, | ||
|
||
/** | ||
* Generate pseudo-random float from [min, max) interval. | ||
* | ||
* @param {number} min | ||
* @param {number} max | ||
* @return {number} | ||
*/ | ||
randFloat: function( min, max ) { | ||
return min + Math.random() * (max - min); | ||
}, | ||
|
||
/** | ||
* Generate pseudo-random integer from [min, max) interval. | ||
* | ||
* @param {number} min | ||
* @param {number} max | ||
* @return {number} | ||
*/ | ||
randInt: function( min, max ) { | ||
return Math.floor(this.randFloat(min, max)); | ||
}, | ||
|
||
/** | ||
* Round the input value to the nearest multiple of the passed number. | ||
* | ||
* @param {number} value | ||
* @param {number} nearest | ||
* @return {number} | ||
*/ | ||
roundToNearest: function( value, nearest ) { | ||
return Math.round(value / nearest) * nearest; | ||
}, | ||
|
||
/** | ||
* Convert radians to degrees. | ||
* | ||
* @param {number} radians | ||
* @return {number} | ||
*/ | ||
toDegrees: function( radians ) { | ||
return radians * 180 / Math.PI; | ||
}, | ||
|
||
/** | ||
* Convert degrees to radians. | ||
* | ||
* @param {number} degrees | ||
* @return {number} | ||
*/ | ||
toRadians: function( degrees ) { | ||
return degrees * Math.PI / 180; | ||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/** | ||
* Math utilities | ||
*/ | ||
var math = require('../source/index'); | ||
|
||
describe('clamp', function() { | ||
|
||
it('clamps the input value', function() { | ||
expect(math.clamp(-5, -2, 4)).toBe(-2); | ||
expect(math.clamp(-2, -2, 4)).toBe(-2); | ||
expect(math.clamp(0, -2, 4)).toBe(0); | ||
expect(math.clamp(4, -2, 4)).toBe(4); | ||
expect(math.clamp(9, -2, 4)).toBe(4); | ||
}); | ||
|
||
}); | ||
|
||
describe('fuzzyEquals', function() { | ||
|
||
it('tests equality with default tolerance', function() { | ||
expect(math.fuzzyEquals(1.00000, 0.999999)).toBe(true); | ||
expect(math.fuzzyEquals(1.00000, 0.99998)).toBe(false); | ||
expect(math.fuzzyEquals(1, 2)).toBe(false); | ||
}); | ||
|
||
it('tests equality with custom tolerance', function() { | ||
expect(math.fuzzyEquals(3.2, 1, 2.5)).toBe(true); | ||
expect(math.fuzzyEquals(5.2, 1, 2.5)).toBe(false); | ||
expect(math.fuzzyEquals(6, 4, 3)).toBe(true); | ||
}); | ||
|
||
}); | ||
|
||
describe('mapLinear', function() { | ||
|
||
it('maps the input value', function() { | ||
expect(math.mapLinear(-1, 0, 2, 10, 20)).toBe(5); | ||
expect(math.mapLinear(0, 0, 2, 10, 20)).toBe(10); | ||
expect(math.mapLinear(1, 0, 2, 10, 20)).toBe(15); | ||
expect(math.mapLinear(2, 0, 2, 10, 20)).toBe(20); | ||
expect(math.mapLinear(3, 0, 2, 10, 20)).toBe(25); | ||
}); | ||
|
||
}); | ||
|
||
describe('mapLinearClamp', function() { | ||
|
||
it('maps the input value and clamps the result', function() { | ||
expect(math.mapLinearClamp(-1, 0, 2, 10, 20)).toBe(10); | ||
expect(math.mapLinearClamp(0, 0, 2, 10, 20)).toBe(10); | ||
expect(math.mapLinearClamp(1, 0, 2, 10, 20)).toBe(15); | ||
expect(math.mapLinearClamp(2, 0, 2, 10, 20)).toBe(20); | ||
expect(math.mapLinearClamp(3, 0, 2, 10, 20)).toBe(20); | ||
expect(math.mapLinearClamp(-1, 0, 2, 20, 10)).toBe(20); | ||
expect(math.mapLinearClamp(3, 0, 2, 20, 10)).toBe(10); | ||
}); | ||
|
||
}); | ||
|
||
describe('modulo', function() { | ||
|
||
it('performs modulo operation and retains the divisor sign', function() { | ||
expect(math.modulo(256, 8)).toBe(0); | ||
expect(math.modulo(7, 8)).toBe(7); | ||
expect(math.modulo(23, 8)).toBe(7); | ||
expect(math.modulo(-1, 8)).toBe(7); | ||
expect(math.modulo(1, -5)).toBe(-4); | ||
expect(math.modulo(6, -5)).toBe(-4); | ||
expect(math.modulo(-4, -5)).toBe(-4); | ||
}); | ||
|
||
}); | ||
|
||
describe('randFloat', function() { | ||
|
||
it('generates pseudo-random float', function() { | ||
expect(math.randFloat(0, 1)).toEqual(jasmine.any(Number)); | ||
expect(math.randFloat(0, 1)).toBeLessThan(1); | ||
expect(math.randFloat(0, 1)).not.toBeLessThan(0); | ||
expect(math.randFloat(-3, 6)).toBeLessThan(6); | ||
expect(math.randFloat(-3, 6)).not.toBeLessThan(-3); | ||
}); | ||
|
||
}); | ||
|
||
describe('randInt', function() { | ||
|
||
it('generates pseudo-random integer', function() { | ||
expect(math.randInt(4, 6)).toEqual(jasmine.any(Number)); | ||
expect(math.randInt(4, 6)).not.toBeLessThan(4); | ||
expect(math.randInt(4, 6)).not.toBeGreaterThan(5); | ||
}); | ||
|
||
}); | ||
|
||
describe('roundToNearest', function() { | ||
|
||
it('rounds to the nearest multiple', function() { | ||
expect(math.roundToNearest(2, 5)).toBe(0); | ||
expect(math.roundToNearest(3, 5)).toBe(5); | ||
expect(math.roundToNearest(16, 5)).toBe(15); | ||
expect(math.roundToNearest(-1, 5)).toBe(0); | ||
expect(math.roundToNearest(-3, 5)).toBe(-5); | ||
}); | ||
|
||
}); | ||
|
||
describe('toDegrees', function() { | ||
|
||
it('converts radians to degrees', function() { | ||
expect(math.toDegrees(-12.5663706143591729)).toBe(-720); | ||
expect(math.toDegrees(-3.1415926535897932)).toBe(-180); | ||
expect(math.toDegrees(0)).toBe(0); | ||
expect(math.toDegrees(3.1415926535897932)).toBe(180); | ||
expect(math.toDegrees(12.5663706143591729)).toBe(720); | ||
}); | ||
|
||
}); | ||
|
||
describe('toRadians', function() { | ||
|
||
it('converts degrees to radians', function() { | ||
expect(math.toRadians(-720)).toBe(-12.5663706143591729); | ||
expect(math.toRadians(-180)).toBe(-3.1415926535897932); | ||
expect(math.toRadians(0)).toBe(0); | ||
expect(math.toRadians(180)).toBe(3.1415926535897932); | ||
expect(math.toRadians(720)).toBe(12.5663706143591729); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Test runner options | ||
*/ | ||
module.exports = function( options ) { | ||
options.set({ | ||
files: ['../*-spec.js'], | ||
frameworks: ['browserify', 'jasmine'], | ||
plugins: ['karma-browserify', 'karma-jasmine'], | ||
preprocessors: { '../*-spec.js': ['browserify'] } | ||
}); | ||
}; |