Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial version of extended math helper functions and constant values
- Loading branch information
0 parents
commit c184f02
Showing
7 changed files
with
266 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,11 @@ | ||
node_modules | ||
bower_components | ||
coverage | ||
.sass-cache | ||
yarn.lock | ||
npm-debug.log | ||
yarn-error.log | ||
Thumbs.db | ||
desktop.ini | ||
.DS_Store | ||
!.gitkeep |
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,9 @@ | ||
node_modules | ||
bower_components | ||
src | ||
test | ||
coverage | ||
.sass-cache | ||
package-lock.json | ||
yarn.lock | ||
gulpfile.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,28 @@ | ||
# Extended Math | ||
|
||
A collection mathemtatical helper functions and constant values. | ||
|
||
## Client-Side Usage | ||
|
||
```html | ||
<script src="extended-math.js"></script> | ||
|
||
<script type="text/javascript"> | ||
extendedMath.clamp(5, 0, 2); // => 2 | ||
</script> | ||
``` | ||
|
||
## Server-Side Usage | ||
|
||
```javascript | ||
const extendedMath = require("extended-math"); | ||
|
||
extendedMath.clamp(5, 0, 2); // => 2 | ||
``` | ||
|
||
## Installation | ||
|
||
To install this module: | ||
```bash | ||
npm install extended-math | ||
``` |
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,89 @@ | ||
(function(global, factory) { | ||
typeof exports === "object" && typeof module !== "undefined" ? module.exports = factory() : | ||
typeof define === "function" && define.amd ? define(factory) : | ||
(global.extendedMath = factory()); | ||
} (this, function() { | ||
|
||
"use strict"; | ||
|
||
if(typeof require !== "undefined") { | ||
if(typeof utilities === "undefined") { | ||
global.utilities = require("extra-utilities"); | ||
} | ||
} | ||
|
||
let extendedMath = { }; | ||
|
||
extendedMath.HalfPI = 1.57079632679489661923; | ||
extendedMath.QuarterPI = 0.78539816339744830962; | ||
extendedMath.TwoPI = 6.28318530717958647693; | ||
|
||
extendedMath.clamp = function(value, min, max) { | ||
return utilities.isInvalidNumber(value) || utilities.isInvalidNumber(min) || utilities.isInvalidNumber(max) ? NaN : value < min ? min : value > max ? max : value; | ||
}; | ||
|
||
extendedMath.distance = function(a, b) { | ||
return utilities.isInvalidNumber(a) || utilities.isInvalidNumber(b) ? NaN : Math.abs(b - a); | ||
}; | ||
|
||
extendedMath.radiansToDegrees = function(value) { | ||
return utilities.isInvalidNumber(value) ? NaN : value * (180 / Math.PI); | ||
}; | ||
|
||
extendedMath.degreesToRadians = function(value) { | ||
return utilities.isInvalidNumber(value) ? NaN : value * (Math.PI / 180); | ||
}; | ||
|
||
extendedMath.compareAnglesDegrees = function(a, b) { | ||
if(utilities.isInvalidNumber(a) || utilities.isInvalidNumber(b)) { | ||
return NaN; | ||
} | ||
|
||
if(a == b) { | ||
return 0; | ||
} | ||
|
||
let c = a % 360; | ||
let d = b % 360; | ||
|
||
if(c < 0) { | ||
c += 360; | ||
} | ||
|
||
if(d < 0) { | ||
d += 360; | ||
} | ||
|
||
if(c === d) { | ||
return 0; | ||
} | ||
|
||
return Math.cos(extendedMath.degreesToRadians(a - b) + (Math.PI / 2)) < 0 ? -1 : 1; | ||
}; | ||
|
||
extendedMath.compareAnglesRadians = function(a, b) { | ||
return utilities.isInvalidNumber(a) || utilities.isInvalidNumber(b) ? NaN : extendedMath.compareAnglesDegrees(extendedMath.radiansToDegrees(a), extendedMath.radiansToDegrees(b)); | ||
}; | ||
|
||
extendedMath.lerp = function(a, b, amount) { | ||
if(utilities.isInvalidNumber(a) || utilities.isInvalidNumber(b) || utilities.isInvalidNumber(amount)) { | ||
return NaN; | ||
} | ||
|
||
if(amount === 0) { | ||
return a; | ||
} | ||
else if(amount === 1) { | ||
return b; | ||
} | ||
|
||
return a + (b - a) * amount; | ||
}; | ||
|
||
extendedMath.normalize = function(value, min, max) { | ||
return utilities.isInvalidNumber(value) || utilities.isInvalidNumber(min) || utilities.isInvalidNumber(max) ? NaN : (value - min) / (max - min); | ||
}; | ||
|
||
return extendedMath; | ||
|
||
})); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,29 @@ | ||
{ | ||
"name": "extended-math", | ||
"version": "1.0.0", | ||
"description": "A collection mathemtatical helper functions and constant values.", | ||
"main": "dist/extended-math.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/nitro404/extended-math.git" | ||
}, | ||
"keywords": [ | ||
"extended", | ||
"math", | ||
"helper", | ||
"functions", | ||
"constants" | ||
], | ||
"author": "Kevin Scroggins", | ||
"license": "ISC", | ||
"bugs": { | ||
"url": "https://github.com/nitro404/extended-math/issues" | ||
}, | ||
"homepage": "https://github.com/nitro404/extended-math#readme", | ||
"dependencies": { | ||
"extra-utilities": "^1.1.0" | ||
} | ||
} |
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,79 @@ | ||
if(typeof require !== "undefined") { | ||
if(typeof utilities === "undefined") { | ||
global.utilities = require("extra-utilities"); | ||
} | ||
} | ||
|
||
let extendedMath = { }; | ||
|
||
extendedMath.HalfPI = 1.57079632679489661923; | ||
extendedMath.QuarterPI = 0.78539816339744830962; | ||
extendedMath.TwoPI = 6.28318530717958647693; | ||
|
||
extendedMath.clamp = function(value, min, max) { | ||
return utilities.isInvalidNumber(value) || utilities.isInvalidNumber(min) || utilities.isInvalidNumber(max) ? NaN : value < min ? min : value > max ? max : value; | ||
}; | ||
|
||
extendedMath.distance = function(a, b) { | ||
return utilities.isInvalidNumber(a) || utilities.isInvalidNumber(b) ? NaN : Math.abs(b - a); | ||
}; | ||
|
||
extendedMath.radiansToDegrees = function(value) { | ||
return utilities.isInvalidNumber(value) ? NaN : value * (180 / Math.PI); | ||
}; | ||
|
||
extendedMath.degreesToRadians = function(value) { | ||
return utilities.isInvalidNumber(value) ? NaN : value * (Math.PI / 180); | ||
}; | ||
|
||
extendedMath.compareAnglesDegrees = function(a, b) { | ||
if(utilities.isInvalidNumber(a) || utilities.isInvalidNumber(b)) { | ||
return NaN; | ||
} | ||
|
||
if(a == b) { | ||
return 0; | ||
} | ||
|
||
let c = a % 360; | ||
let d = b % 360; | ||
|
||
if(c < 0) { | ||
c += 360; | ||
} | ||
|
||
if(d < 0) { | ||
d += 360; | ||
} | ||
|
||
if(c === d) { | ||
return 0; | ||
} | ||
|
||
return Math.cos(extendedMath.degreesToRadians(a - b) + (Math.PI / 2)) < 0 ? -1 : 1; | ||
}; | ||
|
||
extendedMath.compareAnglesRadians = function(a, b) { | ||
return utilities.isInvalidNumber(a) || utilities.isInvalidNumber(b) ? NaN : extendedMath.compareAnglesDegrees(extendedMath.radiansToDegrees(a), extendedMath.radiansToDegrees(b)); | ||
}; | ||
|
||
extendedMath.lerp = function(a, b, amount) { | ||
if(utilities.isInvalidNumber(a) || utilities.isInvalidNumber(b) || utilities.isInvalidNumber(amount)) { | ||
return NaN; | ||
} | ||
|
||
if(amount === 0) { | ||
return a; | ||
} | ||
else if(amount === 1) { | ||
return b; | ||
} | ||
|
||
return a + (b - a) * amount; | ||
}; | ||
|
||
extendedMath.normalize = function(value, min, max) { | ||
return utilities.isInvalidNumber(value) || utilities.isInvalidNumber(min) || utilities.isInvalidNumber(max) ? NaN : (value - min) / (max - min); | ||
}; | ||
|
||
return extendedMath; |