1 | (function(global, factory) {
|
2 | typeof exports === "object" && typeof module !== "undefined" ? module.exports = factory() :
|
3 | typeof define === "function" && define.amd ? define(factory) :
|
4 | (global.extendedMath = factory());
|
5 | } (this, function() {
|
6 |
|
7 | "use strict";
|
8 |
|
9 | var extendedMath = { };
|
10 |
|
11 | function isInvalidNumber(value) {
|
12 | return typeof value !== "number" || isNaN(value) || value === -Infinity || value === Infinity;
|
13 | }
|
14 |
|
15 | extendedMath.HalfPI = 1.57079632679489661923;
|
16 | extendedMath.QuarterPI = 0.78539816339744830962;
|
17 | extendedMath.TwoPI = 6.28318530717958647693;
|
18 |
|
19 | extendedMath.clamp = function(value, min, max) {
|
20 | return isInvalidNumber(value) || isInvalidNumber(min) || isInvalidNumber(max) ? NaN : value < min ? min : value > max ? max : value;
|
21 | };
|
22 |
|
23 | extendedMath.distance = function(a, b) {
|
24 | return isInvalidNumber(a) || isInvalidNumber(b) ? NaN : Math.abs(b - a);
|
25 | };
|
26 |
|
27 | extendedMath.radiansToDegrees = function(value) {
|
28 | return isInvalidNumber(value) ? NaN : value * (180 / Math.PI);
|
29 | };
|
30 |
|
31 | extendedMath.degreesToRadians = function(value) {
|
32 | return isInvalidNumber(value) ? NaN : value * (Math.PI / 180);
|
33 | };
|
34 |
|
35 | extendedMath.compareAnglesDegrees = function(a, b) {
|
36 | if(isInvalidNumber(a) || isInvalidNumber(b)) {
|
37 | return NaN;
|
38 | }
|
39 |
|
40 | if(a === b) {
|
41 | return 0;
|
42 | }
|
43 |
|
44 | var c = a % 360;
|
45 | var d = b % 360;
|
46 |
|
47 | if(c < 0) {
|
48 | c += 360;
|
49 | }
|
50 |
|
51 | if(d < 0) {
|
52 | d += 360;
|
53 | }
|
54 |
|
55 | if(c === d) {
|
56 | return 0;
|
57 | }
|
58 |
|
59 | return Math.cos(extendedMath.degreesToRadians(a - b) + (Math.PI / 2)) < 0 ? -1 : 1;
|
60 | };
|
61 |
|
62 | extendedMath.compareAnglesRadians = function(a, b) {
|
63 | return isInvalidNumber(a) || isInvalidNumber(b) ? NaN : extendedMath.compareAnglesDegrees(extendedMath.radiansToDegrees(a), extendedMath.radiansToDegrees(b));
|
64 | };
|
65 |
|
66 | extendedMath.lerp = function(a, b, amount) {
|
67 | if(isInvalidNumber(a) || isInvalidNumber(b) || isInvalidNumber(amount)) {
|
68 | return NaN;
|
69 | }
|
70 |
|
71 | if(amount === 0) {
|
72 | return a;
|
73 | }
|
74 | else if(amount === 1) {
|
75 | return b;
|
76 | }
|
77 |
|
78 | return a + (b - a) * amount;
|
79 | };
|
80 |
|
81 | extendedMath.normalize = function(value, min, max) {
|
82 | return isInvalidNumber(value) || isInvalidNumber(min) || isInvalidNumber(max) ? NaN : (value - min) / (max - min);
|
83 | };
|
84 |
|
85 | return extendedMath;
|
86 |
|
87 | }));
|
88 |
|
89 |
|